Skip to content

Commit 9c84f66

Browse files
committed
feat(logging): add comprehensive logging standards and practices documentation
1 parent 34127b7 commit 9c84f66

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
description: Evaluate and enforce Python logging discipline across the codebase
3+
---
4+
5+
## Goal 🎯
6+
7+
Audit every Python file in this repository against the logging standards defined below. For each file, report compliance or non-compliance per category, citing concrete evidence (function names, line numbers, code snippets). Where gaps exist, implement fixes immediately using the sensible defaults described in this prompt.
8+
9+
## Steps 👣
10+
11+
### Step 1: Discover Python files
12+
13+
1. Run `git ls-files '*.py'` to enumerate all tracked Python files.
14+
2. For each file, note whether it imports a logging library (e.g., `logging`, `structlog`, `loguru`) or contains `logger` / `log` references.
15+
16+
### Step 2: Assess logging practices
17+
18+
For every file that uses logging (or should but does not), evaluate compliance against the categories below. Report each finding and remediate inline where feasible.
19+
20+
## Implementation requirements 🛠️
21+
22+
### Principles
23+
24+
- **Signal over noise**: logs must capture intent, boundaries, decisions, and failures—not routine execution. Flag verbose or redundant log statements.
25+
- **Structured format**: prefer key=value pairs or JSON; avoid unstructured prose that is hard to parse.
26+
- **Consistency**: log fields, levels, and phrasing should be uniform across all classes and modules. Flag deviations.
27+
28+
### Project-level logger configuration
29+
30+
- **Single configuration point**: one central factory or module must define logger setup; all other modules import from it.
31+
- **Environment-driven settings**: log level, sinks, and format must be configurable via environment variables or a config file, not hard-coded.
32+
- **Dual sinks**: support both human-friendly console output and machine-readable file output (NDJSON with a stable schema).
33+
- **File output schema**: include UTC timestamp (ISO 8601), level, message, logger name, and correlation/request ID.
34+
- **Console readability**: align fields, keep messages concise, and allow optional expanded context on separate lines.
35+
- **No accidental duplication**: require explicit configuration to enable multiple sinks; guard against double logging.
36+
- **Redaction before output**: sensitive fields must be scrubbed before any sink receives the log entry.
37+
38+
### Class-level logging
39+
40+
- **One logger per class/module**: instantiate a logger with the class or module name so context is always present.
41+
- **Constructor logging**: log significant configuration or injected dependencies once at INFO when the object is created.
42+
- **Lifecycle events**: log state transitions (started, stopped, reloaded) at INFO or WARN as appropriate.
43+
- **Dependency failures**: include identifiers, error details, and retry information when logging external dependency issues.
44+
45+
### Method-level logging
46+
47+
- **Entry and exit traces**: every non-trivial method should log entry and exit at DEBUG or TRACE—unless it is a hot path.
48+
- **Safe input logging**: log inputs only after sanitisation; never log secrets, tokens, or PII.
49+
- **Decision points**: log key branch outcomes with relevant identifiers (e.g., user ID, order ID).
50+
- **Exception handling**: log exceptions once at the boundary, include context and correlation ID, and always capture the stack trace.
51+
- **Timing information**: log duration for slow or critical operations (or emit metrics instead).
52+
- **Avoid noise**: do not add "started/finished" logs to trivial or idempotent methods.
53+
54+
### Log levels
55+
56+
| Level | Purpose |
57+
| ----- | ----------------------------------------- |
58+
| TRACE | Per-call granular detail (rarely enabled) |
59+
| DEBUG | Inputs, decisions, diagnostic data |
60+
| INFO | Business events, lifecycle milestones |
61+
| WARN | Recoverable issues, degraded operation |
62+
| ERROR | Failures requiring attention |
63+
64+
Flag any misuse (e.g., logging an error condition at INFO).
65+
66+
### Content
67+
68+
- **Contextual identifiers**: include correlation/request ID, user/session ID (where safe), and operation name.
69+
- **No secrets**: never log passwords, tokens, API keys, or full request/response bodies.
70+
- **Redaction and truncation**: mask sensitive fields and truncate large payloads to a reasonable length.
71+
72+
### Console visualisation
73+
74+
- **Colour as enhancement**: use colour to reinforce meaning, but always pair with text labels, symbols, or icons for accessibility.
75+
- **Contrast and colour-blindness**: ensure sufficient contrast; avoid red/green-only distinctions.
76+
- **Visual hierarchy**: follow timestamp → level → logger → message → context fields.
77+
- **Stable layout**: use fixed-width alignment and consistent field ordering.
78+
- **Subtle accents**: avoid excessive rainbow colouring; keep output scannable.
79+
- **No-colour mode**: auto-disable colour when stdout is not a TTY; honour `NO_COLOR` or similar environment variables.
80+
- **Level tags and icons**: use short, consistent tags (INFO, WARN, ERROR) with an optional single icon per level.
81+
- **Bounded output**: truncate or wrap large fields; never allow unbounded blobs to break layout.
82+
83+
### Performance
84+
85+
- **Guard expensive construction**: wrap costly string formatting or object serialisation in a level check (e.g., `if logger.isEnabledFor(DEBUG)`).
86+
- **Avoid tight-loop logging**: do not log inside hot loops unless sampled or rate-limited.
87+
- **Metrics vs logs**: use metrics for counts and latency; reserve logs for discrete events.
88+
89+
## Output requirements 📋
90+
91+
1. **Findings per file**: for each category above and each of its bullet point, state "Compliant" or "Issue" with a brief explanation.
92+
2. **Evidence links**: reference specific lines using workspace-relative Markdown links (e.g., `[src/app.py](src/app.py#L10-L40)`).
93+
3. **Immediate fixes**: apply sensible defaults inline where possible; do not defer trivial remediations.
94+
4. **Unknowns**: when information is missing, record **Unknown from code – {suggested action}** rather than guessing.
95+
5. **Summary checklist**: after processing all files, confirm overall compliance with:
96+
- Principles
97+
- Project-level logger configuration
98+
- Class-level logging
99+
- Method-level logging
100+
- Log levels
101+
- Content
102+
- Console visualisation
103+
- Performance
104+
105+
---
106+
107+
> **Version**: 1.0.1
108+
> **Last Amended**: 2026-01-28

0 commit comments

Comments
 (0)