Skip to main content

Fancy logging with local SQLite storage.

Project description

Logscope

Logscope is a Python logging library that brings together SQLite storage and pretty console output. Write logs to both your terminal and a queryable database with a dead-simple API.

Features

  • Write logs to SQLite + console simultaneously
  • Smart context tracking (captures function names and calling code)
  • Colorized, pretty console output
  • Thread-safe SQLite operations
  • Dead simple API
  • Trace Execution: Automatically log function calls, returns, and exceptions
  • Flexible Querying: Retrieve logs with custom SQL queries

Installation

pip install logscope

Quick Start

from logscope import logger

# Get a logger with default settings (SQLite + console output)
log = logger()

# Log some things!
log("Starting analysis")
log("Processing item", {"id": 123, "status": "pending"})

Configuration

import logging
from logscope import logger

# All settings are optional
log = logger(
    db_path='app.db',      # Where to store the SQLite database
    style='plain',         # 'plain' or 'colorful' console output
    name='MyApp',          # Logger name (defaults to timestamp)
    level=logging.INFO     # Logging level (default: DEBUG)
)

Tracing Functions

Logscope includes a @trace decorator to automatically log function calls, returns, and exceptions:

from logscope.tracing import trace

@trace
def my_function(a, b):
    return a + b

my_function(2, 3)  # Automatically logs the call, return value, and source code

How It Works

Each log message is:

  1. Written to a SQLite database for later querying
  2. Displayed in your console with context details
  3. Tracked with its calling context (which function called it and how)

Example console output:

2024-03-15 14:23:45.123456> Starting analysis
  · process_data:data = process_data("large_file.txt")
  · /path/to/script.py:45

Querying Logs

Use the query function to fetch logs directly from Python:

from logscope import query

logs = query("SELECT * FROM logs WHERE event_type = 'error'", db_path='app.db')
for log in logs:
    print(log)

Database Schema

Logs are stored in SQLite with this schema:

CREATE TABLE logs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    run TEXT,              -- Unique ID for each logger instance
    timestamp TEXT,        -- ISO format with microseconds
    message TEXT,          -- The logged message
    filename TEXT,         -- Source file path
    lineno INTEGER,        -- Line number
    source TEXT,          -- The actual calling code
    function TEXT,         -- Calling function name
    event_type TEXT        -- Custom event type (e.g., 'error', 'info')
);

Example Queries

Get the last 10 logs from a specific run:

SELECT timestamp, message, function
FROM logs
WHERE run = '2024-03-15 14:23:45.123456'
ORDER BY timestamp DESC
LIMIT 10;

Find all errors from a specific function:

SELECT timestamp, message
FROM logs
WHERE function = 'process_data'
  AND message LIKE '%error%'
ORDER BY timestamp;

Retrieve all logs with a custom event type:

SELECT *
FROM logs
WHERE event_type = 'trace';

License

MIT License - See LICENSE file for details

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

logscope-0.1.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

logscope-0.1.2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file logscope-0.1.2.tar.gz.

File metadata

  • Download URL: logscope-0.1.2.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.1

File hashes

Hashes for logscope-0.1.2.tar.gz
Algorithm Hash digest
SHA256 cf88ba81bbf9a4e2a2dcca78a173eeca08b4a00382e9575d7cd658cb1b7e92a7
MD5 b21a2f062ac415c0da5299da6a03fc72
BLAKE2b-256 562796a92b09775e0d04769b6ddfc7a838a6965774ab57a769370327a98f665c

See more details on using hashes here.

File details

Details for the file logscope-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: logscope-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.1

File hashes

Hashes for logscope-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 da25a822a52127f06dd4b9b92bc7762e09d1c30b02457798a3c085d61c7e9a9e
MD5 b3dd2c17ca29c0e43db945aaf74a1bce
BLAKE2b-256 dabb20e7c17f78507691c9a92e31bb95bd42e1eb67c582149802294ba9f5b7f5

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