Skip to content

Instantly share code, notes, and snippets.

@rafalf
Created October 6, 2017 07:19
Show Gist options
  • Save rafalf/6f7a6086704667af17ebbf5be010886a to your computer and use it in GitHub Desktop.
Save rafalf/6f7a6086704667af17ebbf5be010886a to your computer and use it in GitHub Desktop.
Python - Rollover logging with logging config - console & file
import logging
from logging import config
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
LOGGING_CONFIG = {
'formatters': {
'brief': {
'format': '[%(asctime)s][%(levelname)s] %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'brief'
},
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'brief',
'filename': os.path.join(BASE_DIR, 'logs', 'logger.log'),
'maxBytes': 5*1024*1024,
'backupCount': 10
}
},
'loggers': {
'main': {
'propagate': False,
'handlers': ['console', 'file'],
'level': LOG_LEVEL
}
},
'version': 1
}
def get_logger():
logging.config.dictConfig(LOGGING_CONFIG)
log = logging.getLogger('main')
return log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment