Created
November 21, 2025 13:04
-
-
Save felixhummel/a6fdbc77d709861ba414d479638166b4 to your computer and use it in GitHub Desktop.
structlog to sqlite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import sys | |
| from pathlib import Path | |
| import sqlite_utils | |
| import structlog | |
| # https://gist.github.com/simonw/3498fadbc9d8aea3967bdb4cddbf48d8 | |
| class LogToSqlite: | |
| def __init__(self, db_path, table='logs'): | |
| self.db = sqlite_utils.Database(db_path) | |
| self.table = table | |
| def __call__(self, logger, name, event_dict): | |
| self.db[self.table].insert( # pyright: ignore[reportAttributeAccessIssue] | |
| { | |
| **event_dict, | |
| **{ | |
| 'logger_name': name, | |
| }, | |
| }, | |
| alter=True, | |
| ) | |
| return event_dict | |
| def configure() -> Path: | |
| persistent_tmp_dir = Path('~/tmp').expanduser() | |
| sqlite_db = persistent_tmp_dir / 'logs.sqlite' | |
| loglevel_name = os.environ.get('LOGLEVEL', 'INFO').upper() | |
| structlog.configure( | |
| wrapper_class=structlog.make_filtering_bound_logger(loglevel_name), | |
| logger_factory=structlog.PrintLoggerFactory(sys.stderr), | |
| ) | |
| structlog.configure( | |
| processors=[ | |
| LogToSqlite(sqlite_db), | |
| # structlog.processors.JSONRenderer(), | |
| structlog.dev.ConsoleRenderer(), | |
| ] | |
| ) | |
| return sqlite_db | |
| def get_logger(): | |
| db_path = configure() | |
| log = structlog.get_logger() | |
| log.info('logging configured', db_path=str(db_path)) | |
| return log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment