Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to logging debug level to file but only info level on screen in python

Tags:

python

logging

in my python project i have several lines that are logging with debug level and other with info level. Now, when i run my code i can set at the begging the log level to info or debug level and that will log to a file and on the screen only the info or both info/debug messages accordingly.

How can i achieve to set to log to file always debug level but on screen always info level?

Example (only a small part):

loggername = 'Main.Case.'
self.logger = logging.getLogger(loggername)
self.logger.info('case() - Started')
self.logger.debug('System ready...')
self.logger.debug('File ready...')
self.logger.debug('Memory ready...')
self.logger.info('case() - Ready')

This would print with info level only (on screen and log file):

Main.Case.case() - Started
Main.Case.case() - Ready

With debug level i will get (on screen and log file):

Main.Case.case() - Started
Main.Case.System ready...
Main.Case.File ready...
Main.Case.Memory ready...
Main.Case.case() - Ready

I actually would like to have on screen always only the info level lines and in the log file info/debug level!

When i activate debug level then i will get everything to both out puts (screen/file).

Any one any idea??

Thanks in adv.

like image 726
FotisK Avatar asked Jan 22 '26 06:01

FotisK


1 Answers

How to do this exact thing is documented in the logging cookbook in the Python docs.

like image 177
Vinay Sajip Avatar answered Jan 23 '26 19:01

Vinay Sajip