|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +''' |
| 3 | +Logging and debuging facility |
| 4 | +============================= |
| 5 | +
|
| 6 | +Levels: |
| 7 | + DEBUG Detailed information, typically of interest only when diagnosing problems. |
| 8 | + INFO Confirmation that things are working as expected. |
| 9 | + WARNING An indication that something unexpected happened, or indicative of some problem in the |
| 10 | + near future (e.g. ‘disk space low’). The software is still working as expected. |
| 11 | + ERROR Due to a more serious problem, the software has not been able to perform some function. |
| 12 | + CRITICAL A serious error, indicating that the program itself may be unable to continue running. |
| 13 | +
|
| 14 | +There are three loggers: `console_only`, `file_only` and `both`. |
| 15 | +
|
| 16 | +Use: `from debug import logger` to import this facility into whatever module you wish to log messages from. |
| 17 | + Logging is thread-safe so you don't have to worry about locks, just import and log. |
| 18 | +''' |
| 19 | +import logging |
| 20 | +import logging.config |
| 21 | + |
| 22 | +# TODO(xj9): Get from a config file. |
| 23 | +log_level = 'DEBUG' |
| 24 | + |
| 25 | +logging.config.dictConfig({ |
| 26 | + 'version': 1, |
| 27 | + 'formatters': { |
| 28 | + 'default': { |
| 29 | + 'format': '%(asctime)s - %(levelname)s - %(message)s', |
| 30 | + }, |
| 31 | + }, |
| 32 | + 'handlers': { |
| 33 | + 'console': { |
| 34 | + 'class': 'logging.StreamHandler', |
| 35 | + 'formatter': 'default', |
| 36 | + 'level': log_level, |
| 37 | + 'stream': 'ext://sys.stdout' |
| 38 | + }, |
| 39 | + 'file': { |
| 40 | + 'class': 'logging.handlers.RotatingFileHandler', |
| 41 | + 'formatter': 'default', |
| 42 | + 'level': log_level, |
| 43 | + 'filename': 'bm.log', |
| 44 | + 'maxBytes': 1024, |
| 45 | + 'backupCount': 0, |
| 46 | + } |
| 47 | + }, |
| 48 | + 'loggers': { |
| 49 | + 'console_only': { |
| 50 | + 'handlers': ['console'], |
| 51 | + }, |
| 52 | + 'file_only': { |
| 53 | + 'handlers': ['file'], |
| 54 | + }, |
| 55 | + 'both': { |
| 56 | + 'handlers': ['console', 'file'], |
| 57 | + }, |
| 58 | + }, |
| 59 | + 'root': { |
| 60 | + 'level': log_level, |
| 61 | + 'handlers': ['console'], |
| 62 | + }, |
| 63 | +}) |
| 64 | +# TODO (xj9): Get from a config file. |
| 65 | +logger = logging.getLogger('console_only') |
0 commit comments