sloggcp
provides a simple implementation of the ReplaceAttr
function for JSONHandler
from slog.
This implementation adapts the default slog attributes to be compatible with Google Cloud Platform's Structured Logging.
By using sloggcp
, you can ensure correct log severity display in the GCP Logs Viewer
and proper representation of other attributes.
It is applicable to any GCP service that delivers logs via Logging agent.
For example, CloudRun, etc.
package main
import (
"github.com/vlad-tokarev/sloggcp"
"log/slog"
"os"
)
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
ReplaceAttr: sloggcp.ReplaceAttr,
AddSource: true,
Level: slog.LevelDebug,
}))
slog.SetDefault(logger)
}