logger module : Question about log message format

Vinay Sajip vinay_sajip at yahoo.co.uk
Tue Sep 8 10:42:14 CEST 2009

jorma kala <jjkk73 <at> gmail.com> writes:

> Hi,I've created a logger like this:    
> LOG_FILENAME = 'test.txt'
> fh=logging.FileHandler(LOG_FILENAME,'w')
> logger1 = logging.getLogger('myLogger1')
> logger1.addHandler(fh)
> logger1.setLevel(logging.INFO)logger1.info('message from logger1')
> and was hoping to get log messages in this format in my log file:
> :INFO:myLogger1:message from logger1
> instead I just get a plain message like this:
> message from logger1
> Do you know why?
> Many thanks.

If you use basicConfig() to configure the logging system, you get a format
string of "%(levelname)s:%(name)s:%(message)s", which gives you output like you
say you're expecting. If you don't, the default format used is just
"%(message)s", which gives you only the message part.

The format used by basicConfig is available as BASIC_FORMAT; if you want that
format you can initialise a formatter and attach it to your handler:

f = logging.Formatter(logging.BASIC_FORMAT)

More information about the Python-list mailing list