My stdLogging Module
--
Description module at AlexBerUtils project
This is Thin adapter layer that redirects stdout/stderr (or any other stream-like object) to standard Python’s logger.
The source code you can found here. It is available as part of my AlexBerUtils s project.
You can install AlexBerUtils from PyPi:
python3 -m pip install -U alex-ber-utils
You can optionally install some dependencies, such as jinja2. The easiest way is to run:
python3 -m pip install alex-ber-utils[yml]
Note also that hiyapyco should be at least 0.4.16.
However, this is not strictly required.
See here for more details explanation on how to install.
High-Level Description
This modules is based upon https://github.com/fx-kirin/py-stdlogging/blob/master/stdlogging.py See also https://github.com/fx-kirin/py-stdlogging/pull/1. I have encountered on this module here https://stackoverflow.com/questions/47325506/making-python-loggers-log-all-stdout-and-stderr-messages . Quote: “But be careful to capture stdout because it’s very fragile”
I have some 3rd party module (namely webpy), that uses both stdout
and stderr
and not using standard Python’s logging mechanism. My application is actually runs inside docker, so I want to have the ability to redirect those output to the standard logging mechanism (this make me maximum flexibility — for example, I can have my logs both on stdout
and in log files, log file will be stored on the mounted volume).
stdLogging
module provides me exactly this ability. In my specific use case it is sufficient to redirect only stderr
. As I’ve said before I’m running inside Docker. I’m running using tty
. Because of https://unix.stackexchange.com/questions/616616/separate-stdout-and-stderr-for-docker-run it is sufficient only to do it for stderr
.
Usage example:
Configuration file (simplified):
Note: I’m using my init_app_conf module to parse this file. This is not required, you can initialize logging module as you wish.
If you want to adapt stdout
, you should write:
If you want to redirect output both from stdout
and from stderr
(and they’re distinct streams), you can have both lines in the code. Alternatively, you can redirect stderr
to stdout
at the program startup (this what Docker do, if you have -t
option in docker run
command).
You can also provide logger_level
that will be used to echo output. By default, it is logging.ERROR
.
Advanced usage:
You can specify you own adapter class. For example:
This specifies default adapter class. You can provide your own implementation. It can be str
as in the code above or class
. Your implementation can extends the default one or just implement the same protocol.
See also:
importer
module or How to write easily customizable code?fixabscwd()
function inmains
module or Making relative path to file to work.fix_retry_env()
function inmains
module or Make path to file on Windows works on Linux.- parser module or Description of one the oldest AlexBerUtils project
- ymlparsers module or Description of low-level API module for another modules
- init_app_conf module, or My major init_app_con module
- deploys module, or My deploys module
- emails module,or My emails module
- processinvokes module, or My processinvokes module
- stdLogging module, or My stdLogging Module