Skip to main content

Small library for printing warnings and creating logs.

Project description

mylogging

Python versions PyPI version Language grade: Python Documentation Status License: MIT codecov

My python warn-logging module. Based on debug value prints and logs warnings and errors. It's automatically colorized. It log to console or it can log to file if configured.

Motivation for this project is to be able to have one very simple code base for logging and warning at once. You can use one code for logging apps running on server (developer see if problems) and the same code for info and warnings from running python code on computer in some developed library (user see when using code).

One code, two use cases.

Other reasons are to be able to recognise immediately if error is from my library or from some imported library. Library try to be the simplest for use as possible (much simplier than logging or logguru). Library have user friendly formatting.

Installation

Python >=3.6 (Python 2 is not supported).

Install just with

pip install mylogging

Examples

Example set_warnings

This will configure what warnings will be displayed.

If log to console, override warnings display globally!!! => It's used mostly for developing - debugging apps (do not use in set_warnings in python libraries that other imports - you could redefined his warnings filters).

If logging to file (good for web servers), warning levels are ignored!!! you don't have to call this function.

import mylogging
mylogging.set_warnings(debug=1)
  • ignore warnings: debug=0,
  • display warnings once: debug=1,
  • display warnings always: debug=2,
  • stop warnings as errors: debug=3

You can ignore some warnings just by pass list of ignored warnings (any part of warning message suffice) just add ignored_warnings=["invalid value encountered in sqrt", "another ignored..."] arg.

Example of warnings and logginng - info, warn, traceback

import mylogging

mylogging.set_warnings()

mylogging.warn('Hessian matrix copmputation failed for example', caption="RuntimeError on model x")

We can log / warn tracebacks from expected errors and continue runtime.

try:
    print(10 / 0)

except ZeroDivisionError:
    mylogging.traceback("Maybe try to use something different than 0.")

Info will not trigger warning, but just print to console (but follows the rule in set_warnings(debug)).

mylogging.info("I am interesting info")

Logging to file

If you want to log to file, it's very simple just edit 'TO_FILE' to path with suffix (file will be created if not exist).

import mylogging

mylogging.config.TO_FILE = "path/to/my/file.log"  # You can use relative (just log.log)

Then it's the same

import mylogging

mylogging.warn('Hessian matrix copmputation failed for example', caption="RuntimeError on model x")

try:
    print(10 / 0)
except ZeroDivisionError:
    mylogging.traceback("Maybe try to use something different than 0.")

You can use captions as well

mylogging.info("I am interesting info", caption="I am caption")

There is one more function you can use: return_str. It will return edited string (Color, indent and around signs). Use case for that is raising your errors. You can see in one second, whether raise is yours or from imported library.

raise ModuleNotFoundError(mylogging.return_str("It's not in requirements because...", caption="Library not installed error"))

config

There is many things that is better to set globally than setup in each function call.

AROUND

If log to file, whether separate logs with line breaks and ==== or shring to save space. Defaults to True.

COLOR

You can turn off colors if you need (somewhere symbols instead of colors are displayed). "auto" by default means: If to console, it is colorized, if to file, it's not (.log files can be colorized by IDE).

If you have special use case (for example pytest logs on CI/CD), you can override value from auto

mylogging.config.COLORIZE = False # Turn off colorization on all functions to get rid of weird symbols

This is how the results in console look like.

Logging output example

For log file, just open example.log in your IDE. This is how the results in log file opened in VS Code look like.

Logging to file example

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mylogging-3.0.3.tar.gz (16.6 kB view hashes)

Uploaded Source

Built Distribution

mylogging-3.0.3-py3-none-any.whl (15.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page