embedlog

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 12 Imported by: 4

README

embedlog

Linter Status Go Report Card Go Reference

Enhanced logging library for Go with Prometheus metrics integration and structured logging capabilities.

Features
  • Dual-level logging: Automatically splits logs between stdout (info) and stderr (errors)
  • Flexible output: Supports both JSON and text formats
  • Simple API: Familiar Print/Error style interface with context support
  • Embeddable: Designed to be embedded in your application structures
  • Prometheus integration: Built-in metrics for log events (app_log_events_total)
  • Source location: Automatic file/line logging for better debugging
  • PrintOrEr: Function for conditional logging.
  • NewDevLogger: Colored logger for development.
Custom Metrics Integration

The library automatically exposes Prometheus metrics:

  • app_log_events_total{type="info"} - Count of info-level logs
  • app_log_events_total{type="error"} - Count of error-level logs

Use these metrics to set up alerts for error rate spikes.

Best Practices
  • For services: Use JSON format in production
  • For CLI tools: Use text format with verbose flag
  • For error tracking: Monitor app_log_events_total{type="error"}
Quick Start

Please, see:

  • examples/main.go for basic usage.
  • examples/dblog.go for go-pg usage.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

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

Logger is a struct for embedding std loggers.

func NewDevLogger

func NewDevLogger() Logger

NewDevLogger returns colored dev logger.

func NewLogger

func NewLogger(verbose, isJSON bool) Logger

NewLogger returns new Logger wrapper for slog. verbose sets slog.LevelInfo instead of slog.LevelError. isJson uses slog.JSONHandler instead of slog.TextHandler.

func (Logger) Error

func (l Logger) Error(ctx context.Context, msg string, args ...any)

Error logs at slog.LevelError with the given context.

func (Logger) Errorf

func (l Logger) Errorf(format string, v ...any)

Errorf logs at slog.LevelError with the given context with fmt.Sprintf.

func (Logger) Log

func (l Logger) Log() *slog.Logger

Log is a function that returns underlying slog.Logger.

func (Logger) Print

func (l Logger) Print(ctx context.Context, msg string, args ...any)

Print logs at slog.LevelInfo with the given context.

func (Logger) PrintOrErr

func (l Logger) PrintOrErr(ctx context.Context, msg string, err error, args ...any)

PrintOrErr logs an Error if err is not nil, otherwise logs an Info message with args.

func (Logger) Printf

func (l Logger) Printf(format string, v ...any)

Printf logs at slog.LevelInfo with the given context with fmt.Sprintf.

func (Logger) With

func (l Logger) With(args ...any) Logger

With returns a Logger that includes the given attributes in each output operation. Arguments are converted to attributes as if by Logger.Log.

func (Logger) WithGroup

func (l Logger) WithGroup(name string) Logger

WithGroup returns a Logger that starts a group, if name is non-empty. The keys of all attributes added to the Logger will be qualified by the given name. (How that qualification happens depends on the [Handler.WithGroup] method of the Logger's Handler.)

If name is empty, WithGroup returns the receiver.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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