Logging - chainguard-dev/apko

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:

  1. Log level: The log level determines the severity of the log message. The pkg/log package supports the following log levels, in order of severity: DEBUG, INFO, WARN, ERROR, and FATAL. The log level can be set using the SetLevel function. For example, to set the log level to INFO, you would use the following code:
log.SetLevel(log.INFO)
  1. Log output: By default, log messages are written to the standard error (os.Stderr) stream. However, you can use the SetOutput function to redirect log messages to a different output stream. For example, to write log messages to a file called apko.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)
  1. Log format: The pkg/log package supports several different log formats, including plain text and JSON. The log format can be set using the SetFlags function. 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{})
  1. Log fields: In addition to the log message itself, you can include additional fields in the log message to provide more context. The pkg/log package supports structured logging using the With function. For example, to log an INFO message with a file field, 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: