(new): add threshold-based log level filtering and docs
Golang lint / lint (push) Successful in 1m36s
Golang lint / lint (push) Successful in 1m36s
- add threshold-aware LogLevel constructors - add Logger.SetThresholdMode and SameThreshold - preserve legacy SameLevel behavior for compatibility - extend tests for threshold mode and compatibility - update README and release notes for v2.2.0
This commit is contained in:
@@ -71,6 +71,10 @@ func main() {
|
||||
|
||||
Important: with the current level ordering, `Level(sneklog.FATAL)` allows `INFO`, `WARN`, `ERROR`, and `FATAL`, but not `DEBUG`. Use `Level(sneklog.DEBUG)` to enable every level.
|
||||
|
||||
If you want classic threshold filtering instead, call `SetThresholdMode(true)` and
|
||||
configure levels with `NewThresholdLogLevel(...)` or
|
||||
`NewThresholdLogLevelWithColors(...)`.
|
||||
|
||||
## Writers and ownership
|
||||
|
||||
`Logger.Close()` only closes writers created by the logger itself:
|
||||
@@ -142,6 +146,29 @@ access := sneklog.NewInfoLogLevelWithColors("access", sneklog.FgCyan, sneklog.Bg
|
||||
audit := sneklog.NewWarnLogLevel("audit")
|
||||
```
|
||||
|
||||
These helpers keep the legacy severity index for backward compatibility and also
|
||||
assign default threshold values:
|
||||
|
||||
- `INFO`: `th=10`
|
||||
- `WARN`: `th=20`
|
||||
- `ERROR`: `th=30`
|
||||
- `FATAL`: `th=40`
|
||||
- `DEBUG`: `th=0`
|
||||
|
||||
To define custom threshold-based levels explicitly:
|
||||
|
||||
```go
|
||||
trace := sneklog.NewThresholdLogLevelWithColors(4, 5, "trace", sneklog.FgHiBlack, sneklog.BgNone)
|
||||
audit := sneklog.NewThresholdLogLevel(1, 25, "audit")
|
||||
|
||||
logger := sneklog.NewLogger().
|
||||
SetLevel(audit).
|
||||
SetThresholdMode(true)
|
||||
|
||||
logger.Print(trace, "verbose trace") // filtered out
|
||||
logger.Print(audit, "audit event") // allowed
|
||||
```
|
||||
|
||||
HTTP method-specific predefined levels are available out of the box:
|
||||
|
||||
```go
|
||||
@@ -149,8 +176,11 @@ level := sneklog.LogLevelForMethod(http.MethodPost)
|
||||
logger.Print(level, "POST /users")
|
||||
```
|
||||
|
||||
When comparing levels, use `SameLevel` if only the severity matters and `Equal`
|
||||
if the full configuration, including colors and attributes, must match.
|
||||
When comparing levels:
|
||||
|
||||
- use `SameLevel` if only the legacy severity index matters;
|
||||
- use `SameThreshold` if only threshold filtering matters;
|
||||
- use `Equal` if the full configuration, including threshold, colors, and attributes, must match.
|
||||
|
||||
## Message replacement
|
||||
|
||||
|
||||
Reference in New Issue
Block a user