log

package module
v0.0.0-...-444ca68 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2025 License: GPL-3.0 Imports: 12 Imported by: 55

README

log

A lightweight logging wrapper for zerolog, providing convenience functions and sensible defaults for Interline and Transitland projects.

Note: This library is primarily intended for internal use across Interline/Transitland repositories—mainly for our convenience and to avoid having zerolog.Xyz scattered throughout our codebase and copy-pasting logging middlewares. It's published publicly for our convenience; external users should probably just use zerolog directly.

Features

  • Simple printf-style logging functions (Infof, Debugf, Tracef, Errorf)
  • Structured logging via zerolog's fluent API
  • Pretty console output with colors (automatically disabled when piping/redirecting)
  • JSON output mode for production environments
  • Context-aware logging with request ID support
  • HTTP middleware for request logging

Installation

go get github.com/interline-io/log

Usage

Basic Logging
package main

import "github.com/interline-io/log"

func main() {
    // Printf-style logging
    log.Infof("Processing %d items", 42)
    log.Debugf("Debug info: %s", "details")
    log.Errorf("Something went wrong: %v", err)
    log.Tracef("Verbose trace: %+v", obj)

    // Structured logging (zerolog style)
    log.Info().Str("user", "alice").Int("count", 5).Msg("User action")
    log.Error().Err(err).Str("op", "save").Msg("Operation failed")

    // Simple print (no timestamp, ignores log level)
    log.Print("Plain output: %s", "hello")
}
Context-Aware Logging
// Get logger from context
logger := log.For(ctx)
logger.Info().Msg("Using context logger")

// Add logger to context
ctx = log.WithLogger(ctx, customLogger)
HTTP Middleware
import "github.com/interline-io/log"

// Add request ID to each request
r.Use(log.RequestIDMiddleware)

// Add request ID to context logger
r.Use(log.RequestIDLoggingMiddleware)

// Log request duration and details
r.Use(log.LoggingMiddleware(1000, getUserNameFunc))

Configuration

Environment Variables
Variable Description Default
TL_LOG Log level: TRACE, DEBUG, INFO, ERROR, FATAL INFO
TL_LOG_JSON Set to true for JSON output (no colors) false
Examples
# Enable debug logging
TL_LOG=DEBUG ./myapp

# Enable trace logging (most verbose)
TL_LOG=TRACE ./myapp

# JSON output for production/log aggregation
TL_LOG_JSON=true ./myapp
Terminal Colors

Console output includes ANSI colors when writing to a terminal. Colors are automatically disabled when output is piped or redirected:

# Colors enabled (terminal)
./myapp

# Colors disabled (piped)
./myapp | tee output.log

# Colors disabled (redirected)
./myapp > output.log 2>&1

Log Levels

Level Function Use Case
TRACE Tracef(), Trace() Verbose debugging, performance tracing
DEBUG Debugf(), Debug() Development debugging
INFO Infof(), Info() General operational messages
ERROR Errorf(), Error() Error conditions

License

See LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Logger = zerolog.New(os.Stderr).With().Timestamp().Logger().Level(zerolog.TraceLevel)

Functions

func Debug

func Debug() *zerolog.Event

func Debugf

func Debugf(fmts string, a ...interface{})

Debug for debugging messages.

func DurationLoggingMiddleware

func DurationLoggingMiddleware(longQueryDuration int, getUserName func(context.Context) string) func(http.Handler) http.Handler

Log request and duration Renamed from LoggingMiddleware

func Error

func Error() *zerolog.Event

func Errorf

func Errorf(fmts string, a ...interface{})

Error for notable errors.

func Fatal

func Fatal() *zerolog.Event

func For

func For(ctx context.Context) *zerolog.Logger

func GetReqID

func GetReqID(ctx context.Context) string

Re-export GetReqID for use outside the internal package

func Info

func Info() *zerolog.Event

func Infof

func Infof(fmts string, a ...interface{})

Info for regular messages.

func LoggingMiddleware

func LoggingMiddleware(longQueryDuration int, getUserName func(context.Context) string) func(http.Handler) http.Handler

Log request and duration

func Print

func Print(fmts string, args ...interface{})

Print - simple print, without timestamp, without regard to log level.

func RequestIDLoggingMiddleware

func RequestIDLoggingMiddleware(next http.Handler) http.Handler

Glue between chi RequestID and Zerolog

func RequestIDMiddleware

func RequestIDMiddleware(next http.Handler) http.Handler

Copy of chi request id middleware

func SetLevel

func SetLevel(lvalue zerolog.Level)

SetLevel sets the log level.

func Trace

func Trace() *zerolog.Event

func TraceCheck

func TraceCheck(fn func())

TraceCheck checks if the log level is trace before evaluating the anon fn

func Tracef

func Tracef(fmts string, a ...interface{})

Trace for debugging messages.

func Traceln

func Traceln(args ...interface{})

Traceln - prints to trace

func With

func With() zerolog.Context

func WithLogger

func WithLogger(ctx context.Context, logger zerolog.Logger) context.Context

Types

This section is empty.

Directories

Path Synopsis
cmd
testlog command
internal

Jump to

Keyboard shortcuts

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