PYTHON LOGGING with StreamHandler and FileHandler
Ganesh Pal
ganesh1pal at gmail.com
Mon Nov 24 06:38:12 EST 2014
Hi Team,
The below program logs all the messages to the log file and displays the
same on the console .
Please suggest how do I ensure that error and warning log levels go ONLY to
stdout and/or stder
Code :
import logging
import sys
# StreamHandler
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -
%(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
# FileHandler
logger = logging.getLogger('myapp')
ch = logging.FileHandler('/var/tmp/myapp1.log')
logger.setLevel(logging.DEBUG)
logger.error('We have a problem')
logger.info('While this is just chatty')
print "error abort" ## should be displayed on screen with using
loggin.<something>
output :
# python modified_logger.py
2014-11-24 11:18:12,355 - myapp - ERROR - We have a problem
2014-11-24 11:18:12,356 - myapp - INFO - While this is just chatty
error abort
# echo > /var/tmp/myapp1.log
# python modified_logger.py
2014-11-24 11:18:45,872 - myapp - ERROR - We have a problem
2014-11-24 11:18:45,872 - myapp - INFO - While this is just chatty
error abort
Regards,
Ganesh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141124/52c549cf/attachment.html>
More information about the Python-list
mailing list