当前位置:   article > 正文

logging的使用python3_python2 logging

python2 logging
#以时间命名输出日志
def init_logging():
t = str(time.strftime("%Y%m%d_%H%M", time.localtime()))
filename = 'log_%s.txt' % t
logging.basicConfig(level=logging.DEBUG,
                    filename=filename,
                    ​filemode='w',
                    format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')

#实现日志按大小分割,

def init_log():
    handler = logging.handlers.RotatingFileHandler('log/test1.log', maxBytes=1 * 1024 * 1024,
                                                   backupCount=3)  # 按1M大小分割,共保留10个文件
    formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
    handler.setFormatter(formatter)
    my_logger = logging.getLogger('')
    my_logger.setLevel(logging.DEBUG)
    my_logger.addHandler(handler)

两种初始化的方法打印时候都是logging.info()

init_log()  #先初始化
logging.error("456")
logging.info("你好")
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/810462
推荐阅读
相关标签
  

闽ICP备14008679号