Skip to main content

Production logging with console, database, and Discord notification support

Project description

Hibiki Logger

Production logging with console, database, and Discord notification support.

GitHub Python 3.10+ License: MIT

AI coding assistant? Provide LLMGUIDE.md as context for a complete API reference.

Features

  • Console logging (human-readable or JSON)
  • Database logging via SQLAlchemy (async, non-blocking)
  • Discord webhook notifications for errors
  • Request context support (user_id, path, method)
  • Separate log level thresholds for console, DB, and Discord

Installation

pip install hibiki-logger

Quick start

Console only

from hibiki_logger import configure_logging, get_logger

configure_logging(namespace="myapp")
logger = get_logger("myapp.service")
logger.info("Ready")

Add Discord notifications

Set the LOG_DISCORD_WEBHOOK_URL environment variable — no code changes needed.

Add database logging

Uses SQLAlchemy. Call setup_db_logging() after your DB is ready, passing your existing session_maker and a log model created with create_log_model:

from hibiki_logger import setup_db_logging
from hibiki_logger.models import create_log_model

Log = create_log_model(Base)
setup_db_logging(session_maker=session_maker, log_model=Log, namespace="myapp")

The log table must exist before logging starts. The expected schema:

Column Type Nullable
id VARCHAR(36) primary key no
level VARCHAR(20) no
message TEXT no
logger_name VARCHAR(255) no
user_id VARCHAR(36) yes
path VARCHAR(255) yes
method VARCHAR(10) yes
trace TEXT yes
created_at TIMESTAMP WITH TIME ZONE yes

If you use Alembic or another migration tool, generate a migration after calling create_log_model(Base). Otherwise, create it directly:

async with engine.begin() as conn:
    await conn.run_sync(Base.metadata.create_all)

Not using SQLAlchemy? Use the raw DDL from from hibiki_logger.models import LOG_TABLE_SQL.

Configuration

Variable Default Description
LOG_DISCORD_WEBHOOK_URL (none) Discord webhook URL
LOG_DISCORD_USERNAME (none) Display name for Discord webhook messages
LOG_DB_TABLE_NAME log Database table name for log entries (also reads LOG_TABLE_NAME)
LOG_DB_MIN_LEVEL WARNING Minimum level saved to DB
LOG_DISCORD_MIN_LEVEL ERROR Minimum level sent to Discord
ENV development Set to production for JSON output and ERROR-only console level

Namespace

Only loggers whose names start with the namespace receive DB and Discord handlers:

get_logger("myapp.api")     # DB + Discord handlers
get_logger("other.module")  # console only

Use extra_loggers to include third-party loggers:

configure_logging(namespace="myapp", extra_loggers=["uvicorn", "fastapi"])

Request context

from hibiki_logger import add_context_to_logger

logger = add_context_to_logger(get_logger("myapp.users"), user_id="123", path="/api/users", method="POST")
logger.error("User creation failed")

API reference

Function Description
configure_logging(namespace, extra_loggers=None) Configure console + Discord. Call once at startup.
setup_db_logging(session_maker, log_model, namespace) Enable DB logging. Call after DB is ready.
get_logger(name) Get a logger; DB/Discord handlers auto-attached if name matches namespace.
add_context_to_logger(logger, user_id, path, method) Wrap a logger with request context.
async log_to_db(level, message, logger_name, ...) Manually log to DB.
async log_to_discord(level, message, logger_name, ...) Manually send to Discord.
async log_error(error, logger_name, ...) Log an exception with traceback to DB.

Manual Discord notifications

Standard logging calls (logger.error(...)) send Discord notifications in the background automatically. Your code is never blocked. If you need to send a Discord message explicitly and confirm it was delivered, await log_to_discord() directly:

from hibiki_logger import log_to_discord

await log_to_discord(
    level="ERROR",
    message="Payment processing failed",
    logger_name="myapp.billing",
    user_id="123",
)

Framework integration

FastAPI — call both functions inside your lifespan handler:

@asynccontextmanager
async def lifespan(app: FastAPI):
    configure_logging(namespace="app", extra_loggers=["uvicorn", "fastapi"])
    setup_db_logging(session_maker=async_session_maker, log_model=Log, namespace="app")
    yield

Django — call configure_logging() in settings.py.

Flask — call configure_logging() at app initialization.

Troubleshooting

Logs not appearing in database — verify setup_db_logging() was called, the logger name matches the namespace, and LOG_DB_MIN_LEVEL allows the level.

Discord notifications not sending — verify LOG_DISCORD_WEBHOOK_URL is set and LOG_DISCORD_MIN_LEVEL allows the level.

License

MIT

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

hibiki_logger-1.1.1.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

hibiki_logger-1.1.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file hibiki_logger-1.1.1.tar.gz.

File metadata

  • Download URL: hibiki_logger-1.1.1.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for hibiki_logger-1.1.1.tar.gz
Algorithm Hash digest
SHA256 aadf1c0100731ff98c84d265dfab8e42c009bf2a1878da83a365651e3ec03bcf
MD5 acd676c5d34ef2b57ffbe8be7db0f1c8
BLAKE2b-256 57a8a3ec71e0cb778be7e902142d7a5680557f1b64c9f93dd01f404d508187e6

See more details on using hashes here.

File details

Details for the file hibiki_logger-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: hibiki_logger-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for hibiki_logger-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 01358fb7950ab426293ce29abe75126780022106d59c7048387f1d46b9f7b504
MD5 190c7ae4a2f2c20380730fa618565f98
BLAKE2b-256 87011954af06f92a2d76c92a72f34c05aa00a17c98aa57870a84e75ff5394233

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