Python logging
Setting up logging in python application with features like rotating logger.
Introduction
- Read The Hitchhiker’s Guide to Python - Logging.
- Read Logging HOWTO complete.
Setting up logging:
I decided to use dict
based config.
- An example dictionary-based configuration
- Elegant setup of Python logging in Django
- logging.config — Logging configuration
- LogRecord attributes
I used the following configuration:
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {
'format': '[%(asctime)s] [%(levelname)s] [%(name)s:%(funcName)s:%(lineno)d] [%(process)d:%(thread)d] %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'filters': {
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
},
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'altin.log',
'maxBytes': '16777216', # 16megabytes
'formatter': 'verbose',
'backupCount': 10
},
},
'loggers': {
},
'root': {
'handlers': ['file'],
'propagate': True,
'level': 'DEBUG'
}
}
And used it as following:
import logging
import logging.config
logging.config.dictConfig(LOGGING)
logger = logging.getLogger(__name__)
logger.debug("Sample msg")
Compression
Haven’t implemented it but the process is very simple as mentioned in Python, want logging with log rotation and compression.