3

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.

FotisK
  • 1,055
  • 2
  • 13
  • 28
  • RTM. The python [logging docs](https://docs.python.org/2/library/logging.html) show you how to create stream and file handlers. use `setLevel` to set the logger to `DEBUG` and the handlers to `INFO` or `DEBUG`. Besides the docs, there are lots of examples. – tdelaney Apr 20 '15 at 17:19
  • possible duplicate of [Printing to screen and writing to a file at the same time](http://stackoverflow.com/questions/9321741/printing-to-screen-and-writing-to-a-file-at-the-same-time) – Konstantin Apr 20 '15 at 17:20
  • See this possible duplicate: http://stackoverflow.com/questions/6652727/different-logging-levels-for-filehandler-and-display-in-python – supermitch Apr 20 '15 at 17:22
  • Thanks, next time i will search more! – FotisK Apr 20 '15 at 20:16

1 Answers1

1

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

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191