I was lazy to set a daily value by calling the logging library when writing each code, so I made it into a function type. But I set the level to DEBUG, but I don't know what's wrong with the message being printed above warning. Is there a problem with my code?
import logging
def pr_logger(log_name, stream_lvl, file_lvl):
logger = logging.getLogger(__name__)
# # logger.setLevel(eval('logging.{mutual_lvl}'))
formatter = logging.Formatter('[%(levelname)s] [%(asctime)s] [%(message)s]', datefmt='%Y-%m-%d %H:%M:%S')
stream_handler = logging.StreamHandler()
stream_handler.setLevel(eval(f'logging.{stream_lvl}')) # 'DEBUG'
stream_handler.setFormatter(formatter)
file_handler = logging.FileHandler(f'{log_name}.log') # dop
file_handler.setLevel(eval(f'logging.{file_lvl}')) # 'DEBUG'
file_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
logger.addHandler(file_handler)
return logger
a = pr_logger('dop', 'DEBUG', 'DEBUG') # stream, file handler set DEBUG level
a.debug('debug') #msgoutput x
a.info('info') #msgoutput x
a.warning('warning') #msgoutput o
a.error('error') #msgoutput o
a.critical('critical') #msgoutput o
>>> [WARNING] [2022-11-13 20:57:55] [warning]
>>> [ERROR] [2022-11-13 20:57:55] [error]
>>> [CRITICAL] [2022-11-13 20:57:55] [critical]
logger.setLevel(eval(f'logging.{stream_lvl}'))
You took out logger.setLevel again
If you try print(logger)
<Logger __main__ (WARNING)>
It comes out like this. I think the default is WARNING.
The higher level of logger or handler seems to be applied
© 2024 OneMinuteCode. All rights reserved.