Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python3 rotating log-file configuration via "config.dictConfig"
import sys
import logging
import logging.config
import random
import string
# "thank you" to folks on StackOverflow.com for various ideas,
# for this example. Works with Python3.
logger = logging.getLogger('prober')
dict_conf = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': "[%(levelname)4.4s %(asctime)s %(module)s:%(lineno)d] "
"%(message)s",
'datefmt': "%Y-%m-%d+%H:%M:%S",
},
},
'handlers': {
'default': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'standard',
'stream': sys.stderr,
},
'rotating_to_file': {
'level': 'DEBUG',
'class': "logging.handlers.RotatingFileHandler",
'formatter': 'standard',
"filename": "app.log",
"maxBytes": 10000,
"backupCount": 10,
},
},
'loggers': {
'': {
'handlers': ['default', 'rotating_to_file'],
'level': 'INFO',
'propagate': True
}
}
}
# dict_conf['loggers']['']['handlers']=['default',]
logging.config.dictConfig(dict_conf)
N = 20
for n in range(1, 100):
msg = ''.join(random.choices(string.ascii_lowercase + string.digits, k=N))
if n % 3 == 0:
logger.info(msg)
else:
logger.warn(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.