Skip to content

log/slog default attributes adapter for Google Cloud Logging

License

Notifications You must be signed in to change notification settings

vlad-tokarev/sloggcp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages