2

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!

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I had looked at the HOWTO and I couldn't find that specific information. I had not discovered the Cookbook, but I see the examples are very useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.