Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81451 > unrolled thread
| Started by | Ganesh Pal <ganesh1pal@gmail.com> |
|---|---|
| First post | 2014-11-24 17:08 +0530 |
| Last post | 2014-11-24 17:08 +0530 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
PYTHON LOGGING with StreamHandler and FileHandler Ganesh Pal <ganesh1pal@gmail.com> - 2014-11-24 17:08 +0530
| From | Ganesh Pal <ganesh1pal@gmail.com> |
|---|---|
| Date | 2014-11-24 17:08 +0530 |
| Subject | PYTHON LOGGING with StreamHandler and FileHandler |
| Message-ID | <mailman.16245.1416829096.18130.python-list@python.org> |
[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 top | Article view | comp.lang.python
csiph-web