Categorygithub.com/rvflash/logm
modulepackage
0.2.3
Repository: https://github.com/rvflash/logm.git
Documentation: pkg.go.dev

# README

LogM

GoDoc Build Status Code Coverage Go Report Card

LogM is a Go log management package providing facilities to deal with logs and trace context.

Features

  1. Provides simple methods to expose a slog.Logger using logfmt as format:
    • DefaultLogger: A logger ready for production.
    • DebugLogger: A logger exposing debug record for development.
    • DiscardLogger: Another to discard any logs (test purposes or no space left on disk).
  2. Provides a File with automatic rotating, maximum file size, zip archives, etc. Thanks to lumberjack.
  3. Provides a Trace structure to uniquely identified actions, like an HTTP request. See NewTraceFromContext to easily propagate or retrieve trace context.
  4. Exposes HTTP middlewares to handle log and tracing:to create a trace context on each request.
    • LogHandler: a logging middleware to log detail about the request and the response.
    • RecoverHandler: a middleware to recover on panic, log the message as ERROR and the stack trace as DEBUG.
    • TraceHandler: a middleware to retrieve request header X-Trace-Id (see NewTraceFromHTTPRequest) and propagate its value through the request context.
  5. Provides TimeElapsed to log in defer the time elapsed of a function.
  6. Offers a testing sub-package named logmtest to verify the data logged.

Installation

$ go get -u github.com/rvflash/logm

Prerequisite

logm uses the Go modules, debug.BuildSetting and the slog package that required Go 1.18 or later.

Exemples

Create a logger for an application named app that store records in a file named app.log, ignoring DEBUG ones.

By default, the NewFile function returns a self rolling file as soon as it reaches the size of 100 Mo. The rotated log files is compressed using gzip and retained forever. Each log is prefixed by the local time. Customization is available by using File directly. By default, each record has the name and the current version of the application as attributes.

The version is provided on build by the debug.ReadBuildInfo package. It's the revision identifier for the current commit or checkout.

log := logm.DefaultLogger("app", logm.NewFile("app.log"))
log.Info("hello")
$ cat app.log
time=2023-03-25T00:04:35.287+01:00 level=INFO msg=hello app=app version=d1da844711730f2f5cbd08be93e62e71475f7d4e

Create a logger to debug on standard output.

log := logm.DebugLogger("app", os.Stdout)
log.Debug("hello")
time=2023-03-25T10:57:51.772+01:00 level=DEBUG msg=hello app=app version=d1da844711730f2f5cbd08be93e62e71475f7d4e

Propagate a trace identifier through the context.

NewTrace can create a new trace context with an UUID v4 as identifier. It's also possible to create a custom one by directly using Trace and propagate it through a context.Context.

var (
    t   = logm.Trace{ID: "myID"}
    ctx = t.NewContext(context.Background())
)

Add the trace context on each log.

var (
    l = logm.DefaultLogger("app", os.Stdout)
    t = logm.NewTrace()
)
log := l.With(t.LogAttr())
log.Info("hello")
log.Warn("world")
time=2023-03-25T13:06:37.322+01:00 level=INFO msg=hello app=app version=d1da844711730f2f5cbd08be93e62e71475f7d4e trace.id=0a02e16c-7418-4558-9dcc-718c007162b6
time=2023-03-25T13:06:37.322+01:00 level=WARN msg=world app=app version=d1da844711730f2f5cbd08be93e62e71475f7d4e trace.id=0a02e16c-7418-4558-9dcc-718c007162b6

Monitor the time elapsed by a function on defer.

var (
    log = logm.DefaultLogger("app", os.Stdout)
    ctx = context.Background()
)
func(ctx context.Context, log *slog.Logger) {
    defer logm.TimeElapsed(ctx, log, slog.LevelInfo, "example")()
    time.Sleep(time.Millisecond)
}(ctx, log)
time=2023-03-25T12:06:17.605+01:00 level=INFO msg=example app=app version=d1da844711730f2f5cbd08be93e62e71475f7d4e trace.id=ccc05db1-68d2-4442-9353-0789e0b8ca55 trace.time_elapsed_ms=1

Test whether the data is logged in order and contains expected contents.

Testing the logged data sometimes seems useless, but it can be reassuring to quickly check a stream to preserve. The logmtest.NewRecorder provides an in-memory log writer useful for that, with a method Expect to check each Record. Two options to adjust the verification:

  • ExpectAnyOrder to match all expectations in the order they were set or not. By default, it expects in order.
  • ExpectUnexpected to ignore not matching records. By default, if a record is not expected, an error will be triggered.
var (
    rec = logmtest.NewRecorder()
    log = logm.DefaultLogger("testing", rec)
)
log.Info("hello")
log.Warn("beautiful")
log.Error("world")

err := rec.Expect(
    logmtest.Record{Contains: "hello"},
    logmtest.Record{Contains: "beau"},
    logmtest.Record{Contains: "world"},
)
// Will return no error.

# Packages

Package logmtest provides utilities for Log testing.

# Functions

DebugLogger returns a new instance of Logger dedicated to debug or test environment.
DefaultLogger returns a new instance of Logger, ready to be use in production mode.
DiscardLogger is a logger doing anything.
LogHandler is an HTTP middleware designed to log every request and response.
NewFile returns a file with this name.
NewLogger returns a new instance of Logger where the level is the minimum log level to consider.
NewTrace creates a new Trace with a new generated UUID v4 as identifier.
NewTraceFromContext returns a new Trace based on the context.Context.
NewTraceFromHTTPRequest returns a new Trace based on the http.Request.
NewTraceSpan creates a trace span based on the Trace.
RecoverHandler is an HTTP middleware designed to recover on panic, log the error and debug the stack trace.
TimeElapsed allows to monitor the execution time of a function.
TraceHandler is an HTTP middleware designed to share the trace context in the request context.

# Constants

AppNameKey is the name of the application in structured log.
AppVersionKey is the version of the application in structured log.
HTTPMethodKey is the HTTP request method in structured log.
HTTPPathKey is the HTTP request path in structured log.
HTTPQueryKey is the HTTP request query in structured log.
HTTPRemoteAddrKey is the HTTP remote address in structured log.
HTTPRequestKey is the HTTP request name in structured log.
HTTPResponseKey is the HTTP response name in structured log.
HTTPSizeKey is the HTTP response size in structured log.
HTTPStatusKey is the HTTP response status in structured log.
PanicKey is a panic in structured log.
TraceIDHTTPHeader is the name of the HTTP header used to share a trace context ID.
TraceIDKey is the name of the trace ID in structured log.
TraceKey is the name of the trace in structured log.
TraceSpanIDKey is the name of the trace span ID in structured log.
TraceTimeElapsedKey is the name of the trace time in structured log.

# Structs

Middleware provides some standard HTTP handlers to deal with logs.
Trace represents a trace context.

# Type aliases

File is a log file.