Created
April 11, 2017 07:56
-
-
Save timss/8f03ae681256f21e25f8b0a16327c26c to your computer and use it in GitHub Desktop.
Python logging across multiple modules with custom handler/filter and splitting loglevels between stdout/stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import logging | |
import sys | |
import sublog | |
logger = logging.getLogger() | |
# http://stackoverflow.com/a/24956305/1076493 | |
# filter messages lower than level (exclusive) | |
class MaxLevelFilter(logging.Filter): | |
def __init__(self, level): | |
self.level = level | |
def filter(self, record): | |
return record.levelno < self.level | |
def main(): | |
# redirect messages to either stdout or stderr based on loglevel | |
# stdout < logging.WARNING <= stderr | |
formatter = logging.Formatter('%(asctime)s %(levelname)s [%(module)s]: %(message)s') | |
logging_out = logging.StreamHandler(sys.stdout) | |
logging_err = logging.StreamHandler(sys.stderr) | |
logging_out.setFormatter(formatter) | |
logging_err.setFormatter(formatter) | |
logging_out.addFilter(MaxLevelFilter(logging.WARNING)) | |
logging_out.setLevel(logging.DEBUG) | |
logging_err.setLevel(logging.WARNING) | |
# root logger, no __name__ as in submodules further down the hierarchy | |
global logger | |
logger.addHandler(logging_out) | |
logger.addHandler(logging_err) | |
logger.setLevel(logging.DEBUG) | |
logger.info("An INFO message from " + __name__) | |
logger.error("An ERROR message from " + __name__) | |
sublog.log() | |
sys.exit(1) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python3 log.py | |
2017-04-11 09:54:55,316 INFO [log]: An INFO message from __main__ | |
2017-04-11 09:54:55,316 ERROR [log]: An ERROR message from __main__ | |
2017-04-11 09:54:55,316 INFO [sublog]: An INFO message from sublog | |
2017-04-11 09:54:55,316 ERROR [sublog]: An ERROR message from + sublog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import logging | |
logger = logging.getLogger(__name__) | |
def log(): | |
logger.info("An INFO message from " + __name__) | |
logger.error("An ERROR message from + " + __name__) |
@MCilento93 Glad to hear it was useful for someone! :)
My pleasure.
Actually even without the global dichiaration it is working 🤗
this helped!
It saved my day!
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @timss
Thank you for your snippets. I turned around the net to understand why a so simple task I was unable to do.
My error was to assign the name name to the logger, without assigning it as global.
Now I endided up with:
main.py
module2.py
output