log4py日志工具包基于python自带logging进行的封装,默认配置了比较常用的日志输出,方便使用
pip3 install log4py
新建demo.py
from log4py import Logger
Logger.set_level("INFO")
log = Logger.get_logger(__name__)
log.info("hello log4py")
日志输出
python demo.py
2020-03-29 16:35:40 __main__.<module>(demo.py:30) INFO: hello log4py
类的日志
from log4py import Logger
Logger.set_level("INFO")
@Logger.class_logger()
class LogTest:
def __init__(self):
self.logger.info("hello class logger")
LogTest()
日志输出
python demo.py
2020-03-29 16:40:17 __main__.LogTest.__init__(demo.py:32) INFO: hello class logger
定制日志
from log4py import Logger
config = {
"handlers": {"file_handler": {"class": "logging.FileHandler", 'filename': 'demo.log'}},
"loggers": {'__main__': {"level": "INFO", "handlers": ["file_handler"], 'propagate': False}}
}
Logger.configure(**config)
log = Logger.get_logger(__name__)
log.info("hello logger")
注意:Logger.configure 方法是更新形式写入log4py模块的默认配置
完全覆盖log4py模块的默认配置可以使用下面两个方法
Logger.dict_config(dict_config) # 同logging.config.dictConfig
Logger.file_config(“logger.conf”) # 同logging.config.fileConfig