Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PYTHON LOGGING with StreamHandler and FileHandler

544 views
Skip to first unread message

Ganesh Pal

unread,
Nov 24, 2014, 6:38:34 AM11/24/14
to pytho...@python.org

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

dieter

unread,
Nov 25, 2014, 1:50:17 AM11/25/14
to pytho...@python.org
Ganesh Pal <ganes...@gmail.com> writes:

> 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

You read the handler related documentation of the logging module.
When I remember right, you will find there a method allowing you
to control which message levels are handled by the the respective handler.

Ganesh Pal

unread,
Nov 25, 2014, 8:30:22 AM11/25/14
to dieter, pytho...@python.org
Thanks for the hint ,  I was able to get the error messages on the console  by setting the StreamHandler level to WARNING .

It works for  me know  but    one  LAST  question , it might sound simple, but  Iam not able to understand the difference between 

- (a)   ch.setLevel(logging.WARNING)  and ch.setLevel(logging.ERROR) , both are giving the same output for the below program .

 

import logging
import sys

# StreamHandler

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

root = logging.getLogger()
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.WARNING)  =========================> (  even with ch.setLevel(logging.ERROR) I get the same output )
ch.setFormatter(formatter)
root.addHandler(ch)

# FileHandler

ch1  = logging.FileHandler('/var/tmp/myapp1.log')
ch1.setFormatter(formatter)
logger = logging.getLogger('myapp')
logger.setLevel(logging.DEBUG)
logger.addHandler(ch1)
logger.error('We have a problem')
logger.info('While this is just chatty')


# echo > /var/tmp/myapp1.log
# python final_logger.py
2014-11-25 13:16:35,772 - myapp - ERROR - We have a problem
# cat /var/tmp/myapp1.log

2014-11-25 13:16:35,772 - myapp - ERROR - We have a problem
2014-11-25 13:16:35,773 - myapp - INFO - While this is just chatty

Regards,
Ganesh




On Tue, Nov 25, 2014 at 12:17 PM, dieter <die...@handshake.de> wrote:

You read the handler related documentation of the logging module.
When I remember right, you will find there a method allowing you
to control which message levels are handled by the the respective handler.

Peter Otten

unread,
Nov 25, 2014, 10:07:00 AM11/25/14
to pytho...@python.org
Ganesh Pal wrote:

> Thanks for the hint , I was able to get the error messages on the console
> by setting the StreamHandler level to WARNING .
>
> It works for me know but one LAST question , it might sound simple,
> but Iam not able to understand the difference between
>
> - (a) ch.setLevel(logging.WARNING) and ch.setLevel(logging.ERROR) ,
> both are giving the same output for the below program .

Quoting
<https://docs.python.org/2.7/library/logging.html#logging.Logger.setLevel>

"""
Logger.setLevel(lvl)
Sets the threshold for this logger to lvl. Logging messages which are less
severe than lvl will be ignored.
"""

INFO is both below WARNING and ERROR. If you add

logger.warn("whatever")

to your code 'whatever' will disappear from your log when you switch the
level from WARNING to ERROR.
0 new messages