Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python日志模块;有没有一种方法可以记录所有的事情?_Python_Logging - Fatal编程技术网

python日志模块;有没有一种方法可以记录所有的事情?

python日志模块;有没有一种方法可以记录所有的事情?,python,logging,Python,Logging,有没有一种方法可以记录所有的事情? 我想在一个文件中写的信息,警告,关键和错误,任何其他 这是因为记录调试也会让搜索变得疯狂 在这一刻,我正在做: import logging logging.basicConfig(filename='loggs.log', format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',level=logging.DEBUG) logging.info('S

有没有一种方法可以记录所有的事情? 我想在一个文件中写的信息,警告,关键和错误,任何其他

这是因为记录调试也会让搜索变得疯狂

在这一刻,我正在做:

import logging
logging.basicConfig(filename='loggs.log', format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',level=logging.DEBUG)
logging.info('Start Test API.')
以这种方式,我记录了一切,但我想限制它

在阅读了一些文档之后,我正在这样做:

LEVELS = {'info': logging.INFO,
          'warning': logging.WARNING,
          'error': logging.ERROR,
          'critical': logging.CRITICAL}

#define the loggs configuration
if len(sys.argv) > 1:
    level_name = sys.argv[1]
    level = LEVELS.get(level_name, logging.NOTSET)
    logging.basicConfig(filename='loggs.log', format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',level=level)
logging.info('Start Test API.')

所以我希望日志“启动测试API”,而不是我的文件是空的

您可以使用
setLevel
方法设置限制日志记录的级别。

而不是:

logging.basicConfig(filename='loggs.log', format='', datefmt='', level=logging.DEBUG)
你想要:

logging.basicConfig(filename='loggs.log', format='', datefmt='', level=logging.INFO)
见正式文件:

例如:

import logging
import sys

LEVELS = {'debug': logging.DEBUG,
          'info': logging.INFO,
          'warning': logging.WARNING,
          'error': logging.ERROR,
          'critical': logging.CRITICAL}

if len(sys.argv) > 1:
    level_name = sys.argv[1]
    level = LEVELS.get(level_name, logging.NOTSET)
    logging.basicConfig(level=level)

logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical error message')
产生:

$ python logging_level_example.py debug
DEBUG:root:This is a debug message
INFO:root:This is an info message
WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical error message

$ python logging_level_example.py info
INFO:root:This is an info message
WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical error message

你能给我举个例子吗?如果我设置“信息”,它将只设置信息,但我想要日志信息、错误、警告和关键信息,它将记录所有信息和以上信息。我已经用文档中的示例更新了我的答案。您链接到文档的2.6版本有什么原因吗?差不多4年前,支持就结束了。@KevinMGranger-它有一个很好的例子。但好的一点是,我将更改链接,但保留示例。我在回答中添加了我正在做的事情,因为在更改代码后,我的日志文件每次都是空的hi@alxwrd我刚刚看到您的更新,它工作正常:S非常感谢;)ps只是好奇,为什么我的问题有-3?有什么想法吗?我认为我的问题很好:(堆栈溢出问题是指一个特定的问题。如果你的问题得到了回答,你应该接受答案,任何新问题都应该单独提出。嗨@KevinMGranger,是的,这是真的,但我只是尝试答案,它对我不起作用,所以对我来说,它不是我问题的完全正确答案,因为它不起作用你调用程序了吗?它的第一个参数是“info”吗?在那里放一个
打印(“设置级别”,级别)
,看看它说了什么。所以第一个参数是info,这是我代码的第一行!当我试着打印级别时,我收到“name'level'未定义”,这样我就可以看到如果len(sys.argv)它永远不会进入>1:但我不知道为什么:SI打开了一个新文件,编写了相同的代码并执行;日志打印在屏幕上,但在相同的情况下没有写入该文件!