vlad-tokarev / sloggcp

log/slog default attributes adapter for Google Cloud Logging

Home Page:https://cloud.google.com/logging/docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

log/slog default attributes adapter for Google Cloud Logging

https://cloud.google.com/logging/docs

License:MIT License


Languages

Language:Go 100.0%