Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging: how to represent newlines in the format string in a logging config file?

I'm configuring my Python logging from a file (see http://www.python.org/doc//current/library/logging.html#configuration-file-format ).

From the example on that page, i have a formatter in the config file that looks like:

[formatter_form01]
format=F1 %(asctime)s %(levelname)s %(message)s
datefmt=
class=logging.Formatter

How do i put a newline in the "format" string that specifies the formatter? Neither \n nor \\n work (e.g. format=F1\n%(asctime)s %(levelname)s %(message)s does not work). Thanks

like image 797
bshanks Avatar asked Jan 04 '12 23:01

bshanks


People also ask

How do you show logging information in Python?

Python Logging – INFO Level To log an INFO line using Python Logging, Check if the logger has atleast a logging level of INFO. Use logging.info() method, with the message passed as argument, to print the INFO line to the console or log file.


2 Answers

The logging.config module reads config files with ConfigParser, which has support for multiline values.

So you can specify your format string like this:

[formatter_form01]
format=F1
    %(asctime)s %(levelname)s %(message)s
datefmt=
class=logging.Formatter

Multilines values are continued by indenting the following lines (one or more spaces or tabs count as an indent).

like image 78
ekhumoro Avatar answered Sep 21 '22 13:09

ekhumoro


The logging configuration file is based on the ConfigParser module. There you'll find you can solve it like this:

[formatter_form01]
format=F1
   %(asctime)s %(levelname)s %(message)s
datefmt=
class=logging.Formatter
like image 38
Rob Wouters Avatar answered Sep 21 '22 13:09

Rob Wouters