python写日志

复制代码
import logging
from loguru import logger
logging.basicConfig(filename='test_yan.log',#指定文件存放位置
                    level=logging.DEBUG,    #设置写入文件的日志的级别
                    format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s', #日志格式
                    datefmt='%Y-%m-%d %H:%M:%S', #时间格式
                    filemode='w'  #指定写入方式,默认为a-追加
                    )

#只输出日志到日志文件里
logging.debug('1-debug message')
logging.info('1-info messages')
logging.warning('1-warning messages')
logging.error('1-error message')
logging.critical('1-critical message')


logger=logging.getLogger('test')#logger:记录器,应用程序代码能直接使用的接口
logger.setLevel(logging.INFO)#设置logger的输出级别,debug<info<warning<error<critical
ch=logging.StreamHandler()#输出到终端
logger.addHandler(ch)#添加logger的输出位置
formatter=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)#配置输出到终端的日志的格式

#输出日志到终端和文件里
logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('criticalr message')
复制代码

 

posted @   小白成长记-yan  阅读(155)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示