Skip to main content

Add your description here

Project description

Slogger: A Simple, Structured Logging Library for Python

Slogger is a logging library for Python that makes structured, configurable logging simple and intuitive.

Features

  • Structured JSON Logging: Automatically formats logs into JSON lines (.jsonl), perfect for log aggregation services like Datadog, Splunk, or the ELK stack.
  • Colored Console Output: Provides level-differentiated, human-readable logs in the console during development.
  • Configuration as Code: Uses Pydantic models for a type-safe, validated, and easily-serializable configuration.
  • Log Rotation: Built-in support for RotatingFileHandler to manage log file size and backups automatically.
  • Modern Tooling: Developed with uv for dependency management and ruff for formatting and linting, ensuring high code quality.
  • Extensible: Simple, class-based design that's easy to extend with custom formatters or handlers.

Installation

This project uses uv for package and environment management.

  1. Clone the repository
git clone https://github.com/89jobrien/slogger
cd slogger
  1. Create a virtual environment and install dependencies:
uv venv
uv sync

This will install all necessary dependencies listed in pyproject.toml.

Quick Start

Using slogger is designed to be straightforward. Instantiate the StructuredLogger with a name and an optional configuration, and then use the standard logging interface.

# main.py
from pathlib import Path
from slogger import StructuredLogger, LogConfig, ConsoleConfig, FileConfig

# 1. Define a configuration (or use the default)
my_config = LogConfig(
    level="DEBUG",
    console=ConsoleConfig(level="INFO", format="color"),
    file=FileConfig(level="DEBUG", format="json", path=Path("logs/app.jsonl")),
)

# 2. Instantiate the logger
# This sets up all handlers and formatters based on the config
logger_wrapper = StructuredLogger(name="my_app", config=my_config)

## 3. Get the underlying logger instance to use in your application
log = logger_wrapper.get_logger()


## 4. Log messages
log.info("Application starting up.")
log.debug("This is a detailed debug message for the file.")
log.warning("API key is not set, using a default value.")
log.error(
    "Failed to connect to the database.",
    extra={"db_host": "localhost", "port": 5432}
)

try:
    1 / 0
except ZeroDivisionError:
    log.critical("A critical error occurred!", exc_info=True)

print("✅ Logging complete. Check the console and 'logs/app.jsonl'.")

Configuration

Logging behavior is controlled by the LogConfig Pydantic model. You can configure it programmatically (as above) or by loading it from a JSON file.

Programmatic Configuration

Create an instance of LogConfig and pass it to the StructuredLogger.

from slogger import LogConfig, ConsoleConfig, FileConfig

# Disable file logging and only show warnings on the console

prod_config = LogConfig(
    level="WARNING",
    console=ConsoleConfig(level="WARNING", format="text"),
    file=FileConfig(enabled=False)
)

Loading from a File

You can also manage configurations in a file, which is ideal for different environments (dev, staging, prod).

1. Create a config.json file

{
  "level": "INFO",
  "console": {
    "level": "INFO",
    "format": "color"
  },
  "file": {
    "enabled": true,
    "path": "logs/production.jsonl",
    "level": "INFO",
    "format": "json"
  }
}

2. Load it in your application

from slogger import StructuredLogger, LogConfig

# Load the configuration from the file

config = LogConfig.load("config.json")

# Initialize the logger with the loaded config

logger_wrapper = StructuredLogger(name="from_file_app", config=config)
log = logger_wrapper.get_logger()

log.info("This logger was configured from a file.")

Running Tests

This project uses pytest for testing. To run the test suite, execute the following command from the project root:

uv run pytest

License

This project is licensed under the MIT License. See the LICENSE file for more details.

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

slogit-0.1.0.tar.gz (26.7 kB view details)

Uploaded Source

Built Distribution

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

slogit-0.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file slogit-0.1.0.tar.gz.

File metadata

  • Download URL: slogit-0.1.0.tar.gz
  • Upload date:
  • Size: 26.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.19

File hashes

Hashes for slogit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 65b3d0b5e456044b269b44ceda13039f7da905495ae1fd8c8a75d0ae5b3c48da
MD5 ab56d106fd3c4037131630e2c6ee7a5e
BLAKE2b-256 b0b2b04dd591fea4fc79244cb107ae695ba5131cbca52f81f0ad9c71c2ad1fb3

See more details on using hashes here.

File details

Details for the file slogit-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: slogit-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.19

File hashes

Hashes for slogit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 14bb8bf3fd6819c833ae2e04b9240146df47dbe0a5e7626e35f7a47e37d913c6
MD5 b21dfe492a5e3eb7c687c4ddd58bb44c
BLAKE2b-256 d21393f1fc9783af3f3832dcfccb1390a5869ee0cfcacd8b3c05d99e4fdb05ce

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