Missing logging output in Python

W. Matthew Wilson matt at tplus1.com
Tue Mar 12 07:33:40 EDT 2013


I made that code into a program like this:

### BEGIN

import logging

def configure_logging():

    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s
%(name)-12s %(levelname)8s %(message)s',
                        datefmt='%Y-%m-%d\t%H:%M:%s',
                        filename='/tmp/logfun.log', filemode='a')

    # define a Handler that writes INFO messages to sys.stderr
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)

    # set format that is cleaber for console use
    formatter = logging.Formatter('%(name)-12s: %(levelname)-8s
%(message)s')

    # tell the handler to use this format
    console.setFormatter(formatter)

    # add the handler to the root logger
    logging.getLogger('').addHandler(console)

if __name__ == '__main__':

    configure_logging()

    logging.debug('a')
    logging.info('b')
    logging.warn('c')
    logging.error('d')
    logging.critical('e')

### END

and when I run the program, I get INFO and greater messages to stderr:

$ python logfun.py
root        : INFO     b
root        : WARNING  c
root        : ERROR    d
root        : CRITICAL e

and I get this stuff in the log file:

$ cat /tmp/logfun.log
2013-03-12 07:31:1363087862 root            DEBUG a
2013-03-12 07:31:1363087862 root             INFO b
2013-03-12 07:31:1363087862 root          WARNING c
2013-03-12 07:31:1363087862 root            ERROR d
2013-03-12 07:31:1363087862 root         CRITICAL e

In other words, your code works!  Maybe you should check permissions on the
file you are writing to.

Matt




On Fri, Mar 8, 2013 at 9:07 AM, <gabor.a.halasz at gmail.com> wrote:

> Hi,
>
> I would like to enable loggin in my script using the logging module that
> comes with Python 2.7.3.
>
> I have the following few lines setting up logging in my script, but for
> whatever reason  I don't seem to get any output to stdout  or to a file
> provided to the basicConfig method.
>
> Any ideas?
>
> # cinfiguring logging
> logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s
> %(levelname)8s %(message)s',
>                     datefmt='%Y-%m-%d\t%H:%M:%s',
> filename=config["currentLoop"], filemode='a')
> # define a Handler that writes INFO messages to sys.stderr
> console = logging.StreamHandler()
> console.setLevel(logging.INFO)
> # set format that is cleaber for console use
> formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
> # tell the handler to use this format
> console.setFormatter(formatter)
> # add the handler to the root logger
> logging.getLogger('').addHandler(console)
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
W. Matthew Wilson
matt at tplus1.com
http://tplus1.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130312/9e757e76/attachment.html>


More information about the Python-list mailing list