Missing logging output in Python

G

gabor.a.halasz

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)
 
W

W. Matthew Wilson

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




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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top