-
-
Save alecordev/b2a540e3c58b944e21e7f56fc67e9025 to your computer and use it in GitHub Desktop.
Python logging 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
# https://docs.python.org/2/library/logging.config.html | |
# https://docs.python.org/3.6/library/logging.config.html | |
import logging | |
import logging.config | |
import datetime | |
DEFAULT_LOGGING = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'standard': { | |
'format': '%(asctime)s - %(message)s' | |
}, | |
'complete': { | |
'format': '%(asctime)s - PID: %(process)d - PNAME: %(processName)s' \ | |
' - TID: %(thread)d - TNAME: %(threadName)s' \ | |
' - %(levelname)s - %(filename)s - %(message)s', | |
}, | |
}, | |
'handlers': { | |
'default': { | |
'level': 'INFO', | |
'formatter': 'standard', | |
'class': 'logging.StreamHandler', | |
}, | |
'file': { | |
'level': 'INFO', | |
'formatter': 'standard', | |
'class': 'logging.FileHandler', | |
'filename': datetime.datetime.now().strftime('%Y%m%d.log'), | |
}, | |
'rewrite': { | |
'level': 'INFO', | |
'formatter': 'complete', | |
'class': 'logging.FileHandler', | |
'filename': datetime.datetime.now().strftime('%Y%m%d2.log'), | |
'mode': 'w', | |
}, | |
}, | |
'loggers': { | |
'': { | |
'handlers': ['default', 'file', 'rewrite'], | |
'level': 'INFO', | |
'propagate': True | |
}, | |
'another.module': { | |
'level': 'DEBUG', | |
}, | |
} | |
} | |
logging.config.dictConfig(DEFAULT_LOGGING) | |
logging.info('Hello, log') | |
# def setup_logger(): | |
# """ | |
# Initializes and setups 2 logger handlers: console (INFO) and file (DEBUG). | |
# """ | |
# logger = logging.getLogger(__name__) | |
# logger.setLevel(logging.DEBUG) | |
# formatter = logging.Formatter('%(asctime)s - PID: %(process)d - PNAME: %(processName)s' \ | |
# ' - TID: %(thread)d - TNAME: %(threadName)s' \ | |
# ' - %(levelname)s - %(filename)s - %(message)s') | |
# ch = logging.StreamHandler() | |
# ch.setFormatter(formatter) | |
# ch.setLevel(logging.INFO) | |
# logger.addHandler(ch) | |
# filename = datetime.datetime.now().strftime('%Y%m%d.log') | |
# fh = logging.FileHandler(filename, mode='w') | |
# fh.setFormatter(formatter) | |
# fh.setLevel(logging.DEBUG) | |
# logger.addHandler(fh) | |
# return logger | |
# logger = setup_logger() |
For what I remember, the empty str is the way to define the default logger.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
I want to ask why in line 42 the key is an empty string.
I have read the doc and I think it must be a logger's name, but it still works when I call
logger = logging.getLogger('')
orlogger = logging.getLogger("some_name")
.But if I set the name in the key, it will not work.
I don't know why.
After all, thanks for your nice work!