python脚本-记录Python脚本的运行日志的方法

本文介绍Python中的logging模块,包括如何配置基本的日志记录设置、自定义日志格式以及将日志输出到文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python中有一个模块logging,可以直接记录日志# 日志级别

# CRITICAL 50

# ERROR 40

# WARNING 30

# INFO 20

# DEBUG 10

logging.basicConfig()函数中的具体参数:

filename: 指定的文件名创建FiledHandler,这样日志会被存储在指定的文件中;

filemode: 文件打开方式,在指定了filename时使用这个参数,默认值为“w”还可指定为“a”;

format: 指定handler使用的日志显示格式;

datefmt: 指定日期时间格式。,格式参考strftime时间格式化(下文)

level: 设置rootlogger的日志级别

stream: 用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。

若同时列出了filename和stream两个参数,则stream参数会被忽略。

format参数中可能用到的格式化信息:%(name)sLogger的名字

%(levelno)s数字形式的日志级别

%(levelname)s文本形式的日志级别

%(pathname)s调用日志输出函数的模块的完整路径名,可能没有

%(filename)s调用日志输出函数的模块的文件名

%(module)s调用日志输出函数的模块名

%(funcName)s调用日志输出函数的函数名

%(lineno)d调用日志输出函数的语句所在的代码行

%(created)f当前时间,用UNIX标准的表示时间的浮 点数表示

%(relativeCreated)d输出日志信息时的,自Logger创建以 来的毫秒数

%(asctime)s字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒

%(thread)d线程ID。可能没有

%(threadName)s线程名。可能没有

%(process)d进程ID。可能没有

%(message)s用户输出的消息

1、打印日志到标准输出中import logging

logging.debug("debug message")

logging.info("info message")

logging.warning("warning message")

可以看出默认情况下Python的logging模块将日志打印到了标准输出中,且只显示了大于等于WARNING级别的日志。默认的日志的格式为:

日志级别:Logger名称:用户输出消息

2. 将日志文件输入到文件中import os

logging.basicConfig(filename=os.path.join(os.getcwd(),"log.txt"),level=logging.DEBUG)

logging.debug("this is a message")

3. 自定义格式输出# -*-coding:utf-8-*-

import logging

def console_out(logFilename):

""""" Output log to file and console """

# Define a Handler and set a format which output to file

logging.basicConfig(

level=logging.DEBUG, # 定义输出到文件的log级别,大于此级别的都被输出

format="%(asctime)s %(filename)s : %(levelname)s %(message)s", # 定义输出log的格式

datefmt="%Y-%m-%d %A %H:%M:%S", # 时间

filename=logFilename, # log文件名

filemode="w") # 写入模式“w”或“a”

# Define a Handler and set a format which output to console

console = logging.StreamHandler() # 定义console handler

console.setLevel(logging.INFO) # 定义该handler级别

formatter = logging.Formatter("%(asctime)s %(filename)s : %(levelname)s %(message)s") # 定义该handler格式

console.setFormatter(formatter)

# Create an instance

logging.getLogger().addHandler(console) # 实例化添加handler

# Print information # 输出日志级别

logging.debug("logger debug message")

logging.info("logger info message")

logging.warning("logger warning message")

logging.error("logger error message")

logging.critical("logger critical message")

if __name__ == "__main__":

console_out("logging.log")

4. 自定义格式,输出日志文件import logging

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="/tmp/test.log",

filemode="w")

logging.debug("debug message")

logging.info("info message")

logging.warning("warning message")

logging.error("error message")

logging.critical("critical message")

如果本文对您有所帮助,请支持下本站哦!!!^_^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值