2026-03-21 10:05:04 +00:00
|
|
|
# Logging Reference
|
2026-02-05 10:26:32 +00:00
|
|
|
|
2026-03-21 10:05:04 +00:00
|
|
|
Logging lives in the root `core` package in this repository. There is no separate `pkg/log` import path here.
|
2026-02-05 10:26:32 +00:00
|
|
|
|
2026-03-21 10:05:04 +00:00
|
|
|
## Create a Logger
|
2026-02-05 10:26:32 +00:00
|
|
|
|
2026-03-21 10:05:04 +00:00
|
|
|
```go
|
|
|
|
|
logger := core.NewLog(core.LogOptions{
|
|
|
|
|
Level: core.LevelInfo,
|
|
|
|
|
})
|
|
|
|
|
```
|
2026-02-05 10:26:32 +00:00
|
|
|
|
2026-03-21 10:05:04 +00:00
|
|
|
## Levels
|
2026-02-05 10:26:32 +00:00
|
|
|
|
2026-03-21 10:05:04 +00:00
|
|
|
| Level | Meaning |
|
|
|
|
|
|-------|---------|
|
|
|
|
|
| `LevelQuiet` | no output |
|
|
|
|
|
| `LevelError` | errors and security events |
|
|
|
|
|
| `LevelWarn` | warnings, errors, security events |
|
|
|
|
|
| `LevelInfo` | informational, warnings, errors, security events |
|
|
|
|
|
| `LevelDebug` | everything |
|
|
|
|
|
|
|
|
|
|
## Log Methods
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
logger.Debug("workspace discovered", "path", "/srv/workspaces")
|
|
|
|
|
logger.Info("service started", "service", "audit")
|
|
|
|
|
logger.Warn("retrying fetch", "attempt", 2)
|
|
|
|
|
logger.Error("fetch failed", "err", err)
|
|
|
|
|
logger.Security("sandbox escape detected", "path", attemptedPath)
|
|
|
|
|
```
|
2026-02-05 10:26:32 +00:00
|
|
|
|
2026-03-21 10:05:04 +00:00
|
|
|
## Default Logger
|
2026-02-05 10:26:32 +00:00
|
|
|
|
2026-03-21 10:05:04 +00:00
|
|
|
The package owns a default logger.
|
2026-02-05 10:26:32 +00:00
|
|
|
|
|
|
|
|
```go
|
2026-03-21 10:05:04 +00:00
|
|
|
core.SetLevel(core.LevelDebug)
|
|
|
|
|
core.SetRedactKeys("token", "password")
|
|
|
|
|
|
|
|
|
|
core.Info("service started", "service", "audit")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Redaction
|
|
|
|
|
|
|
|
|
|
Values for keys listed in `RedactKeys` are replaced with `[REDACTED]`.
|
2026-02-05 10:26:32 +00:00
|
|
|
|
2026-03-21 10:05:04 +00:00
|
|
|
```go
|
|
|
|
|
logger.SetRedactKeys("token")
|
|
|
|
|
logger.Info("login", "user", "cladius", "token", "secret-value")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Output and Rotation
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
logger := core.NewLog(core.LogOptions{
|
|
|
|
|
Level: core.LevelInfo,
|
|
|
|
|
Output: os.Stderr,
|
|
|
|
|
})
|
2026-02-05 10:26:32 +00:00
|
|
|
```
|
|
|
|
|
|
2026-03-21 10:05:04 +00:00
|
|
|
If you provide `Rotation` and set `RotationWriterFactory`, the logger writes to the rotating writer instead of the plain output stream.
|
2026-02-05 10:26:32 +00:00
|
|
|
|
2026-03-21 10:05:04 +00:00
|
|
|
## Error-Aware Logging
|
|
|
|
|
|
|
|
|
|
`LogErr` extracts structured error context before logging:
|
2026-02-05 10:26:32 +00:00
|
|
|
|
|
|
|
|
```go
|
2026-03-21 10:05:04 +00:00
|
|
|
le := core.NewLogErr(logger)
|
|
|
|
|
le.Log(err)
|
2026-02-05 10:26:32 +00:00
|
|
|
```
|
|
|
|
|
|
2026-03-21 10:05:04 +00:00
|
|
|
`ErrorLog` is the log-and-return wrapper exposed through `c.Log()`.
|
|
|
|
|
|
|
|
|
|
## Panic-Aware Logging
|
|
|
|
|
|
|
|
|
|
`LogPanic` is the lightweight panic logger:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
defer core.NewLogPanic(logger).Recover()
|
|
|
|
|
```
|
2026-02-05 10:26:32 +00:00
|
|
|
|
2026-03-21 10:05:04 +00:00
|
|
|
It logs the recovered panic but does not manage crash files. For crash reports, use `c.Error().Recover()`.
|