Skip to content

Repository files navigation

Go Structured Logger

Go Reference Go Report Card License Release

A structured logging package for Go applications built on top of the standard library's slog package.

Features

  • Built on Go's standard library: Uses slog from Go 1.21+
  • Structured logging: Key-value pairs for better log filtering and analysis
  • Multiple output formats: JSON for production, text for development
  • Colored console output: Improves readability during local development
  • Customizable log levels: debug, info, warn, error
  • Source information: Automatically includes caller information (file:line)
  • Global logger: Convenient access throughout your application
  • Testing support: Mock logger for easy testing

Installation

go get github.com/legrch/logger

Quick Start

package main

import (
	"github.com/legrch/logger"
	"os"
)

func main() {
	// Initialize logger with default configuration
	cfg := &logger.Config{
		Level:           "info",
		Format:          "text",
		EnableColors:    true,
		EnableCaller:    true,
		CallerSkipFrame: 1,
	}
	
	log := logger.New(cfg)
	
	// Use the logger
	log.Info("application started", "version", "1.0.0")
	log.Debug("debug message") // Won't be shown with info level
	log.Error("something went wrong", "error", "connection refused", "retry", true)
	
	// With structured fields
	log.With("request_id", "abc123").Info("processing request")
	
	// Set as global logger
	logger.SetGlobal(log)
	
	// Use global logger elsewhere
	globalLogger := logger.Global()
	globalLogger.Info("using global logger")
}

Documentation

For detailed documentation, examples, and API reference, please visit:

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Logger is a structured logging package for Go, built on top of the slog package (Go 1.21+). It supports JSON and text formatting, colored console output, customizable log levels, and automatic caller info. Ideal for both development and production use.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages