Konfig

Three foundational capabilities every Python application needs — settings management, pluggable secrets, and structured logging — with an optional lightweight app lifecycle context manager that ties them together.

Python Configuration Secrets Logging MIT
View on GitHub Install

Overview

Konfig is a clean-sheet Python library that bundles the three things almost every application needs: configuration, secrets, and logging. Instead of wiring together multiple packages and writing boilerplate, Konfig gives you a single, consistent interface with sensible defaults and platform-aware behaviour out of the box.

Each subsystem works independently or together via the AppContext lifecycle manager — no inheritance required.

Features

Settings Management

  • Layered precedence — system, user, environment, and runtime levels with automatic merging
  • Persistent writes — save settings back to disk at any layer
  • Dot-notation accessctx.settings.get("database.host", "localhost")
  • Platform-aware paths — config, data, and log directories resolve correctly on macOS, Linux, and Windows

Secrets Handling

  • Pluggable backends — choose the right storage for your environment:
    • OS Keyring — native credential storage (macOS Keychain, Windows Credential Locker, Linux Secret Service)
    • AES-encrypted file — portable, encrypted secrets file
    • AWS Secrets Manager — cloud-native secrets for deployed applications
  • Custom backends — implement your own with a simple interface
  • Consistent APIctx.secrets.get("api_key") regardless of backend

Structured Logging

  • Run-scoped log files — each application run gets its own log file with automatic historical retention
  • JSON output mode — structured logs for machine consumption
  • Safe stdio handling — graceful fallback when stdout/stderr are unavailable
  • Configurable levels — per-logger granularity

App Lifecycle

  • Context manager — sync and async support via with / async with
  • No inheritance — just wrap your code, no base class required
  • Automatic cleanup — resources are properly released on exit

Installation

Requires Python 3.10+.

Quick Start

Use all three subsystems together via AppContext:

from konfig import AppContext

with AppContext(name="My Application", version="1.0.0") as ctx:
    # Settings — layered, with defaults
    host = ctx.settings.get("database.host", "localhost")
    port = ctx.settings.get("database.port", 5432)

    # Secrets — backend-agnostic
    api_key = ctx.secrets.get("api_key")

    # Logging — already configured
    ctx.log.info("Application started", host=host)

Or use each subsystem independently:

from konfig import Settings, Secrets, LogManager

# Just settings
settings = Settings(app_name="my-app")
db_host = settings.get("database.host", "localhost")

# Just secrets
secrets = Secrets(app_name="my-app")
token = secrets.get("github_token")

Async Support

from konfig import AppContext

async with AppContext(name="My Server", version="2.0.0") as ctx:
    api_key = ctx.secrets.get("api_key")
    ctx.log.info("Server ready")

Platform Paths

Konfig automatically uses the right directories for each platform:

  • macOS: ~/Library/Application Support/<app>/
  • Linux: ~/.config/<app>/ (XDG compliant)
  • Windows: %APPDATA%/<app>/

Licence

MIT Licence — free for any use.


View on GitHub Report an Issue ← All Projects