Disable root logger output

I have the following code in a file called 'logger.py'

import logging format = '%(asctime)s - %(name)-30s - %(levelname)-8s - %(message)s' logging.basicConfig(level=logging.DEBUG, format=format) formatter = logging.Formatter(format) fh = logging.FileHandler('test.log') fh.setFormatter(formatter) fh.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setFormatter(formatter) ch.setLevel(logging.INFO) logging.getLogger().addHandler(fh) logging.getLogger().addHandler(ch) 

and in another file

 import logging import logger logger = logging.getLogger(__name__) logger.info('Staring Scheduler') 

and I get the following console output

 2014-07-14 22:27:10,915 - __main__ - INFO - Staring Scheduler 2014-07-14 22:27:10,915 - __main__ - INFO - Staring Scheduler 

I can not disable double output. I would like to use an additional stream handler to configure the log level printed on the console. In the future, I would also like to use RotatingFileHandler instead of a simple file handler.

Does anyone know how to achieve this while maintaining a simple registrar setup, as in the second file? I have a search, but no solution seems to do this.

UPDATE 1 (enabled)

logger.py file

 import logging logging.getLogger().setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(name)-30s - %(levelname)-8s - %(message)s') fh = logging.FileHandler('test.log') fh.setFormatter(formatter) fh.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setFormatter(formatter) ch.setLevel(logging.ERROR) logging.getLogger().addHandler(fh) logging.getLogger().addHandler(ch) 

File test.py

 import logging import logger logger = logging.getLogger(__name__) logger.debug('Debug message') logger.info('Info message') logger.warning('Warning message') logger.error('Error message') logger.critical('Critical message') 

Console output:

 2014-07-15 09:47:58,171 - __main__ - ERROR - Error message 2014-07-15 09:47:58,171 - __main__ - CRITICAL - Critical message 

Test.log :

 2014-07-15 09:47:58,171 - __main__ - DEBUG - Debug message 2014-07-15 09:47:58,171 - __main__ - INFO - Info message 2014-07-15 09:47:58,171 - __main__ - WARNING - Warning message 2014-07-15 09:47:58,171 - __main__ - ERROR - Error message 2014-07-15 09:47:58,171 - __main__ - CRITICAL - Critical message 
+8
python logging
source share
3 answers

The reason you see double output is because you created two StreamHandlers in your first file; logger.py

This line explicitly states:

 ch = logging.StreamHandler() 

another in this line:

 logging.basicConfig(level=logging.DEBUG, 

As per the docs for logging.basicConfig :

Does it have a basic configuration for a logging system by creating a StreamHandler with a standard Formatter and adding it to the root log.

So you should remove the basicConfig line.

However, after removing it, you need to set the level for the root logger to DEBUG, since you do this in the basicConfig line:

 logging.getLogger().setLevel(logging.DEBUG) 
+5
source share

So, you can start by resetting your base Config, which already adds StreamHandler to your root syslogger, so you actually have two StreamHandlers:

 logging.getLogger().addHandler(fh) logging.getLogger().addHandler(ch) print logging.getLogger().handlers [<logging.StreamHandler object at 0x7f3f57c4d050>, <logging.FileHandler object at 0x7f3f57c091d0>, <logging.StreamHandler object at 0x7f3f57c09250>] 

Then you need to do what basicConfig did for you:

 logging.getLogger().setLevel(logging.INFO) 
0
source share

You call logging.basicConfig() , the default is StreamHandler . Instead, you must set the level manually.

 >>> import logging >>> format = '%(asctime)s - %(name)-30s - %(levelname)-8s - %(message)s' >>> formatter = logging.Formatter(format) >>> fh = logging.FileHandler('test.log') >>> fh.setFormatter(formatter) >>> fh.setLevel(logging.DEBUG) >>> ch = logging.StreamHandler() >>> ch.setFormatter(formatter) >>> ch.setLevel(logging.INFO) >>> logging.getLogger().addHandler(fh) >>> logging.getLogger().addHandler(ch) >>> l = logging.getLogger(__name__) >>> l.setLevel(logging.INFO) >>> l.info("HI") 2014-07-15 08:46:50,000 - __main__ - INFO - HI 

File contents:

 msvalkon@lunkwill:~$ cat test.log 2014-07-15 08:46:50,000 - __main__ - INFO - HI 

In addition, you are rewriting your logger . In another file :

 import logging import logger # <-- importing logger.py logger = logging.getLogger(__name__) # <-- overwriting the logger name, nothing from logger.py is now availabe logger.info('Staring Scheduler') # This is just a logging object 

Logging configuration is done in this case, as the import will go through your code. In some other cases this may not be true. Correct by changing the variable name or by completely changing the configuration scheme.

0
source share

All Articles