logx

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

logx

Reusable structured logger for Go microservices, built on top of phuslu/log.

✨ Features

  • ✅ Minimal setup
  • 📁 File + console output support
  • 📦 Injects service metadata (service, env, version)
  • 🧠 Context-aware fields (trace_id, workflow_id, run_id)
  • 🧪 Simple, testable, and dependency-light

🚀 Quick Start

import (
  "context"
  "github.com/victor-robbin/logx"
)

func main() {
  logx.Init(logx.InitConfig{
    Service:     "worker",
    Environment: "prod",
    Version:     "v1.0.0",
    Level:       "info",
    LogToFile:   true,
    LogPath:     "/var/log/worker.log",
    MaxSizeMB:   10,
    MaxBackups:  5,
  })

  ctx := logx.WithTraceID(context.Background(), "trace-123")
  logx.InfoCtx(ctx, "task started", map[string]interface{}{"task": "email"})
}

🧩 Context Fields

Supports contextual injection of tracing fields:

  • trace_id
  • workflow_id
  • run_id

Example:

ctx := context.Background()
ctx = logx.WithRunID(ctx, "run-xyz")
ctx = logx.WithWorkflowID(ctx, "wf-abc")
ctx = logx.WithTraceID(ctx, "trace-123")

logx.ErrorCtx(ctx, "workflow failed", map[string]interface{}{"retries": 3})

🧪 Testing Helpers

Use TestWriter to capture and assert log output in tests:

buf := &bytes.Buffer{}
testWriter := &logx.TestWriter{Buf: buf}

logger := logx.FromContext(ctx)
l := *logger
l.Writer = testWriter

l.Info().Str("step", "test").Msg("test case")

// assert buf.String() contains expected fields

📂 Example CLI App

See examples/cli/main.go for CLI demo.


🔒 License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, fields map[string]interface{})

Non-contextual logging helpers

func DebugCtx

func DebugCtx(ctx context.Context, msg string, fields map[string]interface{})

Context-aware logging helpers

func Error

func Error(msg string, fields map[string]interface{})

func ErrorCtx

func ErrorCtx(ctx context.Context, msg string, fields map[string]interface{})

func Fatal

func Fatal(msg string, fields map[string]interface{})

func FatalCtx

func FatalCtx(ctx context.Context, msg string, fields map[string]interface{})

func FromContext

func FromContext(ctx context.Context) *log.Logger

FromContext returns a contextual logger with run_id, workflow_id, etc.

func Info

func Info(msg string, fields map[string]interface{})

func InfoCtx

func InfoCtx(ctx context.Context, msg string, fields map[string]interface{})

func Init

func Init(cfg InitConfig)

Init sets up the global logger

func Logger

func Logger() *log.Logger

func ResetLoggerForTest

func ResetLoggerForTest()

func Warn

func Warn(msg string, fields map[string]interface{})

func WarnCtx

func WarnCtx(ctx context.Context, msg string, fields map[string]interface{})

func WithRunID

func WithRunID(ctx context.Context, id string) context.Context

func WithTraceID

func WithTraceID(ctx context.Context, id string) context.Context

func WithWorkflowID

func WithWorkflowID(ctx context.Context, id string) context.Context

Types

type InitConfig

type InitConfig struct {
	Service     string
	Environment string
	Version     string
	Level       string

	LogToFile  bool
	LogPath    string
	MaxSizeMB  int64
	MaxBackups int
	MaxAgeDays int // for converting to time.Duration
	Compress   bool
}

Directories

Path Synopsis
examples
cli command

Jump to

Keyboard shortcuts

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