如何自定义Python日志记录的时间格式?
我对 Python 的日志记录包很陌生,并计划将其用于我的项目。我想根据自己的喜好自定义时间格式。这是我从教程中复制的简短代码:
import logging
# create logger
logger = logging.getLogger("logging_tryout2")
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s")
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
# "application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")
这是输出:
2010-07-10 10:46:28,811;DEBUG;debug message
2010-07-10 10:46:28,812;INFO;info message
2010-07-10 10:46:28,812;WARNING;warn message
2010-07-10 10:46:28,812;ERROR;error message
2010-07-10 10:46:28,813;CRITICAL;critical message
我想将时间格式缩短为:“2010-07-10 10:46:28
”,删除毫秒后缀。我查看了 Formatter.formatTime,但我很困惑。
I am new to Python's logging package and plan to use it for my project. I would like to customize the time format to my taste. Here is a short code I copied from a tutorial:
import logging
# create logger
logger = logging.getLogger("logging_tryout2")
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s")
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
# "application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")
And here is the output:
2010-07-10 10:46:28,811;DEBUG;debug message
2010-07-10 10:46:28,812;INFO;info message
2010-07-10 10:46:28,812;WARNING;warn message
2010-07-10 10:46:28,812;ERROR;error message
2010-07-10 10:46:28,813;CRITICAL;critical message
I would like to shorten the time format to just: '2010-07-10 10:46:28
', dropping the mili-second suffix. I looked at the Formatter.formatTime, but I am confused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自关于 Formatter 类的官方文档:
所以
改为
From the official documentation regarding the Formatter class:
So change
to
使用
logging.basicConfig
,以下示例适用于我:这允许您格式化 &全部配置在一行中。生成的日志记录如下所示:
Using
logging.basicConfig
, the following example works for me:This allows you to format & config all in one line. A resulting log record looks as follows:
要添加到其他答案,这里是 变量列表来自 Python 文档。
To add to the other answers, here is the variable list from Python Documentation.
如果将logging.config.fileConfig与配置文件一起使用,请使用类似以下内容的内容:
if using logging.config.fileConfig with a configuration file use something like:
尝试这些格式:
格式 1:
格式 1 的输出:
格式 2:
< strong>格式 2 的输出:
Try These Formats:
Format 1:
Output of Format 1:
Format 2:
Output of Format 2:
为了在记录时自定义时间格式,我们可以创建一个记录器对象和一个文件处理程序。
In order to customize time format while logging we can create a logger object and and a fileHandler to it.