简体   繁体   English

python记录配置并访问所有方法

[英]python logging config and access to all methods

I have enabled logging to python code. 我已启用日志记录到python代码。

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s : %(message)s')


logging.info('Log this message')

However I am not able to use any of other logging methods. 但是,我无法使用其他任何日志记录方法。 Other methods don't print anything. 其他方法不打印任何内容。

logging.debug('This is to debug only')

Is this related to level=logging.INFO ? 这与level=logging.INFO吗? Which level should i set so it could work with all logging methods? 我应该设置哪个级别,使其可以与所有日志记录方法一起使用?

Thanks for help. 感谢帮助。

You should use level=logging.DEBUG to log everything. 您应该使用level=logging.DEBUG记录所有内容。

But logging.error should print the error message even with level=logging.INFO 但是即使使用level=logging.INFO logging.error应打印错误消息

DEBUG Detailed information, typically of interest only when diagnosing problems. 调试详细信息,通常仅在诊断问题时才有用。

INFO Confirmation that things are working as expected. INFO确认一切正常。

WARNING An indication that something unexpected happened, or indicative of some problem in the near future (eg 'disk space low'). 警告表示发生了意外情况,或在不久的将来出现了某些问题(例如“磁盘空间不足”)。 The software is still working as expected. 该软件仍按预期运行。

ERROR Due to a more serious problem, the software has not been able to perform some function. 错误由于存在更严重的问题,该软件无法执行某些功能。

CRITICAL A serious error, indicating that the program itself may be unable to continue running. 关键严重错误,表示程序本身可能无法继续运行。

The default level is WARNING, which means that only events of this level and above will be tracked, unless the logging package is configured to do otherwise. 默认级别为WARNING,这意味着将仅跟踪此级别及更高级别的事件,除非将日志记录程序包配置为执行其他操作。

when you set you level to info you must get info , warning , error and critical logs 当您将级别设置为信息时,您必须获取infowarningerrorcritical日志

If you want to log all of level set your level to logging.DEBUG 如果要记录所有级别,请将级别设置为logging.DEBUG

The python logging module have multiple level of configuration python日志记录模块具有多个配置级别

  • DEBUG DEBUG
  • INFO 信息
  • WARNING 警告
  • ERROR 错误
  • CRITICAL 危急

This logs that will be shown are in the sorted order shown above depending on what level you choose. 根据您选择的级别,将按照上面显示的排序顺序显示此日志。 It will show the selected stage and any other stage "levelled above" the selected stage. 它将显示所选阶段以及“在所选阶段之上”位于其上方的任何其他阶段”。

For example, if you choose INFO, the logs will show for all INFO/WARNING/ERROR logs except DEBUG. 例如,如果选择INFO,则将显示除DEBUG之外的所有INFO / WARNING / ERROR日志的日志。

This helps in the way that you can sort them by environment variables according to the environment you are currently in, such as you can set it to DEBUG when you are in Development mode, and then set it to WARNING when you are in Production mode. 这有助于您根据当前环境根据环境变量对它们进行排序,例如,在开发模式下可以将其设置为DEBUG,在生产模式下可以将其设置为WARNING。

You can read more about it in the link below 您可以在下面的链接中了解更多信息

Python Logging Module Python记录模块

And finally, to give a concrete answer to your question. 最后,给出您问题的具体答案。 You should set it to DEBUG to see all the logs. 您应该将其设置为DEBUG以查看所有日志。

Hopefully this gives you a better idea on what the python logging module is for! 希望这使您对python日志记录模块的用途有了更好的了解!

Cheers! 干杯! Max 马克斯

The whole point of logging.DEBUG level is so that your, or any code you use, can log debug messages - if you don't want debug messages set your level to something higher. logging.DEBUG级别的重点在于,您或您使用的任何代码都可以记录调试消息-如果您不希望调试消息将您的级别设置为更高的级别。

If you still want to print debug messages but want to suppress messages from some of the modules you're using, like the requests and urllib3 , set the level of their loggers to something higher than logging.DEBUG , eg: 如果您仍然想打印调试消息,但是想抑制来自您正在使用的某些模块的消息,例如requestsurllib3 ,请将其记录器的级别设置为高于logging.DEBUG ,例如:

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s : %(message)s')
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)

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

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