Skip to content

Latest commit

 

History

History
41 lines (27 loc) · 1004 Bytes

README.md

File metadata and controls

41 lines (27 loc) · 1004 Bytes

sloggcp

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.

Usage

Override default attributes

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)
}