簡體   English   中英

如何重新安裝python 3的日志庫?

[英]How to reinstall logging library for python 3?

我在做最簡單的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: ()

您應該在格式字符串中使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM