Created
October 6, 2017 07:19
-
-
Save rafalf/6f7a6086704667af17ebbf5be010886a to your computer and use it in GitHub Desktop.
Python - Rollover logging with logging config - console & file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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