Clogger is a context aware, structured logging library written in Go.
- GCP Stackdriver
- JSON
- Terminal (human friendly output)
- OpenCensus
- OpenTelemetry
package main
import log "github.com/foae/clogger"
func main() {
// Create a new default logger
logger := log.NewDefaultLogger()
// We want the output to be human-readable
// – we choose a terminal encoder
logger.SetEncoder(&log.TerminalEncoder{})
// For each and every EVENT, we can choose
// decorators or write our own.
logger.SetEventOptions(log.WithOpenTelemetryTrace(), log.WithExampleEventOption())
// For each and every LOG ENTRY, we can choose
// decorators or write our own.
logger.SetLogEntryOptions(log.WithOpenCensusSpan())
// Everything is configured, stored it globally for later use
log.SetGlobal(logger)
}
- Creating new Events
package main
import (
"context"
log "github.com/foae/clogger"
)
func handlePOST(ctx context.Context) {
ctx, event := log.NewEvent(ctx, "Received a new POST request")
defer event.End()
// Attach data directly to the event
event.Set("key", "value")
event.SetOnErr("key", "value")
event.SetLabel("key", "value")
// ... TODO: add more examples, explain what each Set* is doing
}
- Logging fields
// consider: import log "github.com/foae/clogger"
// consider: ctx exists
log.Debug(ctx, "A debug statement")
log.With("user_id", userID).Debug(ctx, "A debug statement")
- Events
- Set
- SetOnErr
- SetLabel
- Log entries
- Fields
Current state: alpha