首先的概念是日志等级logging level。在logging中有如下几个日志等级,同时每个日志等级对应一个整数:NOTSET=0,DEBUG=10,
INFO=20,WARN=30,ERROR=40, and
CRITICAL=50。注意这里的NOTSET,严格来说它并不是一个日志等级,它的作用与真实场景有关,实际起作用的日志等级与它的父日志器有关。
defget_logger(logger_name, logging_level=logging.DEBUG, logging_path=LOG_FILE, propagate=True): logger = logging.getLogger(logger_name) logger.setLevel(logging_level) # better to have too much log than not enough logger.addHandler(get_console_handler()) logger.addHandler(get_file_handler(logging_path)) logger.propagate = propagate return logger
2023-04-03 14:52:04,443 INFO in main.py,test_logging,line8 [MainProcess-89912-MainThread-4559111680][test-logger] test info 2023-04-03 14:52:04,443 DEBUG in main.py,test_logging,line9 [MainProcess-89912-MainThread-4559111680][test-logger] test debug 2023-04-03 14:52:04,443 WARNING in main.py,test_logging,line10 [MainProcess-89912-MainThread-4559111680][test-logger] test warning