Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81451

PYTHON LOGGING with StreamHandler and FileHandler

Date 2014-11-24 17:08 +0530
Subject PYTHON LOGGING with StreamHandler and FileHandler
From Ganesh Pal <ganesh1pal@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.16245.1416829096.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

PYTHON LOGGING with StreamHandler and FileHandler Ganesh Pal <ganesh1pal@gmail.com> - 2014-11-24 17:08 +0530

csiph-web