Skip to content

salsadigitalauorg/go-lagoon-log-forwarder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go Lagoon Log Forwarder

CI Release codecov Go Report Card Go Reference

A high-performance Go library for forwarding structured logs to UDP endpoints, designed for Lagoon/Kubernetes environments with built-in support for ELK stack integration.

✨ Features

  • πŸš€ High Performance: Multi-writer output (stdout + UDP) with efficient connection handling
  • πŸ“‹ Structured Logging: JSON logging using Go's native slog package
  • βš™οΈ Flexible Configuration: Programmatic configuration (no global flags)
  • πŸ”§ ELK Stack Ready: Built-in log formatting for Logstash/Elasticsearch
  • πŸ›‘οΈ Production Ready: Comprehensive error handling and graceful failures
  • πŸ”’ Thread Safe: Serialized UDP writes ensure log integrity in concurrent environments
  • πŸ§ͺ Well Tested: 100% test coverage with benchmarks
  • πŸ“¦ Zero Dependencies: Uses only Go standard library

πŸ“¦ Installation

go get github.com/salsadigitalauorg/go-lagoon-log-forwarder@latest

πŸš€ Quick Start

Basic Usage

package main

import (
    "log/slog"
    
    "github.com/salsadigitalauorg/go-lagoon-log-forwarder"
)

func main() {
    // Create configuration with defaults
    cfg := logger.NewConfig()
    cfg.LogType = "my-app"        // Required: must match k8s namespace
    cfg.LogHost = "logstash.example.com"
    
    // Initialize logger
    if err := logger.Initialize(cfg); err != nil {
        panic(err)
    }
    
    // Use structured logging
    slog.Info("Application started", 
        "version", "1.0.0",
        "environment", "production",
    )
    
    slog.Error("Database connection failed", 
        "error", err,
        "database", "postgres",
        "retry_count", 3,
    )
}

Advanced Configuration

cfg := logger.NewConfig()
cfg.LogType = "my-microservice"
cfg.LogHost = "logs.k8s.cluster"
cfg.LogPort = 5140
cfg.ApplicationName = "user-service"
cfg.LogChannel = "ProductionLogs"
cfg.AddSource = true  // Include source file/line info

if err := logger.Initialize(cfg); err != nil {
    log.Fatalf("Failed to initialize logger: %v", err)
}

βš™οΈ Configuration Options

Field Type Default Description
LogType string required Log type (must match k8s namespace)
LogHost string "" UDP host for log forwarding
LogPort int 5140 UDP port number
ApplicationName string "" Application identifier
LogChannel string "LagoonLogs" Channel name for log routing
AddSource bool true Include source file/line information
MessageVersion int 1 Log message format version

πŸ“ Log Format

The logger produces structured JSON logs compatible with ELK stack:

{
  "time": "2024-01-15T10:30:00Z",
  "level": "INFO", 
  "message": "User authenticated",
  "@version": 3,
  "@timestamp": "2024-01-15T10:30:00Z",
  "application": "user-service",
  "channel": "LagoonLogs",
  "host": "pod-abc123",
  "type": "production",
  "user_id": 12345,
  "method": "POST"
}

Automatic Field Mapping

  • msg β†’ message
  • time β†’ @timestamp
  • timestampOverride β†’ @timestamp

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Application   β”‚    β”‚   Logger     β”‚    β”‚   Multi-Writer  β”‚
β”‚                 │───▢│              │───▢│                 β”‚
β”‚  slog.Info()    β”‚    β”‚  Transform   β”‚    β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚  slog.Error()   β”‚    β”‚  Fields      β”‚    β”‚  β”‚   Stdout    β”‚β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
                                           β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
                                           β”‚  β”‚ UDP Socket  β”‚β”‚
                                           β”‚  β”‚ (Logstash)  β”‚β”‚
                                           β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
                                           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ§ͺ Testing

Run the comprehensive test suite:

# Run all tests
go test -v ./...

# Run tests with coverage
go test -v -race -coverprofile=coverage.out ./...

# View coverage report
go tool cover -html=coverage.out

# Run benchmarks
go test -bench=. -benchmem ./...

πŸ› οΈ Development

Prerequisites

  • Go 1.21 or later
  • Git

Local Setup

git clone https://github.com/salsadigitalauorg/go-lagoon-log-forwarder.git
cd go-lagoon-log-forwarder

# Install dependencies
go mod download

# Run tests
go test -v ./...

# Run linter (requires golangci-lint)
golangci-lint run

Code Quality

This project maintains high code quality standards:

  • 100% Test Coverage: All code paths tested
  • Comprehensive Linting: 30+ linters via golangci-lint
  • Security Scanning: Gosec security analysis
  • Performance Testing: Benchmark tests included
  • Multi-Version Support: Tested on Go 1.21-1.24

πŸš€ CI/CD Pipeline

This project uses automated CI/CD with:

  • Pull Request Testing: Comprehensive checks on all PRs
  • Automatic Versioning: Semantic versioning based on conventional commits
  • Automated Releases: GitHub releases with auto-generated changelogs
  • Cross-Platform Testing: Linux, macOS, Windows compatibility

Commit Message Format

Use Conventional Commits for automatic versioning:

feat: add custom log formatter support       # Minor version bump
fix: resolve UDP connection memory leak      # Patch version bump  
feat!: change Initialize function signature  # Major version bump

πŸ“Š Performance

Benchmark results on typical hardware:

BenchmarkNewConfig-10      1000000000    0.23 ns/op    0 B/op    0 allocs/op
BenchmarkConfig-10            2441269  475.40 ns/op   48 B/op    1 allocs/op
BenchmarkDefaultAttrs-10     50000000   28.50 ns/op   24 B/op    1 allocs/op
BenchmarkReplaceAttr-10     100000000   12.30 ns/op    0 B/op    0 allocs/op

🀝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for:

  • Development workflow
  • Commit message conventions
  • Code quality standards
  • Testing requirements

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

🏷️ Versioning

This project uses Semantic Versioning:

  • Major: Breaking changes
  • Minor: New features (backward compatible)
  • Patch: Bug fixes and improvements

Releases are automated based on conventional commit messages.

πŸ“ž Support


Built with ❀️ by the Salsa Digital team for Lagoon & Kubernetes deployments

About

No description, website, or topics provided.

Resources

Contributing

Stars

Watchers

Forks

Packages

No packages published