Simple logging example doesn't work!

robinsiebler at gmail.com robinsiebler at gmail.com
Mon Oct 18 14:52:36 EDT 2010


Here is an example straight out of the help, and for some reason, it
is not working. I get the error messages in the log, but I do not get
the info messages in the console.

import datetime, logging

def main():
    timestamp = datetime.datetime.now().strftime("%Y%m%d-%I%M%S")
    #set up logging
    logfile = os.path.join('db_backup_' + timestamp + '.log')
    logging.basicConfig(filename=logfile,
                        level=logging.WARN,
                        format="%(asctime)s - %(levelname)s - %
(message)s",
                        datefmt="%H:%M:%S")
    # define a Handler which writes INFO messages or higher to the
sys.stderr
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    # set a format which is simpler 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)
    conlog = logging.getLogger('console')
    #logging is now configured
    conlog.info('In Main')
    logging.error('An error occured.')



More information about the Python-list mailing list