In the apko project, logging is implemented using the pkg/log package in Go. The pkg/log package provides a simple and flexible logging interface that can be used to emit log messages with different levels of severity.
There are several options for configuring logging in apko:
- Log level: The log level determines the severity of the log message. The
pkg/logpackage supports the following log levels, in order of severity:DEBUG,INFO,WARN,ERROR, andFATAL. The log level can be set using theSetLevelfunction. For example, to set the log level toINFO, you would use the following code:
log.SetLevel(log.INFO)
- Log output: By default, log messages are written to the standard error (
os.Stderr) stream. However, you can use theSetOutputfunction to redirect log messages to a different output stream. For example, to write log messages to a file calledapko.log, you would use the following code:
f, err := os.OpenFile("apko.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
log.SetOutput(f)
- Log format: The
pkg/logpackage supports several different log formats, including plain text and JSON. The log format can be set using theSetFlagsfunction. For example, to use the JSON format, you would use the following code:
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.SetOutput(os.Stdout)
log.SetPrefix("{\"time\":\"")
log.SetFormatter(&log.JSONFormatter{})
- Log fields: In addition to the log message itself, you can include additional fields in the log message to provide more context. The
pkg/logpackage supports structured logging using theWithfunction. For example, to log anINFOmessage with afilefield, you would use the following code:
log.WithField("file", "/app/go").Info("Opening dir")
For more information on the pkg/log package, see the official Go documentation: https://pkg.go.dev/log
Sources:
- https://edu.chainguard.dev/open-source/melange/overview
- https://grafana.com/blog/2021/05/13/how-to-correlate-graphite-metrics-and-loki-logs
- https://docs.gitlab.com/ee/development/go_guide
- https://developer.hashicorp.com/vault/api-docs/system/loggers
- https://opensource.com/article/17/9/python-logging
- https://edu.chainguard.dev/open-source/sigstore/rekor/how-to-verify-file-signatures-with-rekor-or-curl
- https://sweetcode.io/apm-log-management-logging-monitoring-different-why-need-both
- https://piotrminkowski.com/2023/02/13/best-practices-for-java-apps-on-kubernetes
- https://backstage.io/docs/reference/plugin-airbrake-backend.routeroptions.logger
- https://sweetcode.io/apm-log-management-logging-monitoring-different-why-need-both
- https://grafana.com/docs/plugins/grafana-splunk-datasource/latest
- https://karmada.io/docs/reference/components/karmada-metrics-adapter
- https://edu.chainguard.dev/chainguard/chainguard-images/reference/argo-cli/tags_history