在单独的行上显示Python记录器消息 [英] Python Logger Message Displayed on Separate Line

查看:47
本文介绍了在单独的行上显示Python记录器消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在正在处理的Python应用程序中格式化记录器输出时遇到了一些麻烦.目前,我正在以独立文件创建记录器,并将其存储在这样的类变量中……

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

然后我继续在用法上引用logger类变量,如下所示:

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...

我还尝试过将message属性显式添加到格式化字符串中,但这导致消息被打印两次:一次在格式化的第一行,第二次不进行格式化.

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.

我是Python日志记录的新手,看到其他用户确定他们的问题是由附加的多个处理程序引起的.但是,我不认为这是对的,因为我试图声明一次记录器并在类之间共享同一实例.如果确实是这个问题,我很抱歉提出一个重复的问题,并希望您能提供一些指导.

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.

*侧面说明:我在不同线程上运行的进程中使用了相同的记录器,但是我已经阅读了一些有关python中的日志记录实现的信息,这似乎是线程安全的.

*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.

推荐答案

您不需要添加其他StreamHandler,因为logging.basicConfig会将StreamHandler添加到根记录器. https://docs.python.org/2/library/logging.html#logging.basicConfig

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')

应该记录一行

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

这篇关于在单独的行上显示Python记录器消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆