Commit 3630c51
feat: Add structured logging with log/slog for observability (#47)
Implements comprehensive structured logging using Go's standard log/slog
package (Go 1.21+). Logging is optional with zero overhead when disabled.
## Changes
### Core Infrastructure
- Added WithLogger() functional option to Convert()
- Default behavior uses slog.DiscardHandler (zero overhead)
- Added logger field to converter struct
- Performance metrics with conversion duration tracking
### Logging Points Added
1. **JSON Path Detection** (json.go)
- shouldUseJSONPath: Logs table, field, and JSON detection results
- buildJSONPath: Logs JSON path construction
- buildJSONPathInternal: Logs operator selection (->>, ->)
2. **Comprehension Analysis** (comprehensions.go)
- analyzeComprehensionPattern: Logs identified comprehension type
- Logs for all, exists, exists_one, map, filter comprehensions
- Includes iter_var, accu_var, and has_filter details
3. **Schema Lookups** (cel2sql.go)
- isFieldJSON: Logs field type lookups with hits/misses
- Logs when schemas not provided or table/field not found
4. **Operator Conversion** (cel2sql.go)
- visitCallBinary: Logs CEL to SQL operator mapping
5. **Regex Conversion** (cel2sql.go)
- callMatches: Logs RE2 to POSIX pattern transformation
- Includes original pattern, converted pattern, case sensitivity
6. **Conversion Lifecycle** (cel2sql.go)
- Start/completion events with duration metrics
- Error logging with full context
### Testing
- Added 11 comprehensive tests in logging_test.go
- Tests cover JSON handler, text handler, all logging points
- Verifies zero overhead default behavior
- All existing tests pass
### Documentation
- Updated CLAUDE.md with logging section
- Added examples/logging/ with working example
- Demonstrates both JSON and text handlers
- README with integration guidance
### Example Usage
```go
// Zero overhead (default)
sql, err := cel2sql.Convert(ast)
// With logging enabled
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
sql, err := cel2sql.Convert(ast,
cel2sql.WithSchemas(schemas),
cel2sql.WithLogger(logger))
```
## Benefits
- **Zero Overhead**: DiscardHandler when logging disabled
- **Standards-Based**: Uses Go 1.21+ standard library
- **Structured**: All logs use key-value pairs
- **Flexible**: Supports any slog.Handler
- **Production-Ready**: JSON handler for log aggregation
- **Developer-Friendly**: Text handler for debugging
## Log Events
- JSON path detection decisions
- Comprehension type identification
- Schema lookups (hits/misses, field types)
- Performance metrics
- Regex pattern transformations
- Operator mapping decisions
- Error contexts with full details
Closes #47
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 198d97f commit 3630c51
File tree
7 files changed
+741
-7
lines changed- examples/logging
7 files changed
+741
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
178 | 236 | | |
179 | 237 | | |
180 | 238 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
42 | 44 | | |
43 | 45 | | |
44 | 46 | | |
| 47 | + | |
45 | 48 | | |
46 | 49 | | |
47 | 50 | | |
| |||
78 | 81 | | |
79 | 82 | | |
80 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
81 | 111 | | |
82 | 112 | | |
83 | 113 | | |
| |||
89 | 119 | | |
90 | 120 | | |
91 | 121 | | |
92 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
93 | 127 | | |
94 | 128 | | |
95 | 129 | | |
96 | 130 | | |
| 131 | + | |
| 132 | + | |
97 | 133 | | |
98 | 134 | | |
| 135 | + | |
99 | 136 | | |
100 | 137 | | |
| 138 | + | |
101 | 139 | | |
102 | 140 | | |
103 | 141 | | |
104 | 142 | | |
| 143 | + | |
105 | 144 | | |
| 145 | + | |
106 | 146 | | |
| 147 | + | |
107 | 148 | | |
108 | 149 | | |
109 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
110 | 161 | | |
111 | 162 | | |
112 | 163 | | |
113 | 164 | | |
114 | 165 | | |
115 | 166 | | |
116 | 167 | | |
| 168 | + | |
117 | 169 | | |
118 | 170 | | |
119 | 171 | | |
| |||
158 | 210 | | |
159 | 211 | | |
160 | 212 | | |
| 213 | + | |
161 | 214 | | |
162 | 215 | | |
163 | 216 | | |
164 | 217 | | |
165 | 218 | | |
| 219 | + | |
166 | 220 | | |
167 | 221 | | |
168 | 222 | | |
169 | 223 | | |
170 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
171 | 231 | | |
172 | 232 | | |
173 | 233 | | |
174 | 234 | | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
175 | 238 | | |
176 | 239 | | |
177 | 240 | | |
| |||
359 | 422 | | |
360 | 423 | | |
361 | 424 | | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
362 | 432 | | |
363 | 433 | | |
364 | 434 | | |
| |||
623 | 693 | | |
624 | 694 | | |
625 | 695 | | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
626 | 706 | | |
627 | 707 | | |
628 | 708 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
| |||
95 | 97 | | |
96 | 98 | | |
97 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
98 | 106 | | |
99 | 107 | | |
100 | 108 | | |
| |||
104 | 112 | | |
105 | 113 | | |
106 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
107 | 121 | | |
108 | 122 | | |
109 | 123 | | |
| |||
113 | 127 | | |
114 | 128 | | |
115 | 129 | | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
116 | 136 | | |
117 | 137 | | |
118 | 138 | | |
| |||
122 | 142 | | |
123 | 143 | | |
124 | 144 | | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
125 | 151 | | |
126 | 152 | | |
127 | 153 | | |
| |||
131 | 157 | | |
132 | 158 | | |
133 | 159 | | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
134 | 166 | | |
135 | 167 | | |
136 | 168 | | |
| |||
140 | 172 | | |
141 | 173 | | |
142 | 174 | | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
143 | 180 | | |
144 | 181 | | |
145 | 182 | | |
| |||
0 commit comments