discard

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 6 Imported by: 17

README

discard

Go Reference codecov

No-op logging handler for darvaza.org/slog. Discards all log entries except Fatal and Panic levels, perfect for testing and optional logging scenarios.

Installation

go get darvaza.org/slog/handlers/discard

Quick Start

// Create a discard logger
logger := discard.New()

// Use anywhere a logger is needed
service := NewService(logger) // Won't produce any output

// Safe to use - never returns nil
logger.Debug().WithField("data", complexObject).Print("This is discarded")
logger.Info().Print("This too")

// Fatal and Panic still work as expected
logger.Fatal().Print("This will exit") // Calls log.Fatal()
logger.Panic().Print("This will panic") // Calls log.Panic()

Features

  • Zero overhead for disabled log levels
  • Immutable logger instances for safe concurrent use
  • Always returns a valid logger (never nil)
  • Fatal and Panic levels still trigger appropriate exits

Use Cases

  • Testing: Silence logs during test execution
  • Optional logging: When a component doesn't require logging
  • Benchmarking: Remove logging overhead from performance tests
  • Default logger: Safe placeholder before configuration

Documentation

Documentation

Overview

Package discard is a Logger that doesn't really log anything

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() slog.Logger

New creates a slog.Logger that doesn't really log anything

Types

type Logger

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

Logger implements slog.Logger but doesn't log anything

func (*Logger) Debug

func (nl *Logger) Debug() slog.Logger

Debug pretends to return a new NOOP logger

func (*Logger) Enabled

func (nl *Logger) Enabled() bool

Enabled tells that we only handle Fatal and Panic.

func (*Logger) Error

func (nl *Logger) Error() slog.Logger

Error pretends to return a new NOOP logger

func (*Logger) Fatal

func (nl *Logger) Fatal() slog.Logger

Fatal return a new Fatal logger

func (*Logger) Info

func (nl *Logger) Info() slog.Logger

Info pretends to return a new NOOP logger

func (*Logger) Level added in v0.6.2

func (nl *Logger) Level() slog.LogLevel

Level returns the current log level. Exposed for testing only.

func (*Logger) Panic

func (nl *Logger) Panic() slog.Logger

Panic return a new Panic logger

func (*Logger) Print

func (nl *Logger) Print(args ...any)

Print pretends to add a log entry with arguments handled in the manner of fmt.Print

func (*Logger) Printf

func (nl *Logger) Printf(format string, args ...any)

Printf pretends to add a log entry with arguments handled in the manner of fmt.Printf

func (*Logger) Println

func (nl *Logger) Println(args ...any)

Println pretends to add a log entry with arguments handled in the manner of fmt.Println

func (*Logger) Warn

func (nl *Logger) Warn() slog.Logger

Warn pretends to return a new NOOP logger

func (*Logger) WithEnabled

func (nl *Logger) WithEnabled() (slog.Logger, bool)

WithEnabled passes the logger, but also indicates if it's enabled or not. This logger is only enabled for Fatal entries

func (*Logger) WithField

func (nl *Logger) WithField(_ string, _ any) slog.Logger

WithField pretends to add a fields to the Logger

func (*Logger) WithFields

func (nl *Logger) WithFields(_ map[string]any) slog.Logger

WithFields pretends to add fields to the Logger

func (*Logger) WithLevel

func (nl *Logger) WithLevel(level slog.LogLevel) slog.Logger

WithLevel pretends to return a new logger set to add entries to the level.

func (*Logger) WithStack

func (nl *Logger) WithStack(skip int) slog.Logger

WithStack pretends to attach a call stack to the logger

Jump to

Keyboard shortcuts

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