Ubuntu中Python日志怎么配置

在Ubuntu系统中配置Python日志记录可以通过Python的内置logging模块来实现。以下是一个基本的配置示例,展示了如何设置日志记录器、处理器和格式化器。步骤 1: 创建Python脚本首先,创建一个新的Python脚本文件,例如 log_example.py。import logging# 配置日志记录器logging.basicConfig(level=logging.DE

在Ubuntu系统中配置Python日志记录可以通过Python的内置logging模块来实现。以下是一个基本的配置示例,展示了如何设置日志记录器、处理器和格式化器。

步骤 1: 创建Python脚本

首先,创建一个新的Python脚本文件,例如 log_example.py

import logging

# 配置日志记录器
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    filename='app.log',
    filemode='a'  # 文件模式(追加)
)

# 获取日志记录器
logger = logging.getLogger(__name__)

# 记录不同级别的日志
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')

步骤 2: 运行Python脚本

在终端中运行你的Python脚本:

python3 log_example.py

运行脚本后,你会在当前目录下看到一个名为 app.log 的日志文件,其中包含了你记录的日志信息。

步骤 3: 进一步配置日志记录器

你可以进一步配置日志记录器,例如添加多个处理器(handler)或设置不同的日志级别。以下是一个更复杂的示例:

import logging

# 创建日志记录器
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)

# 创建文件处理器并设置级别
file_handler = logging.FileHandler('app.log')
file_handler.setLevel(logging.DEBUG)

# 创建控制台处理器并设置级别
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)

# 创建日志格式器
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# 将格式器添加到处理器
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)

# 将处理器添加到日志记录器
logger.addHandler(file_handler)
logger.addHandler(console_handler)

# 记录不同级别的日志
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')

使用配置文件配置日志

你还可以使用一个单独的配置文件来配置日志记录器。以下是一个名为 logging.conf 的配置文件示例:

[loggers]
keys=root

[handlers]
keys=consoleHandler,fileHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler,fileHandler

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=('app.log', 'a')

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S

在你的Python脚本中,使用 logging.config.fileConfig() 函数加载配置文件:

import logging
import logging.config

# 加载配置文件
logging.config.fileConfig('logging.conf')

# 获取记录器
logger = logging.getLogger(__name__)

# 使用记录器记录日志
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')

通过这些步骤,你可以在Ubuntu系统上配置和使用Python的日志记录功能。根据你的需求,你可以进一步自定义日志记录器的配置。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/1364908.html

(0)
派派
上一篇 2025-07-09
下一篇 2025-07-09

发表回复

登录后才能评论