Skip to main content

An easy, customizable formatter for Python logging

Project description

Easy Logging

A highly customizable, zero-dependency logging formatter for Python.

easy-logging bridges the gap between the standard library's complexity and the ease of use of third-party tools like Loguru. It creates beautiful, colored logs for development and structured JSON logs for production, all while using the standard logging module you already know.

Features

  • Zero Dependencies: No heavy libraries like rich or pydantic. Just pure Python.
  • Zero Config Defaults: readable, colored logs out of the box.
  • Context Injection: Automatically adds extra={...} fields to your logs without manual formatting.
  • Production Ready: Switch to JSON output with a single boolean flag.
  • Smart Paths: Automatically shortens absolute file paths (e.g., src/main.py instead of /Users/me/projects/src/main.py).

Installation

Pip/PyPI

pip install easy-logging-formatter

uv

uv add easy-logging-formatter

Usage

Basic Usage

import logging
from easy_logging import EasyFormatter

# Set up the handler
handler = logging.StreamHandler()
handler.setFormatter(EasyFormatter())

# Configure the logger
logger = logging.getLogger("app")
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.info("Hello, world!")

Advanced Usage

import logging
from easy_logging import EasyFormatter, FormatterConfig

config = FormatterConfig(
    template="[{time}] {level_icon} {level} | {message}",
    date_format="%H:%M:%S",
    use_colors=True,
    json_output=False,
    shorten_paths=True,
    exclude_fields=["color", "icon"],
    use_icons=False,
)
formatter = EasyFormatter(config=config)

handler = logging.StreamHandler()
handler.setFormatter(formatter)
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(logging.INFO)

logging.info("Hello, world!")

Configuration

easy-logger provides a FormatterConfig class that allows you to customize the output of your logs. Here's an example of how to use it:

from easy_logging import FormatterConfig

config = FormatterConfig(
    template="[{time}] {level_icon} {level} | {message}",
    date_format="%H:%M:%S",
    use_colors=True,
    json_output=False,
    shorten_paths=True,
    exclude_fields=["color", "icon"],
    use_icons=False,
)

The FormatterConfig class has the following attributes:

Attribute Type Description
template str The format string to use for the log message.
date_format str The format string to use for the timestamp.
use_colors bool Whether to use colors in the output.
json_output bool Whether to output the log message as JSON.
shorten_paths bool Whether to shorten the paths in the log message.
exclude_fields list[str] A list of fields to exclude from the log message.
use_icons bool Whether to use icons in the output (e.g. ⚠️ for warnings).

Customization

easy-logger is designed to be highly customizable. Here are some examples of how you can customize the output of your logs:

Customize the Template

You can customize the template by passing a string to the template attribute of the FormatterConfig class. The template supports the following placeholders:

Placeholder Description
{time} The current time in the format specified by the date_format attribute.
{name} The name of the logger.
{filename} The name of the file where the log message was generated.
{lineno} The line number where the log message was generated.
{level} The log level as a string.
{level_icon} The log level as an icon.
{message} The log message.
{context} The context fields for the log message.

Here's an example of how to customize the template:

from easy_logging import FormatterConfig

config = FormatterConfig(
    template="[{time}] {level_icon} {level} | {message}",
)

Customize the Date Format

You can customize the date format by passing a string to the date_format attribute of the FormatterConfig class. Here's an example of how to customize the date format:

from easy_logging import FormatterConfig

config = FormatterConfig(
    date_format="%H:%M:%S",
)

Customize the Colors

You can customize the colors by passing a Theme object to the theme attribute of the EasyFormatter class. Here's an example of how to customize the colors:

from easy_logging import EasyFormatter, Theme

theme = Theme(
    level_colors={
        logging.DEBUG: Ansi.CYAN,
        logging.INFO: Ansi.GREEN,
        logging.WARNING: Ansi.YELLOW,
        logging.ERROR: Ansi.RED,
        logging.CRITICAL: Ansi.MAGENTA,
    },
)
formatter = EasyFormatter(theme=theme)

The Theme class has the following attributes:

Attribute Type Description
default_color Ansi The default color for log messages.
level_colors dict[int, Ansi] A dictionary mapping log levels to colors.

The Ansi class has the following attributes:

Attribute Type Description
RESET str The reset code for ANSI escape sequences.
BOLD str The bold code for ANSI escape sequences.
DIM str The dim code for ANSI escape sequences.
RED str The red code for ANSI escape sequences.
GREEN str The green code for ANSI escape sequences.
YELLOW str The yellow code for ANSI escape sequences.
BLUE str The blue code for ANSI escape sequences.
MAGENTA str The magenta code for ANSI escape sequences.
CYAN str The cyan code for ANSI escape sequences.
WHITE str The white code for ANSI escape sequences.

License

easy-logger is licensed under the GNU General Public License v3.0. See the LICENSE file for more information.

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

easy_logging_formatter-0.1.1.tar.gz (47.1 kB view details)

Uploaded Source

Built Distribution

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

easy_logging_formatter-0.1.1-py3-none-any.whl (32.3 kB view details)

Uploaded Python 3

File details

Details for the file easy_logging_formatter-0.1.1.tar.gz.

File metadata

  • Download URL: easy_logging_formatter-0.1.1.tar.gz
  • Upload date:
  • Size: 47.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for easy_logging_formatter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 996359868c7605142531048679ffedd6884b8beeb4bd622bcf8d5be4898cecd3
MD5 c9ae708302fcf84962f55980dd37eed5
BLAKE2b-256 8601581211e93d5ea97f434fdadb92e82bb1afd058fc50be04b89b7145d37b29

See more details on using hashes here.

File details

Details for the file easy_logging_formatter-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for easy_logging_formatter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5ea2831ad11fb94eb61dc79eb9410b85c3c1306e23ebd2632d24fe57264acf93
MD5 9d71f580ff17e8e04a8dd220aaf7ee35
BLAKE2b-256 402f82bb65813e33fc9a301733611dc55d34f6cbf2f620338acd76ba36a54609

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