prezerolog

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2025 License: MIT Imports: 16 Imported by: 0

README

Pre-zerolog (prezerolog)

Go Reference License Release

A logging wrapper around zerolog with:

  • JSON logs to rotated files (optional to STDOUT for use to ingest)
  • Colored console output in development/DEBUG
  • Normalized keys: time, level, msg, service, env, trace_id, span_id, request_id, caller, err
  • RFC3339Nano timestamps (UTC)
  • Context-aware correlation IDs
  • Minimal API for consistency across services

Install

go get github.com/renegadevi/prezerolog

Quick start

package main

import (
	"context"
	"errors"

	log "github.com/renegadevi/prezerolog"
)

func main() {
	// Initialize logging (loads .env if present, sets up rotation & console)
	log.InitLogging()

	// Logging exmaples (check example file for more)
	log.Info("Logging initialized")
	log.Warn("This is a warning message")
	log.Error("This is an error message", errors.New("example error"))
	log.Trace("This is a trace message", map[string]any{"key": "value"})
	log.Debug("This is a debug message", map[string]any{"debug": true})

}

Screenshots

screenshot-env screenshot-json screenshot-file

Log files & rotation

By default, logs are written to LOG_DIR (default: logs/) as JSON. Each run uses a date-based base name and a zero-padded index that increments on rotation.

Fresh start:

logs/
└─ my-service_2025-08-10T15-10-22_00.log

After a few rotations:

logs/
├─ my-service_2025-08-10T15-10-22_00.log
├─ my-service_2025-08-10T15-10-22_01.log
└─ my-service_2025-08-10T15-10-22_02.log

Where:

  • my-service_YYYY-MM-DDTHH-MM-SS = base name for the current process start
  • _00.log = the first file for this run; subsequent files increment to _01.log, _02.log, …

Rotation is controlled by environment variables:

  • LOG_ROTATE_MAX_SIZE (MB, default 100)
  • LOG_ROTATE_MAX_BACKUPS (default 7, older files are removed beyond this count)
  • Files are compressed (.gz) automatically when rotated.

Example JSON line:

{
  "time":"2025-08-10T13:45:15.123456789Z",
  "level":"info",
  "msg":"login",
  "service":"my-service",
  "env":"development",
  "trace_id":"tr-9a22",
  "span_id":"sp-001",
  "request_id":"req-7c1b",
  "caller":"auth.go:88",
  "user":"john",
  "role":"admin"
}

Keys

All records include:

time, level, msg, service, env, caller

Optional correlation IDs (when using *Ctx funcs and a context carrying values):

trace_id, span_id, request_id

Errors passed as error args are logged under err.

Environment variables

LOG_DIR=<folder name>                          # (default: "logs")
LOG_NAME=<service name>                        # (default: current dir)
LOG_ENV=production|development                 # (default: production)
LOG_FILE=true|false                            # (default: true)
LOG_CONSOLE=true|false                         # (default: true)
LOG_CONSOLE_LEVEL=trace|debug|info|warn        # (default: info)
LOG_FILE_LEVEL=trace|debug|info|warn           # (default: info)
LOG_CONSOLE_OUTPUT=minimal|full|extended|json  # (default: full)
LOG_SAMPLING_N=1                               # (default: 1)
LOG_ROTATE_MAX_SIZE=100                        # (default: 100)
LOG_ROTATE_MAX_BACKUPS=7                       # (default: 7)

Example of Fatal error messages.

// Generic failure (defaults to 1)
log.Fatal("unrecoverable error", err)

License

MIT 2025 - Philip Andersen

Documentation

Index

Constants

View Source
const (
	CtxRequestID ctxKey = "request_id"
	CtxTraceID   ctxKey = "trace_id"
	CtxSpanID    ctxKey = "span_id"
)

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

func DebugCtx

func DebugCtx(ctx context.Context, args ...interface{})

func Error

func Error(args ...interface{})

func ErrorCtx

func ErrorCtx(ctx context.Context, args ...interface{})

func Fatal added in v0.3.0

func Fatal(args ...interface{})

func FatalCode added in v0.2.0

func FatalCode(code int, args ...interface{})

func Info

func Info(args ...interface{})

func InfoCtx

func InfoCtx(ctx context.Context, args ...interface{})

func InitLogging

func InitLogging()

func Trace added in v0.3.0

func Trace(args ...interface{})

Simple variants

func TraceCtx added in v0.3.0

func TraceCtx(ctx context.Context, args ...interface{})

Context-aware variants

func Warn

func Warn(args ...interface{})

func WarnCtx

func WarnCtx(ctx context.Context, args ...interface{})

Types

type FatalError

type FatalError struct {
	Message string
	Code    int
	File    string
}

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

---------- Public types ----------

var AppLogger *Logger

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

func (*Logger) DebugCtx

func (l *Logger) DebugCtx(ctx context.Context, args ...interface{})

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

func (*Logger) ErrorCtx

func (l *Logger) ErrorCtx(ctx context.Context, args ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

Fatal logs an error, includes a stack trace in development, and exits.

func (*Logger) GetCurrentLogFile

func (l *Logger) GetCurrentLogFile() string

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

func (*Logger) InfoCtx

func (l *Logger) InfoCtx(ctx context.Context, args ...interface{})

func (*Logger) Shutdown

func (l *Logger) Shutdown()

func (*Logger) Trace added in v0.3.0

func (l *Logger) Trace(args ...interface{})

func (*Logger) TraceCtx added in v0.3.0

func (l *Logger) TraceCtx(ctx context.Context, args ...interface{})

func (*Logger) Warn

func (l *Logger) Warn(args ...interface{})

func (*Logger) WarnCtx

func (l *Logger) WarnCtx(ctx context.Context, args ...interface{})

type RotatingLogger

type RotatingLogger struct {
	*lumberjack.Logger
}

func NewRotatingLogger

func NewRotatingLogger(logDir string) *RotatingLogger

Directories

Path Synopsis
examples
basic command
json command
examples/json/main.go
examples/json/main.go

Jump to

Keyboard shortcuts

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