简体   繁体   中英

How to reinstall logging library for python 3?

I get an error while doing the simplest of logging in python 3. Am I doing anything wrong or is there a way of reinstalling the logging module?

I've updated from 3.6.5 to 3.7.3, but have the same problem. Been trying different logging levels but no luck there either.

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: ()

You should be using levelname in your format string, not 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")

Showing the file gives:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM