logfx

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

README ΒΆ

ajan/logfx

Overview

logfx package is a configurable logging solution that leverages the log/slog of the standard library for structured logging. It includes pretty-printing options and OpenTelemetry collector integration as the preferred export method, with optional direct Loki export for legacy setups. The package supports OpenTelemetry-compatible severity levels and provides extensive test coverage to ensure reliability and correctness.

Key Features
  • 🎯 Extended Log Levels - OpenTelemetry-compatible levels while using standard log/slog under the hood
  • πŸ”„ Automatic Correlation IDs - Request tracing across your entire application
  • 🌐 OpenTelemetry Integration - Native OTLP export to OpenTelemetry collectors
  • πŸ“Š Multiple Export Formats - JSON logs, Loki export, OTLP export
  • 🎨 Pretty Printing - Colored output for development
  • ⚑ Performance Optimized - Asynchronous exports, structured logging

πŸš€ Extended Log Levels

The Problem: Go's standard log/slog package provides only 4 log levels (Debug, Info, Warn, Error), which is insufficient for modern observability and OpenTelemetry compatibility.

The Solution: logfx extends the standard library to provide 7 OpenTelemetry-compatible log levels while maintaining full compatibility with log/slog:

// Standard Go slog levels (limited)
slog.LevelDebug  // -4
slog.LevelInfo   //  0
slog.LevelWarn   //  4
slog.LevelError  //  8

// logfx extended levels (OpenTelemetry compatible)
logfx.LevelTrace // -8  ← Additional
logfx.LevelDebug // -4
logfx.LevelInfo  //  0
logfx.LevelWarn  //  4
logfx.LevelError //  8
logfx.LevelFatal // 12  ← Additional
logfx.LevelPanic // 16  ← Additional
Why This Matters
  1. OpenTelemetry Compatibility - Maps perfectly to OpenTelemetry log severity levels
  2. Better Observability - More granular log levels for better debugging and monitoring
  3. Standard Library Foundation - Built on log/slog, not a replacement
  4. Zero Breaking Changes - Existing slog code works unchanged
  5. Proper Severity Mapping - Correct OTLP export with appropriate severity levels
Extended Level Usage
import "github.com/eser/ajan/logfx"

logger := logfx.NewLogger(
    logfx.WithLevel(logfx.LevelTrace), // Now supports all 7 levels
)

// Use all OpenTelemetry-compatible levels
logger.Trace("Detailed debugging info")           // Most verbose
logger.Debug("Debug information")                 // Development debugging
logger.Info("General information")                // Standard info
logger.Warn("Warning message")                    // Potential issues
logger.Error("Error occurred")                    // Errors that don't stop execution
logger.Fatal("Fatal error")                       // Critical errors
logger.Panic("Panic condition")                   // Most severe

Colored Output (development mode):

23:45:12.123 TRACE Detailed debugging info
23:45:12.124 DEBUG Debug information
23:45:12.125 INFO General information
23:45:12.126 WARN Warning message
23:45:12.127 ERROR Error occurred
23:45:12.128 FATAL Fatal error
23:45:12.129 PANIC Panic condition

Structured Output (production mode):

{"time":"2024-01-15T23:45:12.123Z","level":"TRACE","msg":"Detailed debugging info"}
{"time":"2024-01-15T23:45:12.124Z","level":"DEBUG","msg":"Debug information"}
{"time":"2024-01-15T23:45:12.125Z","level":"INFO","msg":"General information"}
{"time":"2024-01-15T23:45:12.126Z","level":"WARN","msg":"Warning message"}
{"time":"2024-01-15T23:45:12.127Z","level":"ERROR","msg":"Error occurred"}
{"time":"2024-01-15T23:45:12.128Z","level":"FATAL","msg":"Fatal error"}
{"time":"2024-01-15T23:45:12.129Z","level":"PANIC","msg":"Panic condition"}

OpenTelemetry Export (automatic severity mapping):

{
  "logRecords": [
    {"body": {"stringValue": "Detailed debugging info"}, "severityNumber": 1, "severityText": "TRACE"},
    {"body": {"stringValue": "Debug information"}, "severityNumber": 5, "severityText": "DEBUG"},
    {"body": {"stringValue": "General information"}, "severityNumber": 9, "severityText": "INFO"},
    {"body": {"stringValue": "Warning message"}, "severityNumber": 13, "severityText": "WARN"},
    {"body": {"stringValue": "Error occurred"}, "severityNumber": 17, "severityText": "ERROR"},
    {"body": {"stringValue": "Fatal error"}, "severityNumber": 21, "severityText": "FATAL"},
    {"body": {"stringValue": "Panic condition"}, "severityNumber": 24, "severityText": "PANIC"}
  ]
}

Quick Start

Basic Usage
package main

import (
    "log/slog"
    "os"

    "github.com/eser/ajan/logfx"
)

func main() {
    // Create logger with OpenTelemetry collector export
    logger := logfx.NewLogger(
        logfx.WithOTLP("http://otel-collector:4318", true),
    )

    // Use structured logging with extended levels
    logger.Info("Application started",
        slog.String("service", "my-service"),
        slog.String("version", "1.0.0"),
    )

    // Extended levels for better observability
    logger.Trace("Connection pool initialized")     // Very detailed
    logger.Debug("Processing user request")         // Debug info
    logger.Warn("High memory usage detected")       // Warnings
    logger.Fatal("Database connection failed")      // Critical errors
}
With HTTP Correlation IDs
package main

import (
    "log/slog"
    "net/http"
    "os"

    "github.com/eser/ajan/httpfx"
    "github.com/eser/ajan/httpfx/middlewares"
    "github.com/eser/ajan/logfx"
)

func main() {
    logger := logfx.NewLogger(
        logfx.WithWriter(os.Stdout),
        logfx.WithConfig(&logfx.Config{
            Level:        "TRACE",  // Use extended levels for comprehensive logging
            PrettyMode:   false,
            OTLPEndpoint: "http://otel-collector:4318",
        }),
    )

    router := httpfx.NewRouter("/api")

    // Add correlation middleware for automatic request tracking
    router.Use(middlewares.CorrelationIDMiddleware())
    router.Use(middlewares.LoggingMiddleware(logger))

    router.Route("GET /users/{id}", func(ctx *httpfx.Context) httpfx.Result {
        // All logs automatically include correlation_id from HTTP headers
        logger.TraceContext(ctx.Request.Context(), "Starting user lookup")
        logger.InfoContext(ctx.Request.Context(), "Processing user request",
            slog.String("user_id", "123"),
        )

        return ctx.Results.JSON(map[string]string{"status": "success"})
    })

    http.ListenAndServe(":8080", router.GetMux())
}

Log Output with Correlation:

{"time":"2024-01-15T10:30:00Z","level":"INFO","msg":"HTTP request started","method":"GET","path":"/api/users/123","correlation_id":"abc-123-def"}
{"time":"2024-01-15T10:30:00Z","level":"TRACE","msg":"Starting user lookup","correlation_id":"abc-123-def"}
{"time":"2024-01-15T10:30:00Z","level":"INFO","msg":"Processing user request","user_id":"123","correlation_id":"abc-123-def"}
{"time":"2024-01-15T10:30:00Z","level":"INFO","msg":"HTTP request completed","method":"GET","status_code":200,"correlation_id":"abc-123-def"}

Configuration

type Config struct {
	Level      string `conf:"level"      default:"INFO"`        // Supports: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, PANIC
	PrettyMode bool   `conf:"pretty"     default:"true"`
	AddSource  bool   `conf:"add_source" default:"false"`

	// OpenTelemetry Collector configuration (preferred)
	OTLPEndpoint string `conf:"otlp_endpoint" default:""`
	OTLPInsecure bool   `conf:"otlp_insecure" default:"true"`

	// Direct Loki export (legacy/additional option)
	LokiURI   string `conf:"loki_uri" default:""`
	LokiLabel string `conf:"loki_label" default:""`
}
Export Priority

The package automatically chooses the best export method:

  1. πŸ₯‡ OTLP Collector (OTLPEndpoint) - Preferred for production
  2. πŸ₯ˆ Direct Loki (LokiURI) - Legacy/fallback option
  3. Both can run simultaneously if needed

OpenTelemetry Collector Integration

config := &logfx.Config{
    Level:        "INFO",           // Use any of the 7 extended levels
    PrettyMode:   false,
    OTLPEndpoint: "http://otel-collector:4318",
    OTLPInsecure: true,
}
logger := logfx.NewLogger(
    logfx.WithWriter(os.Stdout),
    logfx.WithConfig(config),
)
Benefits of OpenTelemetry Collector
  • πŸ”„ Unified Pipeline - All logs, metrics, and traces flow through one point
  • πŸŽ›οΈ Flexibility - Change backends without code changes
  • ⚑ Performance - Built-in batching, retries, and buffering
  • πŸ’° Cost Optimization - Sampling and filtering before export
  • πŸ”§ Processing - Transform, enrich, and route data
Example Collector Configuration
# otel-collector-config.yaml
receivers:
  otlp:
    protocols:
      http:
        endpoint: 0.0.0.0:4318

exporters:
  loki:
    endpoint: http://loki:3100/loki/api/v1/push

service:
  pipelines:
    logs:
      receivers: [otlp]
      exporters: [loki]

Correlation IDs

Automatic HTTP Correlation

When using with httpfx, correlation IDs are automatically:

  • βœ… Extracted from X-Correlation-ID headers
  • βœ… Generated if missing
  • βœ… Propagated through Go context
  • βœ… Added to all log entries
  • βœ… Included in response headers
Manual Correlation Access
import "github.com/eser/ajan/httpfx/middlewares"

func MyHandler(ctx *httpfx.Context) httpfx.Result {
    correlationID := middlewares.GetCorrelationIDFromContext(ctx.Request.Context())

    // Use in external service calls
    externalReq.Header.Set("X-Correlation-ID", correlationID)

    return ctx.Results.JSON(map[string]string{
        "correlation_id": correlationID,
    })
}

Advanced Usage

Multiple Export Destinations
config := &logfx.Config{
    Level:        "DEBUG",                          // Extended level support
    OTLPEndpoint: "http://otel-collector:4318",     // Primary
    LokiURI:      "http://backup-loki:3100",        // Backup
    LokiLabel:    "backup=true,service=my-app",
}
Level Configuration Examples
// Development - verbose logging with all levels
devConfig := &logfx.Config{
    Level:      "TRACE",    // Most verbose - see everything
    PrettyMode: true,
    AddSource:  true,
}

// Production - structured output with appropriate level
prodConfig := &logfx.Config{
    Level:        "INFO",   // Production appropriate
    PrettyMode:   false,
    OTLPEndpoint: "http://otel-collector:4318",
}

// Debug production issues - temporary verbose logging
debugConfig := &logfx.Config{
    Level:        "DEBUG",  // More detail for troubleshooting
    PrettyMode:   false,
    OTLPEndpoint: "http://otel-collector:4318",
}
Standard Library Compatibility
// logfx extends slog.Level, so standard slog works unchanged
import "log/slog"

// This works exactly as before
slog.Info("Standard slog message")
slog.Debug("Debug with standard slog")

// But you can also use extended levels through logfx
logger.Trace("Extended trace level")    // Not available in standard slog
logger.Fatal("Extended fatal level")    // Not available in standard slog
logger.Panic("Extended panic level")    // Not available in standard slog

Error Handling

The logger handles export failures gracefully:

// Logger continues working even if exports fail
logger := logfx.NewLogger(
    logfx.WithWriter(os.Stdout),
    logfx.WithConfig(config),
)

// Check for initialization errors
if handler, ok := logger.Handler.(*logfx.Handler); ok && handler.InitError != nil {
    log.Printf("Logger init warning: %v", handler.InitError)
}

// Logs always go to the primary writer (stdout/file)
// Export failures are logged to stderr without affecting your app

Observability Integration

Complete Observability Stack

Use with other ajan packages for full observability:

import (
    "github.com/eser/ajan/logfx"     // Extended logs
    "github.com/eser/ajan/metricsfx" // Metrics
    // "github.com/eser/ajan/tracesfx"  // Traces
)

// All export to the same OpenTelemetry collector
config := &Config{
    OTLPEndpoint: "http://otel-collector:4318",
}
Log Correlation with Traces

When OpenTelemetry tracing is active, logs automatically include:

{
  "time": "2024-01-15T10:30:00Z",
  "level": "INFO",
  "msg": "Processing request",
  "correlation_id": "abc-123-def",  // HTTP correlation ID
  "trace_id": "4bf92f3577b34da6",   // OpenTelemetry trace ID
  "span_id": "00f067aa0bb902b7"     // OpenTelemetry span ID
}

This provides multiple correlation dimensions for complete request traceability.

API Reference

Logger Creation
NewLogger (Options Pattern)
func NewLogger(options ...NewLoggerOption) *Logger

Create a logger using the flexible options pattern:

// Basic logger with default configuration
logger := logfx.NewLogger()

// Logger with custom writer and config
logger := logfx.NewLogger(
    logfx.WithWriter(os.Stdout),
    logfx.WithConfig(&logfx.Config{
        Level:        "INFO",
        PrettyMode:   false,
        OTLPEndpoint: "http://otel-collector:4318",
    }),
)

// Logger with individual options
logger := logfx.NewLogger(
    logfx.WithLevel(slog.LevelDebug),
    logfx.WithPrettyMode(true),
    logfx.WithAddSource(true),
    logfx.WithOTLP("http://otel-collector:4318", true),
    logfx.WithDefaultLogger(), // Set as default logger
)
Available Options
// Configuration options
WithConfig(config *Config)                    // Full configuration
WithLevel(level slog.Level)                   // Set log level
WithPrettyMode(pretty bool)                   // Enable/disable pretty printing
WithAddSource(addSource bool)                 // Include source code location
WithDefaultLogger()                           // Set as default logger

// Output options
WithWriter(writer io.Writer)                  // Set output writer
WithFromSlog(slog *slog.Logger)              // Wrap existing slog.Logger

// Export options
WithOTLP(endpoint string, insecure bool)      // OpenTelemetry collector export
WithLoki(uri string, label string)           // Direct Loki export
Convenience Functions
// Quick default setup
logger := logfx.NewLogger(
    logfx.WithConfig(&logfx.Config{
        Level:      "INFO",
        PrettyMode: true,
        AddSource:  false,
    }),
)

// Wrap existing slog logger
slogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
logger := logfx.NewLogger(logfx.WithFromSlog(slogger))

// Production setup
logger := logfx.NewLogger(
    logfx.WithConfig(&logfx.Config{
        Level:        "INFO",
        PrettyMode:   false,
        OTLPEndpoint: "http://otel-collector:4318",
    }),
)

// Development setup
logger := logfx.NewLogger(
    logfx.WithLevel(slog.LevelDebug),
    logfx.WithPrettyMode(true),
    logfx.WithAddSource(true),
    logfx.WithDefaultLogger(),
)

Usage Examples:

// Quick default setup
logger := logfx.NewLogger(
    logfx.WithConfig(&logfx.Config{
        Level:      "INFO",
        PrettyMode: true,
        AddSource:  false,
    }),
)

// Wrap existing slog logger
slogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
logger := logfx.NewLogger(logfx.WithFromSlog(slogger))

// Production setup
logger := logfx.NewLogger(
    logfx.WithConfig(&logfx.Config{
        Level:        "INFO",
        PrettyMode:   false,
        OTLPEndpoint: "http://otel-collector:4318",
    }),
)

// Development setup
logger := logfx.NewLogger(
    logfx.WithLevel(slog.LevelDebug),
    logfx.WithPrettyMode(true),
    logfx.WithAddSource(true),
    logfx.WithDefaultLogger(),
)

Ideal Architecture

🎯 Recommended Setup: Use OpenTelemetry Collector as the central hub for all observability data.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 β”‚    β”‚                     β”‚    β”‚                 β”‚
β”‚   Your App      │───▢│ OpenTelemetry       │───▢│  Backends       β”‚
β”‚                 β”‚    β”‚ Collector           β”‚    β”‚                 β”‚
β”‚ β€’ logfx         β”‚    β”‚                     β”‚    β”‚ β€’ Loki (logs)   β”‚
β”‚ β€’ metricsfx     β”‚    β”‚ β€’ Receives all 3    β”‚    β”‚ β€’ Prometheus    β”‚
β”‚ β€’ tracesfx      β”‚    β”‚   pillars (L+M+T)   β”‚    β”‚   (metrics)     β”‚
β”‚                 β”‚    β”‚ β€’ Routes & filters  β”‚    β”‚ β€’ Tempo         β”‚
β”‚                 β”‚    β”‚ β€’ Transforms        β”‚    β”‚   (traces)      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Benefits:

  • Unified observability pipeline - All logs, metrics, and traces flow through one point
  • Flexibility - Change backends without touching application code
  • Processing - Filter, transform, sample, and enrich data before export
  • Reliability - Built-in buffering, retries, and load balancing
  • Cost optimization - Sampling and filtering reduce backend costs

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

View Source
const (
	LevelTrace slog.Level = slog.Level(-8)
	LevelDebug slog.Level = slog.LevelDebug
	LevelInfo  slog.Level = slog.LevelInfo
	LevelWarn  slog.Level = slog.LevelWarn
	LevelError slog.Level = slog.LevelError
	LevelFatal slog.Level = slog.Level(12)
	LevelPanic slog.Level = slog.Level(16)
)

OtelLevel - 9 = Level.

View Source
const DefaultLogLevel = "INFO"

Variables ΒΆ

View Source
var (
	ErrFailedToParseLogLevel = errors.New("failed to parse log level")
	ErrFailedToWriteLog      = errors.New("failed to write log")
	ErrFailedToHandleLog     = errors.New("failed to handle log")
	ErrFailedToInitLoki      = errors.New("failed to initialize loki client")
	ErrFailedToInitOTLP      = errors.New("failed to initialize OTLP client")
)
View Source
var (
	ErrInvalidLevelString = errors.New("invalid level string")
	ErrUnknownErrorLevel  = errors.New("unknown error level")
)
View Source
var (
	ErrFailedToSendToLoki  = errors.New("failed to send log to loki")
	ErrFailedToMarshalLoki = errors.New("failed to marshal loki payload")
	ErrInvalidLokiResponse = errors.New("invalid loki response")
	ErrLokiNotConfigured   = errors.New("loki not configured")
)
View Source
var (
	ErrFailedToCreateOTLPLogExporter = errors.New("failed to create OTLP log exporter")
	ErrFailedToCreateLogProcessor    = errors.New("failed to create log processor")
	ErrOTLPNotConfigured             = errors.New("OTLP not configured")
	ErrFailedToShutdownOTLP          = errors.New("failed to shutdown OTLP logger provider")
)

Functions ΒΆ

func Colored ΒΆ

func Colored(color Color, message string) string

func LevelEncoder ΒΆ added in v0.6.14

func LevelEncoder(l slog.Level) string

func LevelEncoderColored ΒΆ added in v0.6.14

func LevelEncoderColored(l slog.Level) string

func ParseLevel ΒΆ added in v0.6.14

func ParseLevel(s string, errorOnEmpty bool) (*slog.Level, error)

func ReplacerGenerator ΒΆ

func ReplacerGenerator(prettyMode bool) func([]string, slog.Attr) slog.Attr

func TraceLines ΒΆ

func TraceLines(frames StackTrace) []string

Types ΒΆ

type Color ΒΆ

type Color string
const (
	ColorReset        Color = "\033[0m"
	ColorRed          Color = "\033[31m"
	ColorGreen        Color = "\033[32m"
	ColorYellow       Color = "\033[33m"
	ColorBlue         Color = "\033[34m"
	ColorMagenta      Color = "\033[35m"
	ColorCyan         Color = "\033[36m"
	ColorGray         Color = "\033[37m"
	ColorDimGray      Color = "\033[90m"
	ColorLightRed     Color = "\033[91m"
	ColorLightGreen   Color = "\033[92m"
	ColorLightYellow  Color = "\033[93m"
	ColorLightBlue    Color = "\033[94m"
	ColorLightMagenta Color = "\033[95m"
	ColorLightCyan    Color = "\033[96m"
	ColorLightGray    Color = "\033[97m"
)

type Config ΒΆ

type Config struct {
	Level string `conf:"level" default:"INFO"`

	// OpenTelemetry Collector configuration (preferred)
	OTLPEndpoint string `conf:"otlp_endpoint" default:""`

	// Direct Loki export (legacy/additional option)
	LokiURI       string `conf:"loki_uri"   default:""`
	LokiLabel     string `conf:"loki_label" default:""`
	DefaultLogger bool   `conf:"default"    default:"false"`

	PrettyMode bool `conf:"pretty"     default:"true"`
	AddSource  bool `conf:"add_source" default:"false"`

	OTLPInsecure bool `conf:"otlp_insecure" default:"true"`
}

type CorrelationIDContextKey ΒΆ added in v0.7.2

type CorrelationIDContextKey struct{}

CorrelationIDContextKey is the context key for correlation ID - shared with httpfx middleware.

type Handler ΒΆ

type Handler struct {
	InitError error

	InnerHandler slog.Handler

	InnerWriter io.Writer
	InnerConfig *Config

	// Export clients (prioritized: OTLP -> Loki)
	OTLPClient *OTLPClient
	LokiClient *LokiClient
}

func NewHandler ΒΆ

func NewHandler(w io.Writer, config *Config) *Handler

func (*Handler) Enabled ΒΆ

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

func (*Handler) Handle ΒΆ

func (h *Handler) Handle(ctx context.Context, rec slog.Record) error

func (*Handler) Shutdown ΒΆ added in v0.7.2

func (h *Handler) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down any active export clients.

func (*Handler) WithAttrs ΒΆ

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup ΒΆ

func (h *Handler) WithGroup(name string) slog.Handler

type Logger ΒΆ added in v0.6.15

type Logger struct {
	*slog.Logger

	Writer io.Writer
	Config *Config
}

func NewLogger ΒΆ

func NewLogger(options ...NewLoggerOption) *Logger

func (*Logger) Fatal ΒΆ added in v0.6.15

func (l *Logger) Fatal(msg string, args ...any)

Fatal logs at LevelFatal.

func (*Logger) FatalContext ΒΆ added in v0.6.15

func (l *Logger) FatalContext(ctx context.Context, msg string, args ...any)

FatalContext logs at LevelFatal with the given context.

func (*Logger) Panic ΒΆ added in v0.6.15

func (l *Logger) Panic(msg string, args ...any)

Panic logs at LevelPanic.

func (*Logger) PanicContext ΒΆ added in v0.6.15

func (l *Logger) PanicContext(ctx context.Context, msg string, args ...any)

PanicContext logs at LevelPanic with the given context.

func (*Logger) SetAsDefault ΒΆ added in v0.7.2

func (l *Logger) SetAsDefault()

func (*Logger) Trace ΒΆ added in v0.6.15

func (l *Logger) Trace(msg string, args ...any)

Trace logs at LevelTrace.

func (*Logger) TraceContext ΒΆ added in v0.6.15

func (l *Logger) TraceContext(ctx context.Context, msg string, args ...any)

TraceContext logs at LevelTrace with the given context.

type LokiClient ΒΆ added in v0.7.2

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

LokiClient handles sending logs to Loki.

func NewLokiClient ΒΆ added in v0.7.2

func NewLokiClient(uri, labelStr string) (*LokiClient, error)

NewLokiClient creates a new Loki client.

func (*LokiClient) SendLog ΒΆ added in v0.7.2

func (c *LokiClient) SendLog(ctx context.Context, rec slog.Record)

SendLog sends a log record to Loki asynchronously.

type LokiPayload ΒΆ added in v0.7.2

type LokiPayload struct {
	Streams []LokiStream `json:"streams"`
}

LokiPayload represents the payload sent to Loki.

type LokiStream ΒΆ added in v0.7.2

type LokiStream struct {
	Stream map[string]string `json:"stream"`
	Values [][]string        `json:"values"`
}

LokiStream represents a single Loki log stream.

type NewLoggerOption ΒΆ added in v0.7.2

type NewLoggerOption func(*Logger)

func WithAddSource ΒΆ added in v0.7.2

func WithAddSource(addSource bool) NewLoggerOption

func WithConfig ΒΆ added in v0.7.2

func WithConfig(config *Config) NewLoggerOption

func WithDefaultLogger ΒΆ added in v0.7.2

func WithDefaultLogger() NewLoggerOption

func WithFromSlog ΒΆ added in v0.7.2

func WithFromSlog(slog *slog.Logger) NewLoggerOption

func WithLevel ΒΆ added in v0.7.2

func WithLevel(level slog.Level) NewLoggerOption

func WithLoki ΒΆ added in v0.7.2

func WithLoki(uri string, label string) NewLoggerOption

func WithOTLP ΒΆ added in v0.7.2

func WithOTLP(endpoint string, insecure bool) NewLoggerOption

func WithPrettyMode ΒΆ added in v0.7.2

func WithPrettyMode(pretty bool) NewLoggerOption

func WithWriter ΒΆ added in v0.7.2

func WithWriter(writer io.Writer) NewLoggerOption

type OTLPClient ΒΆ added in v0.7.2

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

OTLPClient handles sending logs to OpenTelemetry collector.

func NewOTLPClient ΒΆ added in v0.7.2

func NewOTLPClient(endpoint string, insecure bool) (*OTLPClient, error)

NewOTLPClient creates a new OTLP client for sending logs to OpenTelemetry collector.

func (*OTLPClient) SendLog ΒΆ added in v0.7.2

func (c *OTLPClient) SendLog(ctx context.Context, rec slog.Record)

SendLog sends a log record to OpenTelemetry collector asynchronously.

func (*OTLPClient) Shutdown ΒΆ added in v0.7.2

func (c *OTLPClient) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the OTLP client.

type StackTrace ΒΆ

type StackTrace = []uintptr // []runtime.Frame

type StackTracer ΒΆ

type StackTracer interface {
	StackTrace() StackTrace
}

Jump to

Keyboard shortcuts

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