pythonhello换行输出方法_如何在python日志记录中插入换行符?

import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s', datefmt='%H:%M:%S')

logging.info('hello')

logging.warning('\n new hello')

11:15:01 INFO hello

11:16:49 WARNING

new hello

Because the log is crowded, I want to explicitly insert a newline before asctime and levelname. Is this possible without modifying format?

I looked into logging module and googled a bit and could not find a viable way.

解决方案

I have two solutions, the first is very easy, but the output is not very clean. The second method will produce the exact output you want, but it is a little more involved.

Method 1

To produce a blank line, just log an empty string with a new line:

import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s', datefmt='%H:%M:%S')

logging.info('hello')

logging.info('\n')

logging.warning('new hello')

The output will have an empty info line, which is not very clean:

16:07:26 INFO hello

16:07:26 INFO

16:07:26 WARNING new hello

Method 2

In this method, I created two different handlers. The console_handler which I use most of the time. When I need a new line, I switch to a second handler, blank_handler.

import logging

import types

def log_newline(self, how_many_lines=1):

# Switch handler, output a blank line

self.removeHandler(self.console_handler)

self.addHandler(self.blank_handler)

for i in range(how_many_lines):

self.info('')

# Switch back

self.removeHandler(self.blank_handler)

self.addHandler(self.console_handler)

def create_logger():

# Create a handler

console_handler = logging.StreamHandler()

console_handler.setLevel(logging.DEBUG)

console_handler.setFormatter(logging.Formatter(fmt="%(name)s %(levelname)-8s: %(message)s"))

# Create a "blank line" handler

blank_handler = logging.StreamHandler()

blank_handler.setLevel(logging.DEBUG)

blank_handler.setFormatter(logging.Formatter(fmt=''))

# Create a logger, with the previously-defined handler

logger = logging.getLogger('logging_test')

logger.setLevel(logging.DEBUG)

logger.addHandler(console_handler)

# Save some data and add a method to logger object

logger.console_handler = console_handler

logger.blank_handler = blank_handler

logger.newline = types.MethodType(log_newline, logger)

return logger

if __name__ == '__main__':

logger = create_logger()

logger.info('Start reading database')

logger.info('Updating records ...')

logger.newline()

logger.info('Finish updating records')

The output is what you want to see:

logging_test INFO : Start reading database

logging_test INFO : Updating records ...

logging_test INFO : Finish updating records

Discussion

If you can put up with the less-than-perfect output, method 1 is the way to go. It has the advantage of being simple, least amount of effort.

The second method does the job correctly, but it is a little involved. It creates two different handlers and switch them in order to achieve your goal.

Another disadvantage of using method 2 is you have to change your code by searching for logging and replacing them with logger. You must take care replacing only relevant parts and leave such text as logging.DEBUG in tact.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值