selenium-logging

log

import logging

hostname =hos00000

logging.basicConfig(
level=logging.DEBUG,
filename='D:\selenium3\log3/test.log',
filemode='a',
format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
)
logging.debug('debug')
logging.info('info')
logging.warning('waring')
logging.error('error')
logging.critical('critical')

結果:
2022-02-18 16:38:36,368 - D:/selenium3/test.py[line:10] - DEBUG: debug
2022-02-18 16:38:36,368 - D:/selenium3/test.py[line:11] - INFO: info
2022-02-18 16:38:36,368 - D:/selenium3/test.py[line:12] - WARNING: waring
2022-02-18 16:38:36,368 - D:/selenium3/test.py[line:13] - ERROR: error
2022-02-18 16:38:36,368 - D:/selenium3/test.py[line:14] - CRITICAL: critical

logging模块常用format格式说明
%(levelno)s: 打印日志级别的数值
%(levelname)s: 打印日志级别名称
%(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
%(filename)s: 打印当前执行程序名,python如:login.py
%(funcName)s: 打印日志的当前函数
%(lineno)d: 打印日志的当前行号,在第几行打印的日志
%(asctime)s: 打印日志的时间
%(thread)d: 打印线程ID
%(threadName)s: 打印线程名称
%(process)d: 打印进程ID
%(message)s: 打印日志信息
使用前理解:

logging.basicConfig函数各参数:
filename: 指定日志文件名,如my.log 或my.txt
filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
format: 指定输出的格式和内容,format可以输出很多有用信息,如下例所示:
datefmt: 指定时间格式,同time.strftime()
level: 设置日志级别,默认为logging.WARNING
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略
logging中使用format:
例1:指定文件测试
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='myapp.txt',
filemode='w')
logging.debug("这是一个debug")

logging.basicConfig(
level=logging.DEBUG,
filename='D:\IBM\selenium3\log3/test.log',
filemode='a',
format='%(asctime)s %(filename)s %(levelname)s %(message)s',
datefmt='%Y/%m/%d %H:%M:%S'
)

logging.debug('debug')
logging.info('info')
logging.warning('waring')
logging.error('error')
logging.critical('critical')

posted @   z20240722  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示