Search code examples
pythonloggingword-wrap

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?


Solution

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