glog

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 10 Imported by: 1

README

glog

A simple slog wrapper with Google Cloud Logging support.

Features

  • Structured JSON logging compatible with Google Cloud Logging
  • slog-based API with context support
  • Automatic source location for debug and error levels
  • Global extra fields support

Usage

import "github.com/twopow/glog"

// Initialize logger - sets the default logger
logger := glog.NewLogger("info")

// Use package-level functions (default logger)
glog.Info("application started")
glog.Error("something went wrong", "error", err)

// Or use the returned logger directly
logger.Info("using logger directly")

// With context
ctx := glog.WithLogger(context.Background(), logger)
glog.InfoContext(ctx, "handling request", "method", "GET")

Installation

go get github.com/twopow/glog

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, args ...any)

Debug calls [Logger.Debug] on the default logger.

func DebugContext

func DebugContext(ctx context.Context, msg string, args ...any)

DebugContext calls [Logger.DebugContext] on the default logger.

func Discard

func Discard() *slog.Logger

Discard creates a logger that discards all output (useful for testing)

func Error

func Error(msg string, args ...any)

Error calls [Logger.Error] on the default logger.

func ErrorContext

func ErrorContext(ctx context.Context, msg string, args ...any)

ErrorContext calls [Logger.ErrorContext] on the default logger.

func FromContext

func FromContext(ctx context.Context) *slog.Logger

FromContext retrieves the logger from context

func GetLogger added in v0.1.3

func GetLogger() *slog.Logger

GetLogger returns the global glog logger

func Info

func Info(msg string, args ...any)

Info calls [Logger.Info] on the default logger.

func InfoContext

func InfoContext(ctx context.Context, msg string, args ...any)

InfoContext calls [Logger.InfoContext] on the default logger.

func Log

func Log(ctx context.Context, level slog.Level, msg string, args ...any)

Log calls [Logger.Log] on the default logger.

func LogAttrs

func LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)

LogAttrs calls [Logger.LogAttrs] on the default logger.

func MergeGlobalExtraFields

func MergeGlobalExtraFields(extraFields map[string]interface{})

MergeGlobalExtraFields merges extra fields into the global extra fields

func NewLogger

func NewLogger(level string) *slog.Logger

Init initializes the logger with GCP-friendly format

func SetLogger added in v0.1.3

func SetLogger(l *slog.Logger)

SetLogger sets the global glog logger

func SetSourceLevels added in v0.1.2

func SetSourceLevels(levels []slog.Level)

SetSourceLevels sets the source levels for the logger this only has effect if the logger is not already initialized

func Warn

func Warn(msg string, args ...any)

Warn calls [Logger.Warn] on the default logger.

func WarnContext

func WarnContext(ctx context.Context, msg string, args ...any)

WarnContext calls [Logger.WarnContext] on the default logger.

func With

func With(args ...any) *slog.Logger

With calls [Logger.With] on the default logger.

func WithAttrs

func WithAttrs(attrs ...any) *slog.Logger

WithAttrs creates a new logger with added attributes (useful for context)

func WithGroup

func WithGroup(name string) *slog.Logger

WithGroup creates a new logger with a group of attributes

func WithLogger

func WithLogger(ctx context.Context, logger *slog.Logger) context.Context

WithLogger adds a logger to the context

Types

type GCPHandler

type GCPHandler struct {
	// contains filtered or unexported fields
}

GCPHandler is a slog.Handler that formats logs for GCP Cloud Logging

func NewGCPHandler

func NewGCPHandler(w io.Writer, level slog.Level, sourceLevels []slog.Level) *GCPHandler

NewGCPHandler creates a new GCP Cloud Logging compatible handler

func (*GCPHandler) Enabled

func (h *GCPHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level

func (*GCPHandler) Handle

func (h *GCPHandler) Handle(ctx context.Context, r slog.Record) error

Handle formats and writes a log record

func (*GCPHandler) WithAttrs

func (h *GCPHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new handler with additional attributes

func (*GCPHandler) WithGroup

func (h *GCPHandler) WithGroup(name string) slog.Handler

WithGroup returns a new handler with a group prefix

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL