Add your description here
Project description
Mogger
A custom logging library with SQLite persistence, colored terminal output, and Loki integration.
Features
- YAML-driven schema configuration - Define your log tables and fields in a YAML file
- SQLite database with relational design - All logs stored in a persistent database
- Colored terminal output - Beautiful colored logs using Rich library
- Loki integration - Send logs to Grafana Loki for centralized logging
- Flexible logging control - Enable/disable database and terminal output per log
- UUID tracking - Every log entry has a unique identifier
- Multiple log tables - Create custom tables for different types of logs
- Context management - Add context data to all logs in a scope
- Query API - Retrieve and analyze logs from the database
- Automatic config detection - No need to specify config path if file is in project root
Installation
pip install mogger
Quick Start
1. Create a configuration file
Create mogger_config.yaml in your project root:
database:
path: "./logs.db"
wal_mode: true
tables:
- name: "user_actions"
fields:
- name: "user_id"
type: "string"
indexed: true
- name: "action"
type: "string"
terminal:
enabled: true
colors:
INFO: "green"
ERROR: "red"
WARNING: "yellow"
2. Use Mogger in your code
from mogger import Mogger
# Automatic config detection - looks for mogger_config.yaml in current directory
logger = Mogger()
# Or specify config explicitly
# logger = Mogger("path/to/config.yaml")
# Log messages
logger.info("User logged in", category="user_actions", user_id="123", action="login")
logger.error("Something failed", category="errors", error_code=500, error_message="Server error")
# Query logs
recent_errors = logger.query(category="errors", limit=10)
user_logs = logger.query(category="user_actions", user_id="123")
# Close when done
logger.close()
Initialization Options
Basic Initialization
from mogger import Mogger
# Default: local database and terminal output enabled
logger = Mogger("mogger_config.yaml")
# Disable local database (Loki-only or terminal-only logging)
logger = Mogger("mogger_config.yaml", use_local_db=False)
# Custom database path
logger = Mogger("mogger_config.yaml", db_path="./custom_logs.db")
Loki Integration
from mogger import Mogger, LokiConfig
# Configure Loki
loki_config = LokiConfig(
url="http://localhost:3100/loki/api/v1/push",
tags={"application": "my-app", "environment": "production"},
username="loki", # Optional
password="password" # Optional
)
# Initialize with Loki support
logger = Mogger("mogger_config.yaml", loki_config=loki_config)
# Loki-only logging (no local database)
logger = Mogger("mogger_config.yaml", loki_config=loki_config, use_local_db=False)
Logging Control Parameters
All logging methods (debug, info, warning, error, critical) support these parameters:
use_local_db (default: True)
Control whether logs are written to the local SQLite database.
# Skip database for this specific log
logger.info("Temporary message", category="user_actions",
user_id="123", action="click", use_local_db=False)
# Log only to Loki and terminal, not database
logger.error("Remote error", category="errors",
error_code=500, use_local_db=False)
log_to_shell (default: True)
Control whether logs are printed to the terminal.
# Silent logging (database and Loki only)
logger.info("Background task", category="system_events",
event_type="cron", log_to_shell=False)
# Quiet error logging
logger.error("Internal error", category="errors",
error_code=500, log_to_shell=False)
Combining Parameters
# Terminal only (no database, no Loki)
logger.info("Debug info", category="user_actions",
user_id="123", use_local_db=False, log_to_shell=True)
# Completely silent (Loki only if configured)
logger.info("Silent audit", category="audit",
action="access", use_local_db=False, log_to_shell=False)
# Database only (silent logging)
logger.info("Background event", category="system_events",
event_type="backup", log_to_shell=False)
Configuration
Config File Naming
Mogger automatically searches for these config files in your project root:
mogger_config.yaml(recommended)mogger.config.yaml.mogger.yamlmogger_config.ymlmogger.config.yml.mogger.yml
Supported Field Types
string- Variable-length stringtext- Long textinteger- Integer numberfloat- Floating point numberboolean- True/Falsejson- JSON data (automatically serialized/deserialized)datetime- Date and time
Terminal Colors
Available colors: black, red, green, yellow, blue, magenta, cyan, white
Advanced Usage
Context Management
# Set context that applies to all subsequent logs
logger.set_context(request_id="req_123", user_id="user_456")
logger.info("Action 1", category="user_actions", action="click")
logger.info("Action 2", category="user_actions", action="scroll")
# Clear context
logger.clear_context()
Disable Terminal Output Globally
logger.set_terminal(False) # Logs only to database (and Loki if configured)
Query Logs
# Get all logs from a table
all_logs = logger.query(category="user_actions")
# Filter logs
errors = logger.query(category="logs_master", log_level="ERROR")
user_errors = logger.query(category="errors", user_id="123")
# Limit results
recent = logger.query(category="user_actions", limit=50)
Use Cases
Scenario 1: Production with Loki + Local Database
from mogger import Mogger, LokiConfig
loki_config = LokiConfig(
url="https://loki.example.com/loki/api/v1/push",
tags={"application": "web-api", "environment": "production"}
)
logger = Mogger("mogger_config.yaml", loki_config=loki_config)
# All logs go to local DB, Loki, and terminal
logger.info("Request processed", category="api_requests",
endpoint="/api/users", response_time=0.15)
Scenario 2: Development with Terminal Only
# No database, just terminal output
logger = Mogger("mogger_config.yaml", use_local_db=False)
logger.info("Debug message", category="user_actions",
user_id="dev", action="test")
Scenario 3: Loki-Only Logging (No Local Storage)
from mogger import Mogger, LokiConfig
loki_config = LokiConfig(
url="http://localhost:3100/loki/api/v1/push",
tags={"application": "microservice"}
)
logger = Mogger("mogger_config.yaml", loki_config=loki_config, use_local_db=False)
# All logs go only to Loki
logger.info("Service started", category="system_events",
event_type="startup")
Scenario 4: Mixed Logging Patterns
logger = Mogger("mogger_config.yaml", loki_config=loki_config)
# Important logs: all destinations
logger.error("Critical failure", category="errors",
error_code=500, severity="critical")
# Debug logs: terminal only
logger.debug("Variable value", category="debug",
value=42, use_local_db=False)
# Audit logs: database and Loki only (silent)
logger.info("User action", category="audit",
user_id="123", action="delete", log_to_shell=False)
# Temporary logs: terminal only (not persisted)
logger.info("Processing...", category="status",
progress=50, use_local_db=False)
Development
Running Tests
pytest tests/
Building
python -m build
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mogger-0.2.2.tar.gz.
File metadata
- Download URL: mogger-0.2.2.tar.gz
- Upload date:
- Size: 28.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca829cc5d54f1ebd4fd3fcf2418668e96a804c480562d086019f4381ef935b79
|
|
| MD5 |
f69c1687d0df9f9ce4525d7781f05be3
|
|
| BLAKE2b-256 |
4dd1a61801755542e56e5e0d1adf0e46e1035a92ffa3e571aaefa52192b818f5
|
File details
Details for the file mogger-0.2.2-py3-none-any.whl.
File metadata
- Download URL: mogger-0.2.2-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10998b5e3fdb0d77100afead0f1c01bd52350d52de4f311a99f8e714181e23e5
|
|
| MD5 |
338957425a94eea8bf5c73e35c2f0967
|
|
| BLAKE2b-256 |
1f2d7a36911c91fc2bab35b7b2ddb2f18f25ba9b598586829850f7407a5ba86a
|