简体   繁体   English

Python /如何获取具有绝对路径的记录器?

[英]Python / How to get logger with absolute path?

I try to use the logging of 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?")

In the log file of the log, every thing works and it prints: "Everything works great" 在日志的日志文件中,一切正常,并且打印:“一切正常”

But in /var/log/install.log , I don't see anything. 但是在/var/log/install.log ,我什么都看不到。

What I do wrong? 我做错了什么?

Where have you seen that the argument to logging.getLogger() was a file path ??? 您在哪里看到logging.getLogger()的参数是文件路径? It's only a name for your logger object: 它只是记录器对象的名称:

logging.getLogger([name]) logging.getLogger([名称])

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”, “ab” or “abcd”. 如果指定,则名称通常是点分隔的层次结构名称,例如“ a”,“ ab”或“ abcd”。 Choice of these names is entirely up to the developer who is using logging. 这些名称的选择完全取决于正在使用日志记录的开发人员。

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

Where / how this logger logs to (file, stderr, syslog, mail, network call, whatever) depends on how you configure the logging for your own app - the point here is that you decouple the logger's use (in the libs) from the logger config (in the app). 该记录器的记录位置/方式(文件,stderr,syslog,邮件,网络调用等)取决于您如何为自己的应用程序配置记录-这里的要点是您将记录器的使用(在libs中)与记录器配置(在应用程序中)。

Note that the usual practice is to use __name__ so you get a logger hierarchy that mimics the packages / modules hierarchy. 请注意,通常的做法是使用__name__这样您将获得一个类似于程序包/模块层次结构的记录器层次结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM