Structured logging to TSV files with JSON context: grep-able, compressible and searchable.

0 downloads

dbbasic-logs/examples/basic_usage.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-logs.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-logs.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-logs.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add PyPI badges and links to README cea7e20 1 year, 1 month ago History
basic_usage.py · 2.3 KB · 78 lines
1#!/usr/bin/env python3
2"""
3Basic usage examples for dbbasic-logs
4"""
5
6from dbbasic_logs import log
7
8def main():
9 print("DBBasic Logs - Basic Usage Examples\n")
10
11 # Basic logging
12 print("1. Basic logging levels:")
13 log.info("Application started", version="1.0.0")
14 log.debug("Debug information", cache_size=1024)
15 log.warning("Memory usage high", percent=85)
16 log.error("Database connection failed", error="Timeout")
17 print(" ✓ Logged to data/logs/app/YYYY-MM-DD.tsv\n")
18
19 # Logging with context
20 print("2. Logging with rich context:")
21 log.info(
22 "User action",
23 user_id=42,
24 action="update_profile",
25 ip="192.168.1.1",
26 duration=0.123,
27 metadata={"browser": "Chrome", "os": "macOS"}
28 )
29 print(" ✓ Context automatically serialized to JSON\n")
30
31 # Exception logging
32 print("3. Exception logging with stack traces:")
33 try:
34 result = 1 / 0
35 except ZeroDivisionError:
36 log.exception("Division error occurred", operation="calculate", user_id=42)
37 print(" ✓ Stack trace captured in data/logs/errors/YYYY-MM-DD.tsv\n")
38
39 # Access logging
40 print("4. HTTP access logging:")
41 log.access(
42 method="GET",
43 path="/api/users/42",
44 status=200,
45 duration=0.05,
46 ip="192.168.1.1",
47 user_agent="Mozilla/5.0"
48 )
49 print(" ✓ Logged to data/logs/access/YYYY-MM-DD.tsv\n")
50
51 # Searching logs
52 print("5. Search logs:")
53 results = log.search("user_id.*42", log_type='app', days=1)
54 print(f" Found {len(results)} log entries for user_id 42")
55 for result in results[:3]: # Show first 3
56 print(f" - {result['level']}: {result['message']}")
57 print()
58
59 # Tailing logs
60 print("6. Get recent logs:")
61 recent = log.tail('app', lines=5)
62 print(f" Last {len(recent)} application logs:")
63 for entry in recent:
64 print(f" - {entry['level']}: {entry['message']}")
65 print()
66
67 print("All examples complete!")
68 print("\nCheck your logs in:")
69 print(" - data/logs/app/")
70 print(" - data/logs/errors/")
71 print(" - data/logs/access/")
72 print("\nQuery with grep:")
73 print(" grep ERROR data/logs/app/*.tsv")
74 print(" zgrep 'user_id.*42' data/logs/app/*.tsv*")
75
76
77if __name__ == "__main__":
78 main()