Logger¶
Actualy python logger logs per default to sdterr console not not like i expect.
debug, info, warnings etc to strout and error messages to stderr.
This produce some issues by collect logs with logging tools. All logs ar in error console that not good.
Pyre python logger to log in stdout and stderr
Note
pyramid logger is different because gunicorn logs
import logging, sys, os
class StdLogFilter(logging.Filter):
def filter(self, rec: logging.LogRecord):
"""Filter logs for handler"""
return rec.levelno in (logging.DEBUG, logging.INFO, logging.WARNING)
formater = logging.Formatter('%(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s')
handler_1 = logging.StreamHandler(sys.stdout)
handler_1.setFormatter(formater)
handler_1.setLevel(logging.INFO)
handler_1.addFilter(StdLogFilter())
handler_2 = logging.StreamHandler()
handler_2.setLevel(logging.ERROR)
handler_2.setFormatter(formater)
log = logging.getLogger()
# logging.basicConfig(encoding="utf-8")
# add handlers to root log
log.addHandler(handler_1)
log.addHandler(handler_2)
log.setLevel(os.environ.get("LOGLEVEL", "INFO"))
# test logger writes to correct std
log.info("Client Startup: Test logger INFO") # stdout
log.error("Client Startup: Test logger ERROR") # stderr
log.warning("Client Startup: Test logger WARNING") # stderr
You can verify the loggers like
import logging
loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
for l in loggers:
print(l.name)
import logging_tree
logging_tree.printout()
Colorize the stdout see Colorize stdout / stderr
Some sources:
see also https://github.com/rokcarl/gunicorn_logging/tree/1.0 (Specialy for gunicorn it works) and here https://github.com/benoitc/gunicorn/issues/2453 more or less the same.
For formatter see https://docs.python.org/3/library/logging.html
Also found somekind usefull https://stackoverflow.com/questions/13733552/logger-configuration-to-log-to-file-and-print-to-stdout the answer.
Now it should works on the hosting envirment correctly.