logs

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2025 License: Apache-2.0 Imports: 6 Imported by: 2

README

logs

GitHub release (latest SemVer) GitHub Workflow Status GitHub GitHub go.mod Go version

A Go library that provides a simple interface and functions for logging.

Requirements

  • Go 1.24+

Installation

go get github.com/Siroshun09/logs

Usage

This library provides a common Logger interface and simple context-based utility functions. By default, it works with the standard log/slog package, and you can swap the output destination or the default logger to suit your needs.

Quick start
package main

import (
    "context"
    "errors"
    "github.com/Siroshun09/logs"
)

func main() {
    ctx := context.Background()

    // Log with the default logger (internally uses slog.Default)
    logs.Info(ctx, "hello")
    logs.Debug(ctx, "debug message") // whether this appears depends on slog's level settings

    // Log errors at warn/error levels
    logs.Warn(ctx, errors.New("something not critical"))
    logs.Error(ctx, errors.New("something went wrong"))
}
Get and set the default logger
package main

import (
    "github.com/Siroshun09/logs"
)

func main() {
    // Current default logger (when SetDefault is not called, it's based on slog.Default)
    logger := logs.Default()

    // Set any Logger as the default (re-setting the same here as an example)
    logs.SetDefault(logger)
}
Integrate with slog

You can pass a slog.Logger and use it as this library's Logger.

package main

import (
    "context"
    "errors"
    "log/slog"
    "os"
    "github.com/Siroshun09/logs"
)

func main() {
    ctx := context.Background()

    // Prepare slog with a JSON handler
    h := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})
    s := slog.New(h)

    // Convert to this library's Logger and set as default
    logs.SetDefault(logs.NewLoggerWithSlog(s))

    logs.Info(ctx, "info message")
    logs.Warn(ctx, errors.New("warn!"))
}

If you pass nil to logs.NewLoggerWithSlog(nil), it uses slog.Default().

Simple writer-based logger

You can also use a simple plain-text Logger that writes to standard output/error.

package main

import (
    "context"
    "os"
    "github.com/Siroshun09/logs"
)

func main() {
    ctx := context.Background()

    // Write INFO and above to standard output.
    // Arguments: NewLoggerWithWriter(writer, printLevel, debug)
    l := logs.NewLoggerWithWriter(os.Stdout, true /*printLevel*/, true /*debug*/)
    logs.SetDefault(l)

    logs.Debug(ctx, "debug visible") // printed because debug=true
    logs.Info(ctx, "info message")   // prints like: "INFO: info message"
}

Helpers are also provided:

  • logs.NewStdoutLogger(debug bool)
  • logs.NewStderrLogger(debug bool)
Switch logger via context

If you want to temporarily swap the logger per request or operation, use WithContext/FromContext or the package-level functions.

package main

import (
    "context"
    "github.com/Siroshun09/logs"
)

func handler(ctx context.Context) {
    // Use a different logger only for this logic
    l := logs.NewStdoutLogger(true)
    ctx = logs.WithContext(ctx, l)

    // Package-level functions internally call FromContext(ctx)
    logs.Info(ctx, "in handler")
}

func main() {
    handler(context.Background())
}

FromContext(ctx) returns logs.Default() when no logger is attached to the context.

License

This project is under the Apache License version 2.0. Please see LICENSE for more info.

Copyright © 2024-2025, Siroshun09

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, msg string)

Debug logs a message as debug level using Logger obtained from FromContext.

func Error

func Error(ctx context.Context, err error)

Error logs an error as error level using Logger obtained from FromContext.

func Errorf added in v1.2.0

func Errorf(ctx context.Context, format string, args ...any)

Errorf logs an error as error level using Logger obtained from FromContext.

func Info

func Info(ctx context.Context, msg string)

Info logs a message as info level using Logger obtained from FromContext.

func SetDefault

func SetDefault(logger Logger)

SetDefault makes Default return the specified Logger.

If the given Logger is nil, this function triggers a panic.

func Warn

func Warn(ctx context.Context, err error)

Warn logs an error as warn level using Logger obtained from FromContext.

func Warnf added in v1.2.0

func Warnf(ctx context.Context, format string, args ...any)

Warnf logs an error as warn level using Logger obtained from FromContext.

func WithContext

func WithContext(ctx context.Context, logger Logger) context.Context

WithContext puts the Logger into context.Context.

Types

type Logger

type Logger interface {
	// Debug logs a message as debug level.
	Debug(ctx context.Context, msg string)
	// Info logs a message as info level.
	Info(ctx context.Context, msg string)
	// Warn logs an error as warn level.
	Warn(ctx context.Context, err error)
	// Warnf logs an error as warn level.
	Warnf(ctx context.Context, format string, args ...any)
	// Error logs an error as error level.
	Error(ctx context.Context, err error)
	// Errorf logs an error as error level.
	Errorf(ctx context.Context, format string, args ...any)
}

Logger includes functions to log messages or errors.

func Default

func Default() Logger

Default returns a Logger set by SetDefault, or default Logger instance that uses slog.Default.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext takes the Logger from context.Context.

If the given context.Context does not have the Logger, this function returns Default.

func NewLoggerWithSlog

func NewLoggerWithSlog(logger *slog.Logger) Logger

NewLoggerWithSlog creates Logger using slog.Logger.

func NewLoggerWithWriter added in v1.1.0

func NewLoggerWithWriter(writer io.Writer, printLevel bool, debug bool) Logger

NewLoggerWithWriter creates a Logger that writes to the given io.Writer.

func NewStderrLogger added in v1.1.0

func NewStderrLogger(debug bool) Logger

NewStderrLogger creates a Logger that writes to os.Stderr.

func NewStdoutLogger added in v1.1.0

func NewStdoutLogger(debug bool) Logger

NewStdoutLogger creates a Logger that writes to os.Stdout.

Directories

Path Synopsis
logmock module

Jump to

Keyboard shortcuts

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