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 logging-warning module. It log to console or to file based on configuration.

  1. It's automatically colorized and formatted to be more readable and noticeable (you can immediately see what errors are yours)
  2. It's possible to control logs and warnings behaviour (ignore, once, always) as in warnings.
  3. It's possible to filter messages by level (INFO, DEBUG, WARNING, ERROR, CRITICAL) as in logging.

Motivation for this project is to be able to have one very simple code base for logging and warning at once and setup logging at one place, not in every project.

You can use one code for logging apps running on server (developers see what happens on server) and the same code for printing info and warnings from developed library.

Links

Official documentation - https://mylogging.readthedocs.io/

Official repo - https://github.com/Malachov/mylogging

Installation

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

Install just with::

pip install mylogging

Output

This is how the results of examples below look like in console.

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 output example

Examples

Library is made to be as simple as possible, so configuration should be easy (you don't need to configure anything actually)... Just setup path to log file (will be created if not exists). If you will not setup it, log to console will be used. Change FILTER (defaults to once) and LEVEL (defaults to WARNING) if you need. Then syntax is same as in logging module. Functions debug, info, warn, error and critical are available.

import mylogging

mylogging.config.LEVEL = "WARNING"
mylogging.warn("I am interesting warning.")

You can log your errors with traceback, where you set level as input parameter. You can use traceback also with no parameters, traceback type will be used as heading then.

try:
    print(10 / 0)

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

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

raise ModuleNotFoundError(mylogging.return_str("Try pip install...", caption="Library not installed error"))

This is result

Logging output example

Another function is for ignoring specified warnings from imported libraries. Global warnings settings are edited, so if you use it in some library that other users will use, don't forget to reset user settings after end of your call with reset_outer_warnings_filter() or use it in witch.catch_warnings(): block.

Sometimes only message does not work, then ignore it with class and warning type

ignored_warnings = ["mean of empty slice"]
ignored_warnings_class_type = [
    ("TestError", FutureWarning),
]

mylogging.outer_warnings_filter(ignored_warnings, ignored_warnings_class_type)

mylogging.reset_outer_warnings_filter()

If somebody is curious how it looks like on light color theme, here it goes...

Logging output example

Config

Some config, that can be configured globally for not having to use in each function call.

Config values has docstrings, so description should be visible in IDE help.

OUTPUT - Whether log to file or to console. 'console' or path to file (string or pathlib.Path). Defaults by "console"

LEVEL - Set level of severity that will be printed, e.g. DEBUG, ERROR, CRITICAL. Defaults to 'WARNING'.

FILTER - If the same logs, print it always, once or turn all logging off. Possible values "ignore", "once", "always" or "error". Defaults to "once".

Usually thats everything you will setup. If you need different formatting of output, you can define

BLACKLIST - You can filter out some specific messages by content.

FORMATTER_CONSOLE_STR or FORMATTER_FILE_STR with for example::

"{asctime} {levelname} " + "{filename}:{lineno}" + "{message}"

Rest options should be OK by default, but it's all up to you of course: You can setup for example

AROUND - Whether separate logs with line breaks and ==== or shring to save space. Defaults to True.

COLORIZE - Possible options: [True, False, 'auto']. Colorize is automated. If to console, it is colorized, if to file, it's not (.log files can be colorized by IDE). Defaults to 'auto'.

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.4.tar.gz (16.2 kB view hashes)

Uploaded Source

Built Distribution

mylogging-3.0.4-py3-none-any.whl (16.1 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