Skip to main content

A lightweight Python package for structured logging with automatic balance (ab) formatting for cleaner, more readable output.

Project description

logab 🐢

A lightweight Python package for structured logging with automatic balance (ab) formatting for cleaner, more readable output.

  • 🤤 Python built-ins only
  • 🤯 No dependencies
  • 🥵 Minimal set-up

Table of Contents

🛠️ Installation

pip install logab

🔎 How to use

logab provides two functions: log_wrap() and log_init()

🌮 log_wrap()

log_wrap() is a ready-to-use wrapper with pre-configured logging. log_wrap() should be used only once at the program's entry point to configure logging globally.

Parameters:

  • log_file: file path for storing logs. If not specified, logs will be printed to the terminal.
  • log_level: severity level of log messages being recorded. Defaults to "info"
  • print_level: severity level of all messages output by print() command. Defaults "info"

🍉 log_init()

The logab package supports logging through three methods:

  • Using print(): simple built-in Python function for basic output.
  • Using logging: Python's standard logging module for structured and configurable logging.
  • Using log_init(): works just like Python's logging.getLogger(__name__), just shorter syntax so I don't have to write some boilerplate code hehe.

All three methods are compatible with the logab package, as long as they are used within the log_wrap() context. Usage example is provided in the next section.

🍻 example

import logging
from logab import log_init, log_wrap

def main_func():
    # We can log using Python's logging module
    logger_python = logging.getLogger(__name__)
    logger_python.warning("Warning message using Python's logging")
    # or logab's log_init()
    logger = log_init()
    logger.critical("Critical message using logab's log_init()")
    # or print() (default level is "info", can be configured through log_wrap())
    print("Debug message using print()")

if __name__=="__main__":
    with log_wrap(log_file="./app.log", log_level="debug", print_level="debug" ):
        logger = log_init()
        logger.info("Prepare to do main_func")
        main_func()
        logger.info("Finish main_func")
        x = 0
        print(10/x)

Output log messages in "./app.log":

PID   | Time                    | Level       | Function  |    File:No | Message
------+-------------------------+-------------+-----------+------------+-----------------------------------------
35814 | 2025-05-15 14:22:15,950 | 🔵 info     | <module>  | main.py:19 | Prepare to do main_func
35814 | 2025-05-15 14:22:15,954 | 🟡 warning  | main_func | main.py:9  | Warning message using Python's logging
35814 | 2025-05-15 14:22:15,955 | 🟣 critical | main_func | main.py:12 | Critical message using logab's log_init()
35814 | 2025-05-15 14:22:15,955 | 🟢 debug    | main_func | main.py:14 | Debug message using print()
35814 | 2025-05-15 14:22:15,955 | 🔵 info     | <module>  | main.py:21 | Finish main_func
35814 | 2025-05-15 14:22:15,958 | 🔴 error    | log_wrap  |   logab:0  | division by zero
------+-------------------------+-------------+-----------+------------+-----------------------------------------
Traceback (most recent call last):
  File "/home/namkha/Documents/mydev/logab/logab/log_utils.py", line 179, in log_wrap
    yield
  File "/home/namkha/Documents/mydev/logab/main.py", line 23, in <module>
    print(10/x)
          ~~^~
ZeroDivisionError: division by zero
------+-------------------------+-------------+-----------+------------+-----------------------------------------
35814 | 2025-05-15 14:22:15,958 | 🔵 info     | log_wrap  |   logab:0  | Execution time 0.0085 seconds

⚖️ Automatic balance

Log messages will be displayed in a table format. Each column automatically adjusts its width to fit the longest value in that column, ensuring the entire table remains clearly visible and easy to read. Alt Text

⏳ Upcoming features

  • Log rotation & retention: automatically create new files to handle large-size files
  • Structured logging: support for structured (dict-style) logs
  • Thread/process-safe logging: Ensure logging from multiple threads/proccesses is handled correctly
  • ...

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

logab-0.1.3.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logab-0.1.3-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file logab-0.1.3.tar.gz.

File metadata

  • Download URL: logab-0.1.3.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for logab-0.1.3.tar.gz
Algorithm Hash digest
SHA256 f0f63e5b202f8c02277e97019fc31d06a2741c7f2e4a18b2ca3601294d64281f
MD5 5d4eba2271cea39cc1a2b7534b420ba2
BLAKE2b-256 7b5162063a234647631ad82667b941853b4869d88d6bbeada71cc457f3d37385

See more details on using hashes here.

File details

Details for the file logab-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: logab-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for logab-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ce94f74c4034dd71f9ac4f86b88e1d917b2346ed13bade54ed92ba7a1782f087
MD5 1453dbbd278f0cf0ebdcb976b3cb31f3
BLAKE2b-256 9b5ee6621cb3341b8a1572d677ea4c6581fe1a85aa72a2094540f0e4ab8a8b63

See more details on using hashes here.

Supported by

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