简体   繁体   中英

Python: wrapping log file text in logging module

Is there a way to config an instance of the Python logging module to wrap the text it write to the log file?

Currently each logging.info() or warning() or whatever call write to only one line even if it's really long. This hurts readability. It would be nice to be able to set a number of characters after which the logger would wrap to the next line.

Running: Windows 7, Python 3.3

CORRECTION: really long entries to the logfile do get wrapped, but after what must be around 500-1,000 characters. Where is this set and can it be made lower?

I cant think of a good way to do this, and as far as I can tell it should never wraps the log line no matter how long it is.

In general logging to multiple lines should be taken care of when you call to log, but I consider it a mostly bad practice as it can make log analysing later a bit more difficult.

If you do something like this I would add a tab to the start of every line that you have wrapped so you can tell which lines are part of the same log message entry:

log.info("firstLine\t\nsecondLine\t\nthridLine")

The out put should look like:

INFO:firstLine
    secondLine
    thridLine

Another option to make it more automagical is to add something like this to the log lines:

X = "This is a String I want to log to many lines"
log.info("".join([X[i:i+10]+"\n\t" for i in xrange(0, len(X), 10)]))

The out put should look like:

INFO: This is a 
    String I w
    ant to log
     to many l
    ines

Just change the 10 in the for loop and the string size to the size you want.

I came here with the same question and ended up using Luke's method, however with using the build in python module textwrap . I liked text to be wrapped on whitespaces and right after the hyphens in hyphenated words, moreover I like the provision to add an indent for wrapped lines. So:

import textwrap
textwrap.fill(log_line, subsequent_indent = '\t\t\t\t')

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