Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: logging.streamhandler is not sending logs to stdout

Tags:

python

logging

I want to use StreamHandler logging handler of python. What i have tried is,

import logging
import sys
mylogger = logging.getLogger("mylogger")
h1 = logging.StreamHandler(stream=sys.stdout)
h1.setLevel(logging.DEBUG)

mylogger.addHandler(h1)

# now trying to log with the created logger
mylogger.debug("abcd") # <no output>
mylogger.info("abcd") # <no output>
mylogger.warn("abcd") # abcd

Am i missing something ? Or doing any wrong ? Why INFO and DEBUG level logs are not coming on STDOUT ?

like image 726
Aashish P Avatar asked Jul 08 '13 09:07

Aashish P


People also ask

How do you write a log message to stdout in Python?

Log to stdout With the logging. basicConfig() Function in Python. If we want to print our logs to the console window and write the logs inside a file, we can use the logging. basicConfig() function for this process.

How do I print logging info in Python?

Python - Print Logs in a File. If you want to print python logs in a file rather than on the console then we can do so using the basicConfig() method by providing filename and filemode as parameter. The format of the message can be specified by using format parameter in basicConfig() method.

How do I print logs from console?

You should use the console. log() method to print to console JavaScript. The JavaScript console log function is mainly used for code debugging as it makes the JavaScript print the output to the console. To open the browser console, right-click on the page and select Inspect, and then click Console.

Where does Python logger store logs?

That call does not store anything. It merely creates a logger object which can be bound and configured however you would like. All warnings and errors would be logged to the standard output (that's what basicConfig does), including the calls that Freebase makes.


1 Answers

You have to set the level of the logger, not only the level of the handler:

mylogger.setLevel(logging.DEBUG) 

Here is a nice graphic of the logging workflow, where you can see that either the logger and the handler check for the log level:

http://docs.python.org/2/howto/logging.html#logging-flow

The default logLevel is WARNING, so even if you set the level of your handler to DEBUG, the message will not get through, since your logger suppresses it (it's also by default WARNING).

By the way, you can do some basic formatting with Formatter:

import logging import sys  mylogger = logging.getLogger("mylogger")  formatter = logging.Formatter('[%(levelname)s] %(message)s')  handler = logging.StreamHandler(stream=sys.stdout) handler.setFormatter(formatter) handler.setLevel(logging.DEBUG)  mylogger.addHandler(handler) mylogger.setLevel(logging.DEBUG)  mylogger.debug("This is a debug message.") mylogger.info("Some info message.") mylogger.warning("A warning.") 

Will give you the output

[DEBUG] This is a debug message. [INFO] Some info message. [WARNING] A warning. 
like image 156
tamasgal Avatar answered Sep 19 '22 15:09

tamasgal