3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-24 19:52:53 +01:00

log: respect child loggers' levels if they are lower than the main one's

Closes #185.
This commit is contained in:
James Lu 2016-03-25 15:45:26 -07:00
parent 2b16f25b61
commit a4395ed989

19
log.py
View File

@ -20,14 +20,22 @@ curdir = os.path.dirname(os.path.realpath(__file__))
logdir = os.path.join(curdir, 'log')
os.makedirs(logdir, exist_ok=True)
# Basic logging setup, set up here on first import, logs to STDOUT based
# on the log level configured.
_format = '%(asctime)s [%(levelname)s] %(message)s'
logformatter = logging.Formatter(_format)
logging.basicConfig(level=stdout_level, format=_format)
# Set up logging to STDERR
stdout_handler = logging.StreamHandler()
stdout_handler.setFormatter(logformatter)
stdout_handler.setLevel(stdout_level)
# Get the main logger object; plugins can import this variable for convenience.
log = logging.getLogger()
log.addHandler(stdout_handler)
# This is confusing, but we have to set the root logger to accept all events. Only this way
# can other loggers filter out events on their own, instead of having everything dropped by
# the root logger. https://stackoverflow.com/questions/16624695
log.setLevel(1)
def makeFileLogger(filename, level=None):
"""
@ -40,8 +48,9 @@ def makeFileLogger(filename, level=None):
filelogger = logging.FileHandler(target, mode='w')
filelogger.setFormatter(logformatter)
if level: # Custom log level was defined, use that instead.
filelogger.setLevel(level)
# If no log level is specified, use the same one as STDOUT.
level = level or stdout_level
filelogger.setLevel(level)
log.addHandler(filelogger)