Geek Culture
Published in

Geek Culture

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.

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

Exploring Amazon Redshift Query Editor v2

Access AWS Services (like Rekognition) from a VPC Enabled Lambda

In Software Engineering, Knowledge Is Only The Beginning

Binary Trees Quiz 2

Guaranteed allocation of Crodo. How to get?

Stop Waiting for an Engineer and Automate It Yourself

Every Computer System is a State Machine — TLA+ series (1)

App Modernization Anecdotes — tales from large legacy app mod

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
alex_ber

alex_ber

Senior Software Engineer at Pursway

More from Medium

Special Python String Formatting in Logging and Unit Conversion

A journey from easy to robust python code

A Ready Reckoner To Upgrade From Python 2 to Python 3

Why You Should Use Python Data Classes