Как переустановить библиотеку журналов для Python 3? - PullRequest
0 голосов
/ 23 мая 2019

Я получаю сообщение об ошибке при выполнении самой простой регистрации в 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 голосов
/ 23 мая 2019

Вы должны использовать 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
...