log

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 5 Imported by: 0

README

Log Package

A lightweight, production-ready JSON logger SDK for Go that wraps the industry-standard zap logger with context-aware logging capabilities.

Features

  • JSON Formatting: All logs are formatted as JSON for easy parsing and analysis
  • Context-Aware: Automatically extracts and includes request IDs, trace IDs, and user IDs from context
  • Multiple Log Levels: Support for debug, info, warn, error, and fatal levels
  • Production Ready: Built on go.uber.org/zap, one of the fastest and most reliable loggers in Go
  • Global & Instance Loggers: Use global functions or create logger instances
  • Easy Context Management: Helper functions to add and retrieve values from context

Installation

go get github.com/adityayuga/go-sdk

Usage

Basic Initialization
import (
    "context"
    "github.com/adityayuga/go-sdk/log"
)

// Initialize the global logger
err := log.Init("info") // or "debug", "warn", "error"
if err != nil {
    panic(err)
}
defer log.GetLogger().Sync()

ctx := context.Background()

// Log messages with fields
log.Info(ctx, "user login", "username", "john", "email", "john@example.com")
log.Warn(ctx, "slow query", "duration_ms", 1500)
log.Error(ctx, "database error", "error", err)
Using Context for Trace Information
// Add trace information to context
ctx := context.Background()
ctx = log.WithRequestID(ctx, "req-12345")
ctx = log.WithTraceID(ctx, "trace-67890")
ctx = log.WithUserID(ctx, "user-456")

// These values are automatically included in all logs from this context
log.Info(ctx, "processing payment", "amount", 99.99)
// Output: {"level":"info","ts":"2026-02-13T10:00:00.000Z","msg":"processing payment","request_id":"req-12345","trace_id":"trace-67890","user_id":"user-456","amount":99.99}
Creating Logger Instances
// Create multiple logger instances with different configurations
logger, err := log.NewLogger("debug")
if err != nil {
    panic(err)
}

ctx := context.Background()
logger.Info(ctx, "instance logger message", "key", "value")
logger.Sync()
Available Methods
Global Functions
  • log.Info(ctx, message, fields...) - Info level
  • log.Warn(ctx, message, fields...) - Warning level
  • log.Error(ctx, message, fields...) - Error level
  • log.Debug(ctx, message, fields...) - Debug level
  • log.Fatal(ctx, message, fields...) - Fatal level (exits process)
Logger Instance Methods
  • logger.Info(ctx, message, fields...)
  • logger.Warn(ctx, message, fields...)
  • logger.Error(ctx, message, fields...)
  • logger.Debug(ctx, message, fields...)
  • logger.Fatal(ctx, message, fields...)
  • logger.Sync() - Flushes buffered logs
Context Helper Functions
  • log.WithRequestID(ctx, id) - Add request ID
  • log.WithTraceID(ctx, id) - Add trace ID
  • log.WithUserID(ctx, id) - Add user ID
  • log.WithLogContext(ctx, requestID, traceID, userID) - Add all at once
  • log.GetRequestID(ctx) - Retrieve request ID
  • log.GetTraceID(ctx) - Retrieve trace ID
  • log.GetUserID(ctx) - Retrieve user ID

Development Mode

For development environments with pretty-printed logs:

err := log.InitDevelopment()
if err != nil {
    panic(err)
}

Log Output Example

{
  "level": "error",
  "ts": "2026-02-13T10:00:00.000Z",
  "msg": "failed to fetch user",
  "request_id": "req-12345",
  "trace_id": "trace-67890",
  "user_id": "user-456",
  "user_id": 123,
  "error": "connection timeout"
}

Best Practices

  1. Always defer Sync(): Call logger.Sync() before exiting to ensure all logs are flushed
  2. Use Context: Pass context through your request handlers to maintain trace information
  3. Structured Logging: Use key-value pairs instead of string formatting for better searchability
  4. Log Levels: Use appropriate levels (debug for development, info for general, error for problems)
  5. Avoid Sensitive Data: Don't log passwords, tokens, or personal information

Performance

Built on zap, this logger is optimized for performance:

  • Minimal allocations
  • Efficient JSON encoding
  • Designed for high-throughput services

License

See LICENSE file in the repository

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, message string, fields ...interface{})

Debug logs a debug message using the global logger

func Debugf

func Debugf(ctx context.Context, format string, args ...interface{})

Debugf logs a formatted debug message using the global logger

func Error

func Error(ctx context.Context, message string, fields ...interface{})

Error logs an error message using the global logger

func Errorf

func Errorf(ctx context.Context, format string, args ...interface{})

Errorf logs a formatted error message using the global logger

func Fatal

func Fatal(ctx context.Context, message string, fields ...interface{})

Fatal logs a fatal message using the global logger and exits

func Fatalf

func Fatalf(ctx context.Context, format string, args ...interface{})

Fatalf logs a formatted fatal message using the global logger and exits

func GetDebugID

func GetDebugID(ctx context.Context) string

GetDebugID retrieves debug ID from context (uses debug package)

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID retrieves request ID from context

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID retrieves trace ID from context

func GetUserID

func GetUserID(ctx context.Context) string

GetUserID retrieves user ID from context

func Info

func Info(ctx context.Context, message string, fields ...interface{})

Info logs an info message using the global logger

func Infof

func Infof(ctx context.Context, format string, args ...interface{})

Infof logs a formatted info message using the global logger

func Init

func Init(level string) error

Init initializes the global logger with JSON encoding

func InitDevelopment

func InitDevelopment() error

InitDevelopment initializes the logger in development mode with pretty printing

func Warn

func Warn(ctx context.Context, message string, fields ...interface{})

Warn logs a warning message using the global logger

func Warnf

func Warnf(ctx context.Context, format string, args ...interface{})

Warnf logs a formatted warning message using the global logger

func WithDebugID

func WithDebugID(ctx context.Context, debugID string) context.Context

WithDebugID adds debug ID to context (uses debug package)

func WithLogContext

func WithLogContext(ctx context.Context, requestID, traceID, userID string) context.Context

WithLogContext sets multiple context values at once

func WithRequestID

func WithRequestID(ctx context.Context, requestID string) context.Context

WithRequestID adds request ID to context

func WithTraceID

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

WithTraceID adds trace ID to context

func WithUserID

func WithUserID(ctx context.Context, userID string) context.Context

WithUserID adds user ID to context

Types

type Logger

type Logger struct {
	*zap.Logger
}

Logger wraps zap.Logger with context-aware logging

func GetLogger

func GetLogger() *Logger

GetLogger returns the global logger instance

func NewLogger

func NewLogger(level string) (*Logger, error)

NewLogger creates a new logger instance

func (*Logger) Debug

func (l *Logger) Debug(ctx context.Context, message string, fields ...interface{})

Debug logs a debug-level message with context

func (*Logger) Debugf

func (l *Logger) Debugf(ctx context.Context, format string, args ...interface{})

Debugf logs a formatted debug-level message with context

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, message string, fields ...interface{})

Error logs an error-level message with context

func (*Logger) Errorf

func (l *Logger) Errorf(ctx context.Context, format string, args ...interface{})

Errorf logs a formatted error-level message with context

func (*Logger) Fatal

func (l *Logger) Fatal(ctx context.Context, message string, fields ...interface{})

Fatal logs a fatal-level message and exits

func (*Logger) Fatalf

func (l *Logger) Fatalf(ctx context.Context, format string, args ...interface{})

Fatalf logs a formatted fatal-level message and exits

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, message string, fields ...interface{})

Info logs an info-level message with context

func (*Logger) Infof

func (l *Logger) Infof(ctx context.Context, format string, args ...interface{})

Infof logs a formatted info-level message with context

func (*Logger) Sync

func (l *Logger) Sync() error

Sync flushes any buffered log entries

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, message string, fields ...interface{})

Warn logs a warning-level message with context

func (*Logger) Warnf

func (l *Logger) Warnf(ctx context.Context, format string, args ...interface{})

Warnf logs a formatted warning-level message with context

Jump to

Keyboard shortcuts

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