Python/如何获得具有绝对路径的logger?

2024-10-02 08:14:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用python的日志记录:

import logging

log = logging.getLogger(__name__)
logInstall = logging.getLogger('/var/log/install.log')

log.info("Everything works great")
logInstall.info("Why is not printed to the log file?")

在日志的日志文件中,每件事都正常工作并打印:“一切正常”

但在/var/log/install.log中,我什么也没看到。在

我做错了什么?在


Tags: installnameimportinfologisvarlogging
1条回答
网友
1楼 · 发布于 2024-10-02 08:14:16

您在哪里看到logging.getLogger()的参数是一个文件路径???它只是记录器对象的名称:

logging.getLogger([name])

Return a logger with the specified name or, if no name is specified, return a logger which is the root logger of the hierarchy. If specified, the name is typically a dot-separated hierarchical name like “a”, “a.b” or “a.b.c.d”. Choice of these names is entirely up to the developer who is using logging.

https://docs.python.org/2/library/logging.html#logging.getLogger

此记录器记录到的位置/方式(file、stderr、syslog、mail、network call等)取决于您如何为自己的应用程序配置日志记录—这里的要点是您将记录器的使用(在libs中)与记录器配置(在应用程序中)分离。在

请注意,通常的做法是使用__name__,这样您就可以得到一个模拟包/模块层次结构的记录器层次结构。在

相关问题 更多 >

    热门问题