Python Logger Message Displayed on Separate Line

二次信任 提交于 2021-02-10 16:25:24

问题


I'm having a bit of trouble formatting my logger output in a Python application I'm working on. Currently, I'm creating my logger in a standalone file and storing it in a class variable like so...

    import logging

    class Logging():
        FORMAT = '[%(levelname)s]: %(asctime)-15s'
        logging.basicConfig(format=FORMAT)
        logger = logging.getLogger('Mariner')
        ch = logging.StreamHandler()
        logger.addHandler(ch)
        logger.setLevel(logging.INFO)

For all of my classes that need logging functionality, I simply import my logger class like so:

    import Logging from Logging

I then proceed to refer to the logger class variable on usage like so:

    Logging.logger.info("some message")

When logging messages appear in my console, they span two lines with the fist containing the formatted output and the second containing the plain text message. For example:

    [INFO]: 2017-07-16 19:28:47,888
    writing book state to mongo db...

I've also tried explicitly adding the message property to my formatting string but that results in the message being printed twice: once on the first line w/ formatting, and once on the second line without.

I'm new to Python logging and have seen other users determining their issues to be caused by multiple handlers being attached. However, I don't think this is true in my case as I'm attempting to declare my logger once and share the same instance across classes. If this is indeed the issue, I apologize for asking a duplicate question and would appreciate some kind direction.

*Side Note: I am making use of the same logger in processes running on separate threads, however I've read a bit about the logging implementation in python and it seems to be thread safe.


回答1:


You don't need to add another StreamHandler since logging.basicConfig will add a StreamHandler to the root logger. https://docs.python.org/2/library/logging.html#logging.basicConfig

Changing you code to the following should work.

FORMAT = '[%(levelname)s]: %(asctime)-15s %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
logger = logging.getLogger('Mariner')
logger.info('some message')

should log one line

[INFO]: 2017-07-16 23:35:49,721 some message



回答2:


You are in fact running into the "multiple handlers attached problem". You're hitting some kind of default handler with your first message and then the actual message is being printed out by the StreamHandler. If you remove don't add the StreamHandler to the logger and change the format string to '[%(levelname)s]: %(asctime)-15s', you should have logs printing on one line.

As an aside, I don't think putting your logger in a class is helpful. I think it would be better to initialize it once at startup and then have separate threads refer to it by name (or a wrapper method that returns the logger returned by getLogger. Perhaps that name could be stored in a constant.



来源:https://stackoverflow.com/questions/45134586/python-logger-message-displayed-on-separate-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!