Skip to main content

sqlogging

A logger that is based on Python's sqlite3 library. Log entries are stored in a SQLite table and can be accessed with SQL queries. It is inspired by the logging library, but does not stay strictly faithful to the API.

The strength of sqlogging isn't speed. It typically takes a few milliseconds to write a log entry (about 7 ms on my machine). But if that's not a blocker for you, the accessibility and flexibility of analysis it gives is a sheer delight. Skip json parsing and unwieldy pandas syntax. If you speak SQL you can make your log data dance for you.

Installation

pip install sqlite-logging

Usage

from sqlogging import logging

logger = logging.create_logger(name="test_logger", columns=["iter", "score"])
logger.info({"iter": 0, "score": .4})
logger.info({"iter": 1, "score": .1})
logger.info({"iter": 2, "score": .8})

result = logger.query(f"SELECT SUM(score) FROM {logger.name}")
print("sum of scores:", result[0][0])

logger.delete()

API

create_logger()

logging.create_logger(name="log", dir_name=".", level="info", columns=["ts", "data"])

For creating a new Logger from scratch. If you try to create a Logger by the same name as a pre-existing logger you'll get a sqlite3.OperationalError.

  • Parameters
    • name(str) - The name of the Logger. This will be both the name of the name of the table and the name of the sqlite3 database file (<name>.db).
    • dir_name(str) - The directory in which the database file will be stored. If it doesn't already exist, it will be created.
    • level(str) - The logging severity level. Must be one of {'debug', 'info', 'warning', 'error', 'critical'} (case insensitive). Only log messages of equal or higher severity will be processed.
    • columns(List of str) - The names of the columns to be created in the sqlite database.
  • Return type: Logger
  • Raises:
    • ValueError: If level is not one of the 5 allowed levels.

open_logger()

logging.open_logger(name="log", dir_name=".", level="info")

For re-opening an existing Logger. If you try to open a Logger that doesn't already exist you'll get a RuntimeError.

  • Parameters: as in create_logger()
  • Return type: Logger

Logger

class logging.Logger(name, dir_name, level="info", columns=None, create=True)

  • Parameters: as in create_logger()
    • create(bool) - Whether a new Logger should be created or an existing one re-opened.

close()

Close the connection to the logger database. Can be reopened later with logging.open_logger().

delete()

Close the connection to the database and delete the database file. Remove it from existence.

debug(data) \ info(data) \ warning(data) \ error(data) \ critical(data)

  • Parameters
    • data(dict) - Write (at the specified severity level) a row into the sqlite db. The dictionary contains keys with the name of the column to be written, and values with the data element corresponding to that column. Any columns not included in the dict keys will be populated with NULL. (These will be None when queried and converted to Python.)

get_columns()

Returns a list of all column names.

  • Return type: list of str

query(query_str)

Run a SQL query against the logger database. Here's a reference for the particular dialect of SQL. It's mostly standard stuff, but as with all SQL dialects can have some surprises, especially if you use some of the fancier features.

  • Parameters:
    • query_str (str)
  • Return type: list of tuple

Release files for sqlite-logging 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlite-logging 0.0.4
File Size Uploaded
sqlite_logging-0.0.4.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlite-logging 0.0.4
File Interpreter ABI Platform
sqlite_logging-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / sqlite_logging-0.0.4.tar.gz

Download URL sqlite_logging-0.0.4.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
791f37387bd49bbd7d4d0422626a1d22e37a91f6ffc70bb9a0e9ce208459254c
BLAKE2b-256 checksum
How to use checksums
b1f13332568b9e66767fd849daeaabf40d501b5b9485a8d3f9b573a711feba4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release files / sqlite_logging-0.0.4-py3-none-any.whl

Download URL sqlite_logging-0.0.4-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
360a5c28dcb4047688a1531bd57dd53e5126223fc6ed430b00bd32e94b57251a
BLAKE2b-256 checksum
How to use checksums
bce4fa47cba6c41069ad7854bbb0fc1d20225d4a949a5a1d02aa2cd415d13395
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page