Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging to stdout and log file

Tags:

python

logging

I am fairly new in python and starting to get into the logging module. I would like to have the message logged into a log file and outputting to the console. The code below prints out the message to console but how can I get all the message to be log in a file?

Logger object does not have a function call (basicConfig(filename=)) for logging to a file. How can I add this functionality?

Thanks for the help in advance.

import logging

# create logger
logger = logging.getLogger(_name_)
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
like image 721
Zen Avatar asked Nov 29 '16 05:11

Zen


People also ask

How do I log in to stdout in Python?

You need to use urllib3 logger and set the log level to DEBUG: python log = logging. getLogger('urllib3') log. setLevel(logging. DEBUG) To maximise the message you can get, set HTTPConnection.

How do I print a log in Python terminal?

Use logging Module to Print Log Message to a File in Python getLogger(name) method. There is a convention to use __name__ variable as the name of the logger. Once we have created a new logger, we should remember to log all our messages using the new logger.info() instead of the root's logging.info() method.


3 Answers

You just need to add another handler, like a logging.FileHandler

fh = logging.FileHandler(r'/path/to/log.txt')
logger.addHandler(fh)
like image 173
Brendan Abel Avatar answered Oct 17 '22 04:10

Brendan Abel


Expanding on @Brendan's answer.

Your logger currently outputs to the console using a StreamHandler.

By adding a FileHandler, you can log to a file.

Each handler instance can be customized to have its own format and logging level.

If you want to log using the same format, you will have to set the format on the new FileHandler as well.

fh = logging.FileHandler(r'/path/to/log.txt')
fh.setFormatter(formatter)
logger.addHandler(fh)

Read more: Python logging cookbook

like image 34
victorlin Avatar answered Oct 17 '22 02:10

victorlin


After having used Waterboy's code for simultaneous logging to console and to file (see this thread) over and over in multiple Python packages, I finally cast it into a tiny standalone Python package, which you can find here:

https://github.com/acschaefer/duallog

The code is well documented and easy to use. Simply download the .py file and include it in your project, or install the whole package via python setup.py install.

Using this package, your code would look like this:

# Set up logging to console and file.
import duallog
duallog.setup(logdir='my_logs')

# Generate log messages.
import logging    
logging.debug('debug message')
logging.info('info message')
logging.warn('warn message')
logging.error('error message')
logging.critical('critical message')
like image 1
Lexxer Avatar answered Oct 17 '22 04:10

Lexxer