Skip to content
/ log Public

The log is go-module that implements functions for logging using different levels: DEBUG, INFO, WARNING, ERROR, FATAL, TRACE.

License

Notifications You must be signed in to change notification settings

goloop/log

Repository files navigation

Go Report Card License License Stay with Ukraine

log

A flexible, high-performance logging package for Go applications with support for multiple output formats, logging levels, and concurrent operations.

Features

  • Multiple Log Levels with clear semantics:

    • Panic: For unrecoverable errors that require immediate attention (calls panic())
    • Fatal: For critical errors that prevent application startup/operation (calls os.Exit(1))
    • Error: For runtime errors that need investigation but don't stop the application
    • Warn: For potentially harmful situations
    • Info: For general operational information
    • Debug: For detailed system state information
    • Trace: For ultra-detailed debugging information
  • Flexible Output Configuration:

    • Multiple simultaneous outputs (console, files, custom writers)
    • Per-output level filtering
    • Text and JSON formats
    • ANSI color support for terminal output
    • Custom prefix support
    • Configurable timestamps and layouts
  • Thread-Safe Operations:

    • Safe for concurrent use across goroutines
    • Mutex-protected logging operations
  • Performance Optimized:

    • Minimal allocations
    • Efficient formatting
    • Level-based filtering at source

Installation

go get -u github.com/goloop/log

Quick Start

Basic Usage

package main

import "github.com/goloop/log"

func main() {
    // Create logger with prefix.
    logger := log.New("APP")

    // Basic logging.
    logger.Info("Application started")
    logger.Debug("Debug information")
    logger.Error("Something went wrong")

    // Formatted logging.
    logger.Infof("User %s logged in", username)

    // With newline.
    logger.Errorln("Failed to connect to database")
}

Advanced Configuration

package main

import (
    "os"
    "github.com/goloop/log"
    "github.com/goloop/log/layout"
    "github.com/goloop/log/level"
)

func main() {
    // Open log file.
    file, err := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    // Configure multiple outputs.
    log.SetOutputs(
        // Console output with colors.
        log.Output{
            Name:      "console",
            Writer:    os.Stdout,
            Levels:    level.Info | level.Warn | level.Error,
            Layouts:   layout.Default,
            WithColor: 1,
            TextStyle: 1,
        },
        // File output in JSON format.
        log.Output{
            Name:      "file",
            Writer:    file,
            Levels:    level.Error | level.Fatal,
            TextStyle: -1, // JSON format
            WithPrefix: 1,
        },
    )

    // Use the logger
    log.Info("System initialized")
    log.Error("Database connection failed")
}

Custom Layout Configuration

package main

import (
    "github.com/goloop/log"
    "github.com/goloop/log/layout"
)

func main() {
    logger := log.New("APP")

    // Configure custom layout.
    logger.SetOutputs(log.Output{
        Name:    "custom",
        Writer:  os.Stdout,
        Layouts: layout.FullFilePath | layout.FuncName | layout.LineNumber,
    })

    logger.Info("Custom layout message")
}

Output Formats

Text Format (Default)

APP: 2023/12/02 15:04:05 INFO main.go:42 Starting application

JSON Format

{
    "prefix": "APP",
    "timestamp": "2023/12/02 15:04:05",
    "level": "INFO",
    "file": "main.go",
    "line": 42,
    "message": "Starting application"
}

Performance Considerations

  • Use appropriate log levels in production (typically Info and above)
  • Consider using JSON format only when structured logging is required
  • Disable debug/trace levels in production for optimal performance
  • Use formatted logging (Infof, etc.) only when necessary

Advanced Features

Stack Frame Skipping

logger := log.New("APP")
logger.SetSkipStackFrames(2) // skip wrapper functions

Multiple Prefix Support

logger := log.New("APP", "SERVICE", "API")  // Results in "APP-SERVICE-API"

Custom Writers

type CustomWriter struct {
    // implementation
}

func (w *CustomWriter) Write(p []byte) (n int, err error) {
    // custom write logic.
    return len(p), nil
}

logger.SetOutputs(log.Output{
    Name: "custom",
    Writer: &CustomWriter{},
    Levels: level.Info,
})

Managing Outputs

The logger provides several methods to manage outputs:

Get Current Outputs

// Get all outputs.
outputs := logger.Outputs()

// Get specific outputs by name.
stdoutOutput := logger.Outputs("stdout")

Edit Outputs

// Change output configuration.
logger.EditOutputs(log.Output{
    Name:    "stdout",
    Levels:  level.Error | level.Fatal,  // change levels
    WithColor: 1,                        // enable colors
})

// Disable specific output.
logger.EditOutputs(log.Output{
    Name:    "stdout",
    Enabled: -1,  // or trit.False
})

Delete Outputs

// Remove specific outputs,
logger.DeleteOutputs("stdout", "file")

// Or disable all logging by removing all outputs.
logger.DeleteOutputs(logger.Outputs()...)

Set New Outputs

// Replace all outputs with new ones.
logger.SetOutputs(
    log.Output{
        Name:    "console",
        Writer:  os.Stdout,
        Levels:  level.Info | level.Warn,
    },
    log.Output{
        Name:    "errors",
        Writer:  errorFile,
        Levels:  level.Error | level.Fatal,
    },
)

Why use this logger?

  • Flexible configuration
  • High performance
  • Multiple output support
  • Structured logging support
  • Thread safety
  • Comprehensive logging levels

Related Projects

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

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

About

The log is go-module that implements functions for logging using different levels: DEBUG, INFO, WARNING, ERROR, FATAL, TRACE.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages