python-记录标准输出,但在每个条目上获得第二个空行 [英] python - logging stdout, but getting second blank line on each entry

查看:70
本文介绍了python-记录标准输出,但在每个条目上获得第二个空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux Ubuntu 12.04系统上.我一直在使用此代码将所有stdout和stderr以及其他INFO级别的日志记录到文件中.

I'm on a Linux Ubuntu 12.04 system. I have been using this code to log all stdout and stderr + additional logging on INFO level to a file..

class LogFile(object):
    def __init__(self, name=None):
        self.logger = logging.getLogger(name)

    def write(self, msg, level=logging.INFO):
        self.logger.log(level, msg)

    def flush(self):
        for handler in self.logger.handlers:
            handler.flush()

logging.basicConfig(level=logging.INFO, 
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%m-%d-%y %H:%M:%S',
                    filename='logging.log')
sys.stdout = LogFile('stdout')
sys.stderr = LogFile('stderr')

由于某种原因,每当我得到一个条目时,它总是后跟一个空行,这是我的日志的一小部分输出:

For some reason whenever I get an entry it's always followed by a blank line, here is a small output of my log:

08-09-12 09:52:54 stdout       INFO     CheckCon: Checking Portal access.
08-09-12 09:52:54 stdout       INFO     

08-09-12 09:52:54 stdout       INFO     CheckCon: Portal ping successful.
08-09-12 09:52:54 stdout       INFO     

08-09-12 09:53:08 stderr       INFO     Bottle server starting up (using PasteServer())...

08-09-12 09:53:08 stderr       INFO     Listening on http://0.0.0.0:8654/

08-09-12 09:53:08 stderr       INFO     Hit Ctrl-C to quit.


08-09-12 09:53:08 stdout       INFO     URI: Generated https://*****
08-09-12 09:53:08 stdout       INFO     

08-09-12 09:53:08 stdout       INFO     CheckCon: Checking Portal access.
08-09-12 09:53:08 stdout       INFO     

08-09-12 09:53:08 stdout       INFO     serving on 0.0.0.0:8654 view at http://127.0.0.1:8654
08-09-12 09:53:08 stdout       INFO     

08-09-12 09:53:08 stdout       INFO     CheckCon: Google ping successful.
08-09-12 09:53:08 stdout       INFO     

这是它在文件中的外观,先是空的标准输出行,然后是整个空白行.如果您发现瓶子服务器的输出似乎没有空行,但每行之间仍然有空行.

This is how it looks in the file, with both an empty stdout line and then followed by an entire blank line. If you notice the output from the bottle server seems to not have the empty line but still a blank line between each line.

任何人都知道是什么原因引起的,或者我如何预防呢?

Anyone know what is causing this or how I can prevent it ?

从建议中,我已将所有打印内容更改为logging.info,但仍然存在瓶装服务器正在执行常规打印的问题:\

From suggestions I have changed all my print to logging.info, I still have the problem that bottle and paste server is doing regular prints :\

所以我的日志现在具有修改后的格式 format ='%(asctime)s%(levelname)-4s:%(message)s':

so my log now with modified format format='%(asctime)s %(levelname)-4s: %(message)s':

08-09-12 12:44:40 INFO: URI: Generated https://****
08-09-12 12:44:40 INFO: CheckCon: Checking Portal access.
08-09-12 12:44:40 INFO: serving on 0.0.0.0:9002 view at http://127.0.0.1:9002
08-09-12 12:44:40 INFO: 

08-09-12 12:44:40 INFO: CheckCon: Google ping successful.
08-09-12 12:44:40 INFO: FullW: opening url: ****
08-09-12 12:44:40 INFO: FullW: showing.
08-09-12 12:44:40 INFO: LOOP: starting.

从我可以看到的其他空行还因为信息行空,所以现在就更近了.

From what I can see the additional empty line is also because of the empty info line, so getting closer now..

编辑以澄清输出:有趣的一点:

Edit to clarify output: The interesting bit:

+++08-09-12 13:01:04 stdout       INFO     serving on 0.0.0.0:9002 view at     http://127.0.0.1:9002+++
+++08-09-12 13:01:04 stdout       INFO     
+++

最终

更改了我的写入内容,以检查msg的长度是否小于2个字符,并且消除了空白行.虽然仍然不能100%地确定原因.

Changed my write to check if the length of the msg is under 2 chars, and that eliminated the blank lines.. still not 100% sure of the reason though.

def write(self, msg, level=logging.INFO):
    if len(msg) < 2:
        pass
    else:
        self.logger.log(level, msg)

推荐答案

这是由于 print 的工作方式引起的.

This is due to the way how print works.

我修改了您的示例,以便在 write()方法中获得 print(级别,味精).

I have modified your example so that I have a print (level, msg) in the write() method.

它向我显示以下内容:

>>> x=LogFile("foo")
>>> print >>x, "123\n321\n444", "321", "222",
(20, '123\n321\n444')
(20, ' ')
(20, '321')
(20, ' ')
(20, '222')
>>> print >>x, "123\n321\n444", "321", "222"
(20, ' ')
(20, '123\n321\n444')
(20, ' ')
(20, '321')
(20, ' ')
(20, '222')
(20, '\n')

并且如果每个调用都被转移到 logger 调用中,则会为每个调用获得一个条目.

And if each of this calls is transferred into a logger call, you get an entry for each of them.

为了解决这个问题,您可以实现一种缓冲,该缓冲仅在完整行和结束时调用 logger.log().

In order to cope with that, you could implement a kind of buffering which only calls logger.log() for complete lines and on closing.

这篇关于python-记录标准输出,但在每个条目上获得第二个空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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