1.基本使用
配置logging基本的设置,然后在控制台输出日志,
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %
(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
logger.info("Start print log")
logger.debug("Do something")
logger.warning("Something maybe fail.")
logger.info("Finish")
运行时,控制台输出
2018-09-20 19:40:36,677 - __main__ - INFO - Start print log
2018-09-20 19:40:36,677 - __main__ - WARNING - Something maybe fail.
2018-09-20 19:40:36,677 - __main__ - INFO - Finish
2.将日志写入到文件
设置logging,创建一个FileHandle
r,并对输出消息的格式进行设置,将其添加到logger
,然后将日志写入到指定的文件中,
import logging
logger = logging.getLogger(__name__)
logger.setLevel(level = logging.INFO)
handler = logging.FileHandler("log.txt")
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info("Start print log")
logger.debug("Do something")
logger.warning("Something maybe fail.")
logger.info("Finish")
log.txt中日志数据为,
2018-09-20 19:44:05,901 - __main__ - INFO - Start print log
2018-09-20 19:44:05,901 - __main__ - WARNING - Something maybe fail.
2018-09-20 19:44:05,901 - __main__ - INFO - Finish
3.将日志同时输出到屏幕和日志文件2.2.2 将日志同时输出到屏幕和日志文件
logger中添加StreamHandler
,可以将日志输出到屏幕上
import logging
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
handler = logging.FileHandler("log.txt")
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logger.addHandler(handler)
logger.addHandler(console)
logger.info("Start print log")
logger.debug("Do something")
logger.warning("Something maybe fail.")
logger.info("Finish")
控制台中看到,
Start print log
Something maybe fail.
Finish
在log.txt文件中可以看到
2018-09-20 19:47:25,266 - __main__ - INFO - Start print log
2018-09-20 19:47:25,266 - __main__ - WARNING - Something maybe fail.
2018-09-20 19:47:25,266 - __main__ - INFO - Finish
4.多模块使用logging
主模块mainModule.py
,
import logging
import subModule
logger = logging.getLogger("mainModule")
logger.setLevel(level = logging.INFO)
handler = logging.FileHandler("log.txt")
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(formatter)
logger.addHandler(handler)
logger.addHandler(console)
logger.info("creating an instance of subModule.subModuleClass")
a = subModule.SubModuleClass()
logger.info("calling subModule.subModuleClass.doSomething")
a.doSomething()
logger.info("done with subModule.subModuleClass.doSomething")
logger.info("calling subModule.some_function")
subModule.som_function()
logger.info("done with subModule.some_function")
子模块subModule.py
import logging
module_logger = logging.getLogger("mainModule.sub")
class SubModuleClass(object):
def __init__(self):
self.logger = logging.getLogger("mainModule.sub.module")
self.logger.info("creating an instance in SubModuleClass")
def doSomething(self):
self.logger.info("do something in SubModule")
a = []
a.append(1)
self.logger.debug("list a = " + str(a))
self.logger.info("finish something in SubModuleClass")
def som_function():
module_logger.info("call function some_function")
执行之后,在控制和日志文件log.txt中输出,
2018-09-20 19:58:21,603 - mainModule - INFO - creating an instance of subModule.subModuleClass
2018-09-20 19:58:21,603 - mainModule.sub.module - INFO - creating an instance in SubModuleClass
2018-09-20 19:58:21,603 - mainModule - INFO - calling subModule.subModuleClass.doSomething
2018-09-20 19:58:21,603 - mainModule.sub.module - INFO - do something in SubModule
2018-09-20 19:58:21,603 - mainModule.sub.module - INFO - finish something in SubModuleClass
2018-09-20 19:58:21,603 - mainModule - INFO - done with subModule.subModuleClass.doSomething
2018-09-20 19:58:21,603 - mainModule - INFO - calling subModule.some_function
2018-09-20 19:58:21,603 - mainModule.sub - INFO - call function some_function
2018-09-20 19:58:21,603 - mainModule - INFO - done with subModule.some_function
5.通过YAML文件配置
通过YAML文件进行配置,比JSON看起来更加简介明了,
version: 1
disable_existing_loggers: False
formatters:
simple:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
info_file_handler:
class: logging.handlers.RotatingFileHandler
level: INFO
formatter: simple
filename: info.log
maxBytes: 10485760
backupCount: 20
encoding: utf8
error_file_handler:
class: logging.handlers.RotatingFileHandler
level: ERROR
formatter: simple
filename: errors.log
maxBytes: 10485760
backupCount: 20
encoding: utf8
loggers:
my_module:
level: ERROR
handlers: [info_file_handler]
propagate: no
root:
level: INFO
handlers: [console,info_file_handler,error_file_handler]
通过YAML加载配置文件,然后通过logging.dictConfig配置logging,
代码
import yaml
import logging.config
import os
def setup_logging(default_path="logging.yaml", default_level=logging.INFO, env_key="LOG_CFG"):
path = default_path
value = os.getenv(env_key, None)
print('value', value)
print(os.path.exists(path))
if value:
path = value
if os.path.exists(path):
with open(path, "r") as f:
config = yaml.load(f)
logging.config.dictConfig(config) # 配置logging
else:
logging.basicConfig(level=default_level)
def func():
logging.info("start func")
logging.info("exec func")
logging.info("end func")
if __name__ == "__main__":
setup_logging(default_path="logging.yaml")
func()
控制台
('value', None)
True
2018-09-20 20:08:57,523 - root - INFO - start func
2018-09-20 20:08:57,523 - root - INFO - exec func
2018-09-20 20:08:57,523 - root - INFO - end func
inf.log文件
2018-09-20 20:08:57,523 - root - INFO - start func
2018-09-20 20:08:57,523 - root - INFO - exec func
2018-09-20 20:08:57,523 - root - INFO - end func
6.日志回滚
使用RotatingFileHandler,可以实现日志回滚
import logging
from logging.handlers import RotatingFileHandler
logger = logging.getLogger(__name__)
logger.setLevel(level = logging.INFO)
#定义一个RotatingFileHandler,最多备份3个日志文件,每个日志文件最大1K
rHandler = RotatingFileHandler("log.txt",maxBytes = 1*1024,backupCount = 3)
rHandler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
rHandler.setFormatter(formatter)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(formatter)
logger.addHandler(rHandler)
logger.addHandler(console)
logger.info("Start print log")
logger.debug("Do something")
logger.warning("Something maybe fail.")
logger.info("Finish")
可以在工程目录中看到,备份的日志文件,
2016/10/09 19:36 732 log.txt
2016/10/09 19:36 967 log.txt.1
2016/10/09 19:36 985 log.txt.2
2016/10/09 19:36 976 log.txt.3