Question

I am using the logging library in python. I am using several logger objects to output the information. I got to the point where the output is too much, and I need to send it to a logfile instead of the console. I can't find a function that configures the output stream of the logger object, and the basicConf function doesn't seem to do the trick. This is what I have:

import logging # Debug logging framework
logging.basicConfig(filename='loggggggmeee.txt')
logger = logging.getLogger('simulation')
logger.setLevel(logging.INFO)

#--------------- etc-------------------#

logger.info('This is sent to the console')

Any ideas? Thanks!

Was it helpful?

Solution

Try to add a file location like here it will solve your problem:

import logging # Debug logging framework
logging.basicConfig(filename='c:\\loggggggmeee.txt')
logger = logging.getLogger('simulation')
logger.setLevel(logging.INFO)

#--------------- etc-------------------#

logger.info('This is sent to the console')

By the way the file without a specific location will be at the module library...

I really recommend you to read the Logging HOWTO It's very simple and useful.

Also the Cookbook has lots of useful examples.

Another good tip about nameing the loggers from the docs:

a good convention to use when naming loggers is to use a module-level logger, in each module which uses logging, named as follows:

logger = logging.getLogger(__name__)

This means that logger names track the package/module hierarchy, and it’s intuitively obvious where events are logged just from the logger name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top