简体   繁体   English

Python 日志记录:更改日志消息级别

[英]Python logging: change log message level

I have an application that uses python logging.我有一个使用 python 日志记录的应用程序。 All INFO messages are printed to console and DEBUG are saved to a file.所有 INFO 消息都打印到控制台,并且 DEBUG 保存到一个文件中。 I use a library that is too verbose on INFO level, but I still want those messages in my log file .我使用的库在 INFO 级别上过于冗长,但我仍然希望在我的日志文件中包含这些消息。 How do I intercept log messages from the library and change their level from INFO to DEBUG?如何拦截来自库的日志消息并将其级别从 INFO 更改为 DEBUG?

If the library is one that you wrote (I suspect this is not the case) you can adjust the logging level with python's own logging system:如果库是您编写的(我怀疑不是这种情况),您可以使用 python 自己的日志记录系统调整日志记录级别:

https://docs.python.org/3/library/logging.html#logging-levels https://docs.python.org/3/library/logging.html#logging-levels

But, it is far more common (and not python language specific) to have to handle tons of crazy logs to debug some issue.但是,必须处理大量疯狂的日志来调试某些问题的情况更为常见(而不是特定于 Python 语言)。 Often you did not write the code or don't have time to alter a working library.通常您没有编写代码或没有时间更改工作库。 Logs are always cluttered with stuff you are not immediately interested in. So here is a work around to only see what you are interested in. One great way I have found to reduce the clutter is to filter the output of the tail command:日志总是堆满你不感兴趣的东西。所以这里有一个变通办法,只看到你感兴趣的东西。我发现减少混乱的一个好方法是过滤 tail 命令的输出:

tail -F ./my_apps.log | tail -F ./my_apps.log | grep -iHn "error" grep -iHn "错误"

the tail command shows the last part (the tail) of a log. tail 命令显示日志的最后一部分(尾部)。 The -F option tells it to aggressively try to follow the log (even if it is deleted and recreated). -F 选项告诉它积极尝试跟踪日志(即使它被删除和重新创建)。 The pipe "|"管道“|” joins the two commands.连接两个命令。

The grep command uses powerful regular expressions (learn them now!) to find patterns. grep 命令使用强大的正则表达式(立即学习!)来查找模式。 It does not have to be "error" it can be any pattern and/or wildcards and categories.它不一定是“错误”,它可以是任何模式和/或通配符和类别。

If you are working on video game AI, for instance, you could have two terminals tailing the same log:例如,如果您正在研究视频游戏 AI,您可能有两个终端跟踪相同的日志:

tail -F ./my_game.log | tail -F ./my_game.log | grep -iHn "monster1" and tail -F ./my_game.log | grep -iHn "monster1" 和 tail -F ./my_game.log | grep -iHn "human" grep -iHn "人类"

and see what each one is up to (so long as the code prefaces the two objects logging calls with the right string).并查看每个对象在做什么(只要代码以正确的字符串开头两个对象的日志记录调用)。 You want to write the logs to the same file so you can debug order of operation issues.您希望将日志写入同一文件,以便调试操作顺序问题。

tail and grep are unix tools but they have windows ports (GnuWin32 I think). tail 和 grep 是 unix 工具,但它们有 Windows 端口(我认为是 GnuWin32)。

you can also filter the console/terminal output:您还可以过滤控制台/终端输出:

python myapp.py | python myapp.py | grep -iHn "some_pattern" grep -iHn "some_pattern"

Something like this as per this post?this post这样的东西?

 import sys f = open('c:\\\\goat.txt', 'w') sys.stdout = f print "test"

This will redirect ALL print statements to the text file.这会将所有打印语句重定向到文本文件。 You can change back by using:您可以使用以下方法改回来:

 sys.stdout = sys.__stdout__

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

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