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')
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