From 935fe33a478276ef1f909054cc1edc51e97aa0d4 Mon Sep 17 00:00:00 2001 From: Carlos Killpack Date: Sat, 29 Jun 2013 10:27:40 -0600 Subject: [PATCH] Real logging, please incorporate into new and existing code. --- src/debug.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/debug.py diff --git a/src/debug.py b/src/debug.py new file mode 100644 index 00000000..d8033f2d --- /dev/null +++ b/src/debug.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +''' +Logging and debuging facility +============================= + +Levels: + DEBUG Detailed information, typically of interest only when diagnosing problems. + INFO Confirmation that things are working as expected. + WARNING An indication that something unexpected happened, or indicative of some problem in the + near future (e.g. ‘disk space low’). The software is still working as expected. + ERROR Due to a more serious problem, the software has not been able to perform some function. + CRITICAL A serious error, indicating that the program itself may be unable to continue running. + +There are three loggers: `console_only`, `file_only` and `both`. + +Use: `from debug import logger` to import this facility into whatever module you wish to log messages from. + Logging is thread-safe so you don't have to worry about locks, just import and log. +''' +import logging +import logging.config + +# TODO(xj9): Get from a config file. +log_level = 'DEBUG' + +logging.config.dictConfig({ + 'version': 1, + 'formatters': { + 'default': { + 'format': '%(asctime)s - %(levelname)s - %(message)s', + }, + }, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + 'formatter': 'default', + 'level': log_level, + 'stream': 'ext://sys.stdout' + }, + 'file': { + 'class': 'logging.handlers.RotatingFileHandler', + 'formatter': 'default', + 'level': log_level, + 'filename': 'bm.log', + 'maxBytes': 1024, + 'backupCount': 0, + } + }, + 'loggers': { + 'console_only': { + 'handlers': ['console'], + }, + 'file_only': { + 'handlers': ['file'], + }, + 'both': { + 'handlers': ['console', 'file'], + }, + }, + 'root': { + 'level': log_level, + 'handlers': ['console'], + }, +}) +# TODO (xj9): Get from a config file. +logger = logging.getLogger('console_only') -- 2.45.1