Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40860 > unrolled thread

Missing logging output in Python

Started bygabor.a.halasz@gmail.com
First post2013-03-08 06:07 -0800
Last post2013-03-12 07:33 -0400
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Missing logging output in Python gabor.a.halasz@gmail.com - 2013-03-08 06:07 -0800
    Re: Missing logging output in Python "W. Matthew Wilson" <matt@tplus1.com> - 2013-03-12 07:33 -0400

#40860 — Missing logging output in Python

Fromgabor.a.halasz@gmail.com
Date2013-03-08 06:07 -0800
SubjectMissing logging output in Python
Message-ID<b570d558-706d-4d2d-af81-41c44e2a3a21@googlegroups.com>
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)

[toc] | [next] | [standalone]


#41115

From"W. Matthew Wilson" <matt@tplus1.com>
Date2013-03-12 07:33 -0400
Message-ID<mailman.3222.1363088028.2939.python-list@python.org>
In reply to#40860

[Multipart message — attachments visible in raw view] — view raw

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@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@tplus1.com
http://tplus1.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web