商城首页欢迎来到中国正版软件门户

您的位置: 首页 > 文章列表 > 编程开发 > 如何配置Debian的Python日志系统

如何配置Debian的Python日志系统

  发布于2026-07-16 阅读(0)

扫一扫,手机访问

在Debian系统上捣鼓Python日志,说白了就是用好Python自带的logging模块。这玩意儿虽然内置,但要想配置得顺手,还是有几个关键点得拿捏住。下面这个指南,从零开始一步步来,帮你把日志系统安排得明明白白。

如何配置Debian的Python日志系统

1. 安装必要的软件包

先确认你的Debian系统里已经装好了Python和相关的日志库。其实大部分情况下,这些东西都是预装的,不过跑一遍更新总没错——毕竟谁也不想因为版本问题卡壳。

sudo apt update
sudo apt install python3 python3-pip

2. 创建一个Python脚本

新建一个脚本文件,比如叫logging_example.py。代码本身不复杂,核心就是basicConfig那一套——设置日志级别、格式、日期格式,然后拿个logger实例出来跑几个不同级别的消息看看效果。

import logging

# 配置日志记录器
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')

# 获取日志记录器实例
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')

3. 运行Python脚本

终端里直接跑起来,看看控制台输出是不是符合预期。

python3 logging_example.py

4. 配置日志文件

如果想把日志存到文件里,只要在basicConfig里加个filename参数就行。注意filemode='a'表示追加写入,不会覆盖之前的日志。

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    filename='app.log',  # 将日志写入文件
                    filemode='a')        # 追加模式

5. 使用不同的日志处理器

实际项目中,往往需要把不同级别的日志分流——比如错误日志单独存一个文件,普通日志在控制台实时输出。这时候就得用Handler来定制了。下面这个例子,错误日志写进error.log,所有日志同时在控制台打印。

import logging

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

# 创建文件处理器,用于写入错误日志
error_handler = logging.FileHandler('error.log')
error_handler.setLevel(logging.ERROR)
error_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
error_handler.setFormatter(error_formatter)

# 创建控制台处理器,用于输出所有日志
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(console_formatter)

# 将处理器添加到日志记录器
logger.addHandler(error_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')

6. 使用日志轮转

日志文件一直写下去,迟早会撑爆磁盘。用RotatingFileHandler可以实现按大小自动轮转,保留最近几个备份文件。比如设置每个文件最大10MB,保留5个备份,旧的自动删除。

import logging
from logging.handlers import RotatingFileHandler

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

# 创建文件处理器,用于写入所有日志,并进行轮转
file_handler = RotatingFileHandler('app.log', maxBytes=10*1024*1024, backupCount=5)
file_handler.setLevel(logging.DEBUG)
file_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(file_formatter)

# 将处理器添加到日志记录器
logger.addHandler(file_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')

以上几步走下来,一个基本的Python日志系统在Debian上就算搭好了。当然,实际场景中可能还需要配合日志格式的微调、多模块的日志链路追踪等等,但核心框架就是这些。根据需求往上加处理器、过滤器,灵活得很。

本文转载于:https://www.yisu.com/ask/60284212.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注