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,我正在尝试学习使用日志模块,但当我实际尝试使用Logger对象记录消息时,会不断收到类似的错误消息。我也无法理解错误信息。我做错了什么?顺便说一句,这些都是闲置的 >>> import logging >>> import sys >>> logger = logging.getLogger("name") >>> logger.setLevel(logging.DEBUG) >>> h

我正在尝试学习使用日志模块,但当我实际尝试使用Logger对象记录消息时,会不断收到类似的错误消息。我也无法理解错误信息。我做错了什么?顺便说一句,这些都是闲置的

>>> import logging
>>> import sys
>>> logger = logging.getLogger("name")
>>> logger.setLevel(logging.DEBUG)
>>> h = logging.StreamHandler(sys.stdout)
>>> h.setLevel(logging.DEBUG)
>>> logger.addHandler(h)
>>> h.setFormatter(logging.Formatter("%(asctime)s|%(level)s|%(message)s"))
>>> logger.debug("hey")
--- Logging error ---
Traceback (most recent call last):
  File "C:\Users\benle\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 1034, in emit
    msg = self.format(record)
  File "C:\Users\benle\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 880, in format
    return fmt.format(record)
  File "C:\Users\benle\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 622, in format
    s = self.formatMessage(record)
  File "C:\Users\benle\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 591, in formatMessage
    return self._style.format(record)
  File "C:\Users\benle\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 433, in format
    return self._fmt % record.__dict__
KeyError: 'level'
Call stack:
  File "<string>", line 1, in <module>
  File "C:\Users\benle\AppData\Local\Programs\Python\Python37\lib\idlelib\run.py", line 144, in main
    ret = method(*args, **kwargs)
  File "C:\Users\benle\AppData\Local\Programs\Python\Python37\lib\idlelib\run.py", line 474, in runcode
    exec(code, self.locals)
  File "<pyshell#10>", line 1, in <module>
Message: 'hey'
Arguments: ()
导入日志记录 >>>导入系统 >>>logger=logging.getLogger(“名称”) >>>logger.setLevel(logging.DEBUG) >>>h=logging.StreamHandler(sys.stdout) >>>h.setLevel(logging.DEBUG) >>>logger.addHandler(h) >>>h.setFormatter(logging.Formatter(“%(asctime)s |%(level)s |%(message)s”)) >>>调试(“嘿”) ---记录错误--- 回溯(最近一次呼叫最后一次): 文件“C:\Users\benle\AppData\Local\Programs\Python\37\lib\logging\\uuuu init\uuuu.py”,第1034行,在emit中 msg=self.format(记录) 文件“C:\Users\benle\AppData\Local\Programs\Python\37\lib\logging\\uuuu init\uuuu.py”,第880行,格式为 返回格式(记录) 文件“C:\Users\benle\AppData\Local\Programs\Python\37\lib\logging\\uuuu init\uuuu.py”,第622行,格式为 s=self.formatMessage(记录) formatMessage中第591行的文件“C:\Users\benle\AppData\Local\Programs\Python\Python37\lib\logging\ \uuuuu init\uuuu.py” 返回self.\u样式格式(记录) 文件“C:\Users\benle\AppData\Local\Programs\Python\37\lib\logging\\uuuu init\uuuu.py”,第433行,格式为 返回自记录的百分比。\u口述__ KeyError:“级别” 调用堆栈: 文件“”,第1行,在 文件“C:\Users\benle\AppData\Local\Programs\Python\Python37\lib\idlelib\run.py”,主目录第144行 ret=方法(*args,**kwargs) 运行代码中的文件“C:\Users\benle\AppData\Local\Programs\Python\Python37\lib\idlelib\run.py”,第474行 exec(代码,self.locals) 文件“”,第1行,在 信息:“嘿” 参数:()
根据中描述的日志记录属性(另请参见此):

问题出在
h.setFormatter(logging.Formatter(“%(asctime)s |%(level)s |%(message)s”)
(正如回溯中指出的那样(keyrerror:'level')

您应该使用
%(levelname)s
。例如:

h.setFormatter(logging.Formatter("%(asctime)s|%(levelname)s|%(message)s"))

根据中描述的日志记录属性(另请参见):

问题出在
h.setFormatter(logging.Formatter(“%(asctime)s |%(level)s |%(message)s”)
(正如回溯中指出的那样(keyrerror:'level')

您应该使用
%(levelname)s
。例如:

h.setFormatter(logging.Formatter("%(asctime)s|%(levelname)s|%(message)s"))

根据标准,我想应该是
%(levelname)s
。(python通常不会“无缘无故地”引发异常)。天哪,非常感谢您!我不敢相信这是那么简单的错误,不是问题!快乐的蟒蛇!根据标准,我想应该是
%(levelname)s
。(python通常不会“无缘无故地”引发异常)。天哪,非常感谢您!我不敢相信这是那么简单的错误,不是问题!快乐的蟒蛇!非常感谢。现在你能告诉我为什么我的SMTPHandler不能工作吗?我创建了一个SMTPHandler,如下所示:
mail\u handler=logging.handlers.SMTPHandler('smtp.gmail.com',“我的_email@gmail.com“,”我的_email@gmail.com“,”服务器死了。真糟糕…:(”,凭据=(“我的”_email@gmail.com“,“我的密码”))
并将其添加到日志记录程序中。当我使用info方法登录时,出现了一个巨大错误。你需要为此发布另一个问题。如何将其标记为已关闭?如果此答案解决了你的问题,请单击此答案左侧的右勾号接受答案。谢谢!是时候提出一个新问题了:谢谢。现在你能告诉我为什么吗我的SMTPHandler无法工作?我创建了一个类似这样的SMTPHandler:
mail\u handler=logging.handlers.SMTPHandler('smtp.gmail.com',“我的_email@gmail.com“,”我的_email@gmail.com“,”服务器死了。真糟糕…:(”,凭据=(“我的”_email@gmail.com“,“我的密码”))
并将其添加到日志记录程序中。当我使用info方法登录时,出现了一个巨大错误。您需要为此发布另一个问题。如何将其标记为已关闭?如果此答案解决了您的问题,请单击此答案左侧的右勾号接受答案。谢谢!是时候提出新问题了:p