如何重新安装python 3的日志记录库?

时间:2019-05-22 23:41:07

标签: python python-3.x debugging logging dependencies

在执行最简单的python 3登录时出现错误。我做错了什么吗?还是可以重新安装日志记录模块?

我已经从3.6.5更新到3.7.3,但是有同样的问题。一直在尝试不同的日志记录级别,但也没有运气。

In [2]: import logging as l
   ...:
   ...: #create and configure l
   ...: LOG_FORMAT="%(Levelname)s %(asctime)s - %(funcName)s - %(message)s"
   ...: l.basicConfig(filename="mailExtractor.log",
   ...:         format=LOG_FORMAT,
   ...:         level=l.DEBUG,
   ...:         filemode="w")
   ...:
   ...:

In [3]: l.info("hej")
--- Logging error ---
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 1034, in emit
    msg = self.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 880, in format
    return fmt.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 622, in format
    s = self.formatMessage(record)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 591, in formatMessage
    return self._style.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 433, in format
    return self._fmt % record.__dict__
KeyError: 'Levelname'
Call stack:
  File "/Library/Frameworks/Python.framework/Versions/3.7/bin/ipython", line 10, in <module>
    sys.exit(start_ipython())
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/__init__.py", line 125, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/terminal/ipapp.py", line 356, in start
    self.shell.mainloop()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/terminal/interactiveshell.py", line 498, in mainloop
    self.interact()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/terminal/interactiveshell.py", line 489, in interact
    self.run_cell(code, store_history=True)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2848, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2874, in _run_cell
    return runner(coro)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/core/async_helpers.py", line 67, in _pseudo_sync_runner
    coro.send(None)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3049, in run_cell_async
    interactivity=interactivity, compiler=compiler, result=result)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3220, in run_ast_nodes
    if (yield from self.run_code(code, result)):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3296, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-3-a98b50bae629>", line 1, in <module>
    l.info("hej")
Message: 'hej'
Arguments: ()

1 个答案:

答案 0 :(得分:0)

您应该在格式字符串中使用levelname,而不是Levelname

>>> import logging as l
>>> LOG_FORMAT="%(levelname)s %(asctime)s - %(funcName)s - %(message)s"
>>> l.basicConfig(filename="mailExtractor.log", 
                  format=LOG_FORMAT, level=l.DEBUG, filemode="w")
>>> l.info("hej")

显示文件会给出:

⚡ cat mailExtractor.log 
INFO 2019-05-22 20:00:23,465 - <module> - hej