Skip to content

goloop/log

Repository files navigation

Go Report Card License License Stay with Ukraine

log

log is a leveled, multi-output logging package for Go. A single log call fans out to every configured output, and each output independently decides which levels it accepts, whether it renders text or JSON, whether it uses colour, and how the message prefix is laid out.

It is safe for concurrent use, and work is skipped when nobody needs it: level filtering happens at the source, and stack-frame capture and formatting run only when at least one output requires them, so silent levels stay cheap.

Features

  • Seven levelsPanic, Fatal, Error, Warn, Info, Debug, Trace, each with plain / …f / …ln forms.
  • Multiple outputs — console, files and custom writers at once, each with per-output level filtering, text or JSON, ANSI colour and a configurable prefix layout.
  • Thread-safe — mutex-protected, with buffer pooling on the hot path.
  • slog bridge — back the standard log/slog with NewSlog or logger.Handler().
  • Ad-hoc writers — the F-family also tees a message to a one-off writer.
  • ObservabilityEnabled guards expensive work; SetErrorHandler surfaces failing outputs.

Installation

go get -u github.com/goloop/log/v2
import "github.com/goloop/log/v2"

Requires Go 1.24 or newer.

Quick start

package main

import "github.com/goloop/log/v2"

func main() {
    logger := log.New("APP")

    logger.Info("Application started")
    logger.Infof("User %s logged in", "bob")
    logger.Errorln("Failed to connect to database")
}

Fan out to a coloured console and a JSON file, each filtered by level:

import (
    "github.com/goloop/log/v2"
    "github.com/goloop/log/v2/layout"
    "github.com/goloop/log/v2/level"
)

log.SetOutputs(
    log.Output{
        Name:      "console",
        Writer:    os.Stdout,
        Levels:    level.Info | level.Warn | level.Error,
        Layouts:   layout.Default,
        WithColor: 1,
        TextStyle: 1,
    },
    log.Output{
        Name:      "file",
        Writer:    file,
        Levels:    level.Error | level.Fatal,
        TextStyle: -1, // JSON
    },
)

log.Info("System initialized")
log.Error("Database connection failed")

Back the standard log/slog:

slogger := log.NewSlog("APP")
slogger.Info("user logged in", "user", "bob", "id", 42)

Documentation

Contributing

Contributions are welcome. Please run go test ./..., go vet ./... and gofmt -l . before submitting a pull request.

License

log is released under the MIT License. See LICENSE.

About

The log is go-module that implements functions for logging using different levels: DEBUG, INFO, WARNING, ERROR, FATAL, TRACE.

Topics

Resources

License

Code of conduct

Contributing

Stars

4 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages