Created
December 19, 2017 19:23
-
-
Save biggers/7ac0118679281b27edaec927953eb8f1 to your computer and use it in GitHub Desktop.
Python3 rotating log-file configuration via "config.dictConfig"
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 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