Python 之logging
2020-12-13 03:03
标签:文件 logger erro handler ror basic 关系 div ica 参考视频:https://www.youtube.com/watch?v=dvtjV4Xmtj0 1.简单用法,不能同时输出到屏幕加文件。 可以用logging.basicConfig()配置。 # Demo https://rgb-24bit.github.io/blog/2018/python-logging.html 2.1 若要同时输出到屏幕加文件。需要用handler 处理 import os logger = logging.getLogger() # create instance 对象实例 fh = logging.FileHandler(‘logging_test.log‘) fh.setFormatter(fm) logger.addHandler(fh) logger.setLevel("DEBUG") # 默认为warning, 打印3行 logger =logger() 2.2. 基本用法如2.1. 一般可以封装为一个函数,返回logger ,直接调用。 3. Log 是树形关系,子log. 避免才坑,参考视频: 参考视频:https://www.youtube.com/watch?v=dvtjV4Xmtj0 Python 之logging 标签:文件 logger erro handler ror basic 关系 div ica 原文地址:https://www.cnblogs.com/ASAP/p/11066779.html
logging.basicConfig(level=logging.DEBUG, format=‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)
logging.warning(‘This will get logged‘)
import logging
ch = logging.StreamHandler()
fm=logging.Formatter("%(asctime)s %(message)s")
ch.setFormatter(fm)
logger.addHandler(ch)
logger.debug(‘debug‘)
logger.info("info")
logger.warning("warning")
logger.error("error")
logger.critical("critical")import os
import logging
‘‘‘
# Demo https://rgb-24bit.github.io/blog/2018/python-logging.html
logging.basicConfig(level=logging.DEBUG, format=‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)
logging.warning(‘This will get logged‘)
‘‘‘
def logger():
logger = logging.getLogger() # create instance
fh = logging.FileHandler(‘logging_test.log‘)
ch = logging.StreamHandler()
fm=logging.Formatter("%(asctime)s %(message)s")
fh.setFormatter(fm)
ch.setFormatter(fm)
logger.addHandler(fh)
logger.addHandler(ch)
logger.setLevel("DEBUG")
return logger
logger =logger()
logger.debug(‘debug‘)
logger.info("info")
logger.warning("warning")
logger.error("error")
logger.critical("critical")