在Debian系统中,Python日志记录可以通过内置的logging
模块来实现。以下是一个简单的示例,展示了如何设置Python日志记录:
logging
模块:import logging
DEBUG
,并将日志输出到控制台和文件中:logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s [%(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[
logging.FileHandler('app.log'),
logging.StreamHandler()
])
在这个例子中,我们使用了两个处理器(handler):
FileHandler
:将日志写入到名为app.log
的文件中。StreamHandler
:将日志输出到控制台。你可以根据需要添加更多的处理器,例如将日志发送到远程服务器或将其记录到数据库中。
logging
模块记录日志:logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
这些日志消息将根据之前的配置输出到控制台和app.log
文件中。
注意:在实际应用中,你可能需要根据实际需求调整日志级别、格式和处理器的配置。