Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging, StreamHandler and standard streams

Tags:

python

logging

I can't figure out how to log info-level messages to stdout, but everything else to stderr. I already read this http://docs.python.org/library/logging.html. Any suggestion?

like image 999
L1ker Avatar asked Sep 05 '09 12:09

L1ker


People also ask

What is logging StreamHandler in Python?

StreamHandler. The StreamHandler class, located in the core logging package, sends logging output to streams such as sys. stdout, sys. stderr or any file-like object (or, more precisely, any object which supports write() and flush() methods).

Is logging a standard library?

Python provides a logging system as a part of its standard library, so you can quickly add logging to your application.

Is logging a standard Python library?

Python comes with a logging module in the standard library that provides a flexible framework for emitting log messages from Python programs. This module is widely used by libraries and is the first go-to point for most developers when it comes to logging.

What is logging basic config?

The basicConfig configures the root logger. It does basic configuration for the logging system by creating a stream handler with a default formatter. The debug , info , warning , error and critical call basicConfig automatically if no handlers are defined for the root logger.


1 Answers

The following script, log1.py:

import logging, sys  class SingleLevelFilter(logging.Filter):     def __init__(self, passlevel, reject):         self.passlevel = passlevel         self.reject = reject      def filter(self, record):         if self.reject:             return (record.levelno != self.passlevel)         else:             return (record.levelno == self.passlevel)  h1 = logging.StreamHandler(sys.stdout) f1 = SingleLevelFilter(logging.INFO, False) h1.addFilter(f1) rootLogger = logging.getLogger() rootLogger.addHandler(h1) h2 = logging.StreamHandler(sys.stderr) f2 = SingleLevelFilter(logging.INFO, True) h2.addFilter(f2) rootLogger.addHandler(h2) logger = logging.getLogger("my.logger") logger.setLevel(logging.DEBUG) logger.debug("A DEBUG message") logger.info("An INFO message") logger.warning("A WARNING message") logger.error("An ERROR message") logger.critical("A CRITICAL message") 

when run, produces the following results.

 C:\temp>log1.py A DEBUG message An INFO message A WARNING message An ERROR message A CRITICAL message 

As you'd expect, since on a terminal sys.stdout and sys.stderr are the same. Now, let's redirect stdout to a file, tmp:

 C:\temp>log1.py >tmp A DEBUG message A WARNING message An ERROR message A CRITICAL message 

So the INFO message has not been printed to the terminal - but the messages directed to sys.stderr have been printed. Let's look at what's in tmp:

 C:\temp>type tmp An INFO message 

So that approach appears to do what you want.

like image 104
Vinay Sajip Avatar answered Oct 24 '22 17:10

Vinay Sajip