logger

package module
v0.0.0-...-283ac43 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2025 License: MIT Imports: 7 Imported by: 36

README

Logger

Simple logger for Go built on log/slog.

Quick Start

import "github.com/shortlink-org/go-sdk/logger"

// Create logger
cfg := logger.Configuration{Level: logger.INFO_LEVEL}
log, _ := logger.New(cfg)

// Basic logging
log.Info("Server started", "port", 8080)
log.Error("Connection failed", "error", err)

// With context
ctx := context.Background()
log.InfoWithContext(ctx, "Request processed", "method", "GET")

API

// Core
logger.New(config) -> *SlogLogger

// Methods
log.Error(msg, fields...)
log.Warn(msg, fields...)
log.Info(msg, fields...)
log.Debug(msg, fields...)

// Context
log.ErrorWithContext(ctx, msg, fields...)
log.WarnWithContext(ctx, msg, fields...)
log.InfoWithContext(ctx, msg, fields...)
log.DebugWithContext(ctx, msg, fields...)

Configuration

type Configuration struct {
    Writer     io.Writer // default: os.Stdout
    TimeFormat string    // default: time.RFC3339Nano
    Level      int       // ERROR_LEVEL, WARN_LEVEL, INFO_LEVEL, DEBUG_LEVEL
}

Features

  • JSON structured logging
  • OpenTelemetry integration
  • Context support
  • High performance
  • Zero external dependencies

Documentation

Index

Constants

View Source
const (
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ERROR_LEVEL int = iota
	// WarnLevel level. Non-critical entries that deserve eyes.
	WARN_LEVEL
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	INFO_LEVEL
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DEBUG_LEVEL
)

The severity levels. Higher values are more considered more important.

Variables

View Source
var (
	// ErrInvalidLogLevel is an error when log level is invalid.
	ErrInvalidLogLevel = errors.New("invalid log level")
)

Functions

This section is empty.

Types

type Configuration

type Configuration struct {
	Writer     io.Writer
	TimeFormat string
	Level      int
}

Configuration - options for logger.

func Default

func Default() Configuration

Default returns a default configuration.

func (*Configuration) Validate

func (c *Configuration) Validate() error

type Logger

type Logger interface {
	Error(msg string, fields ...slog.Attr)
	ErrorWithContext(ctx context.Context, msg string, fields ...slog.Attr)

	Warn(msg string, fields ...slog.Attr)
	WarnWithContext(ctx context.Context, msg string, fields ...slog.Attr)

	Info(msg string, fields ...slog.Attr)
	InfoWithContext(ctx context.Context, msg string, fields ...slog.Attr)

	Debug(msg string, fields ...slog.Attr)
	DebugWithContext(ctx context.Context, msg string, fields ...slog.Attr)

	// Closer is the interface that wraps the basic Close method.
	io.Closer
}

Logger is our contract for the logger.

type SlogLogger

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

func New

func New(cfg Configuration) (*SlogLogger, error)

func (*SlogLogger) Close

func (log *SlogLogger) Close() error

func (*SlogLogger) Debug

func (log *SlogLogger) Debug(msg string, fields ...any)

func (*SlogLogger) DebugWithContext

func (log *SlogLogger) DebugWithContext(ctx context.Context, msg string, fields ...any)

func (*SlogLogger) Error

func (log *SlogLogger) Error(msg string, fields ...any)

func (*SlogLogger) ErrorWithContext

func (log *SlogLogger) ErrorWithContext(ctx context.Context, msg string, fields ...any)

func (*SlogLogger) Info

func (log *SlogLogger) Info(msg string, fields ...any)

func (*SlogLogger) InfoWithContext

func (log *SlogLogger) InfoWithContext(ctx context.Context, msg string, fields ...any)

func (*SlogLogger) Warn

func (log *SlogLogger) Warn(msg string, fields ...any)

func (*SlogLogger) WarnWithContext

func (log *SlogLogger) WarnWithContext(ctx context.Context, msg string, fields ...any)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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