go-logx is a leveled Go logger with JSON and key/value text formatters. It also
provides adapters for the standard library's log/slog package.
- Requirements
- Installation
- Quick start
- Configuration
- Formatters
- slog adapters
- Default logger
- Development
- Contributing
- License
- Go 1.26 or later.
go get go.osspkg.com/logxpackage main
import (
"os"
"go.osspkg.com/logx"
)
func main() {
logger := logx.New(
logx.WithOutput(os.Stdout),
logx.WithLevel(logx.LevelInfo),
)
logger.Info("server started", "address", ":8080", "port", 8080)
}The default formatter is JSON. This call writes one JSON record followed by a newline:
{"time":"2026-10-10T12:00:00Z","level":"INFO","msg":"server started","ctx":{"address":":8080","port":"8080"}}New accepts options for the minimum log level, output writer, and formatter.
The default level is LevelError, and the default writer is standard output.
Available levels, from the least restrictive to the most restrictive, are
LevelDebug, LevelInfo, LevelWarn, and LevelError. A record is written
when its level meets the configured threshold. Fatal writes a fatal record
and then exits the process with status 1.
Context is supplied as alternating key/value arguments:
logger.Info("request handled", "method", "GET", "status", 200)For the JSON formatter, context values are represented as strings. If there
are no context pairs, the ctx field is omitted. Context keys are JSON object
fields, so their order is unspecified.
Loggers serialize writes to their configured writer. To use a different formatter, pass it at construction time:
logger := logx.New(
logx.WithLevel(logx.LevelDebug),
logx.WithFormatter(logx.NewFormatString()),
)The built-in formatters are:
NewFormatJSON()writes JSON records.NewFormatString()writes quotedkey=valuefields separated by tabs.NewFormatString(logx.WithDelimiter('|'))selects another single-byte delimiter.
Applications can provide a custom formatter by implementing logx.Formatter:
import (
"io"
"go.osspkg.com/logx"
)
type MessageOnlyFormatter struct{}
func (MessageOnlyFormatter) Encode(w io.Writer, m *logx.Message) error {
_, err := io.WriteString(w, m.Message+"\n")
return err
}Use the standard library's JSON or text handler with the same logger options:
package main
import (
"os"
"go.osspkg.com/logx"
)
func main() {
logger := logx.NewSLogJsonAdapter(
logx.WithOutput(os.Stdout),
logx.WithLevel(logx.LevelInfo),
)
logger.Info("worker ready", "worker", 3)
}NewSLogStringAdapter selects slog text output. Passing WithFormatter
instead routes the adapter through the supplied logx.Formatter.
The package-level functions use a default logger initialized with JSON output
and the LevelError threshold. Select another logger during startup with
SetDefault:
logx.SetDefault(logx.New(logx.WithLevel(logx.LevelInfo)))
logx.Info("application started", "version", "1.2.3")Call SetDefault before starting concurrent logging.
Run the package tests from the repository root:
go test ./...Run the full project checks with:
make cimake ci installs the repository's goppy tooling and runs license, lint,
test, and build checks. GitHub Actions runs this command on Linux and runs the
Go test suite natively on macOS and Windows.
Report bugs or request changes through GitHub Issues. For code changes, open a pull request and include tests for changed behavior.
BSD 3-Clause. See LICENSE.