Skip to content

Instantly share code, notes, and snippets.

@simonw
Created September 30, 2019 16:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonw/3498fadbc9d8aea3967bdb4cddbf48d8 to your computer and use it in GitHub Desktop.
Save simonw/3498fadbc9d8aea3967bdb4cddbf48d8 to your computer and use it in GitHub Desktop.
Class for logging structlog entries to a SQLite database
# Use it like this:
#
# structlog.configure(
# processors=[
# structlog.processors.JSONRenderer(),
# LogToSqlite("/tmp/logs.db")
# ]
# )
# log = structlog.get_logger()
# log.msg("say-hello", whom="world", num=[random.randint(1,55)])
import sqlite_utils
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({**event_dict, **{
"name": name,
}}, alter=True)
return event_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment