网络编程
位置:首页>> 网络编程>> Python编程>> Python常用模块logging——日志输出功能(示例代码)

Python常用模块logging——日志输出功能(示例代码)

作者:松鼠大帝  发布时间:2022-01-21 15:24:01 

标签:python,logging,模块

用途

logging模块是Python的内置模块,主要用于输出运行日志,可以灵活配置输出日志的各项信息。

基本使用方法


logging.basicConfig(level=logging.DEBUG,
         format='levelname:%(levelname)s filename: %(filename)s '
             'outputNumber: [%(lineno)d] thread: %(threadName)s output msg: %(message)s'
             ' - %(asctime)s', datefmt='[%d/%b/%Y %H:%M:%S]',
         filename='./loggmsg.log', filemode="a")


参数


日志一共分成5个等级,从低到高分别是:DEBUG ,INFO ,WARNING ,ERROR, CRITICAL。



%(levelno)s: 打印日志级别的数值

%(levelname)s: 打印日志级别名称

%(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]

%(filename)s: 打印当前执行程序名

%(funcName)s: 打印日志的当前函数

%(lineno)d: 打印日志的当前行号

%(asctime)s: 打印日志的时间

%(thread)d: 打印线程ID

%(threadName)s: 打印线程名称

%(process)d: 打印进程ID

%(message)s: 打印日志信息



调用




logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')

示例


import logging
logging.basicConfig(level=logging.DEBUG,
         format='levelname:%(levelname)s filename: %(filename)s '
             'outputNumber: [%(lineno)d] thread: %(threadName)s output msg: %(message)s'
             ' - %(asctime)s', datefmt='[%d/%b/%Y %H:%M:%S]',
         filename='./loggmsg.log', filemode="a")
logging.debug("Hello")

日志文件loggmsg.log

levelname:DEBUG filename: test.py outputNumber: [7]  thread: MainThread output msg:  Hello -

总结

以上所述是小编给大家介绍的Python常用模块logging——日志输出功能网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

来源:https://segmentfault.com/a/1190000021058201

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com