golang.composite_logger

command module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 1 Imported by: 0

README ยถ

Composite Logger

A flexible, multi-destination logging library for Go, built with Hexagonal Architecture (Ports & Adapters). It allows you to broadcast logs to the Console, Files, and Telegram simultaneously with a unified API.

Features

  • ๐Ÿš€ Asynchronous Engine: Non-blocking logging using a background worker and buffered channels.
  • ๐Ÿ— Clean Architecture: Decoupled core logic from specific implementations using Ports & Adapters.
  • ๐Ÿ“„ Structured Logging: Powered by Logrus with JSON and Text support.
  • ๐Ÿค– Telegram Integration: Send formatted alerts to Telegram with custom emojis, titles, and configurable timeouts.
  • ๐Ÿ“ฆ Log Rotation: Built-in log rotation for file adapter using Lumberjack.
  • ๐Ÿ” Auto Stack Traces: Automatically captures and cleans stack traces for Error and Fatal levels.
  • ๐Ÿ›ก Panic Recovery: Catch and log panics as Fatal errors.

Installation

go get github.com/Consolushka/golang.composite_logger

Quick Start

package main

import (
	"github.com/Consolushka/golang.composite_logger/pkg"
	"github.com/Consolushka/golang.composite_logger/pkg/adapters/setting"
)

func main() {
	// Initialize with Console and File loggers
	composite_logger.Init(
		setting.ConsoleSetting{
			Enabled:    true,
			LowerLevel: composite_logger.InfoLevel,
		},
		setting.FileSetting{
			Enabled:    true,
			Path:       "logs/app.log",
			LowerLevel: composite_logger.WarningLevel,
		},
	)

	// ALWAYS call Stop() at the end to flush the async queue
	defer composite_logger.Stop()

	composite_logger.Info("Hello, World!", map[string]interface{}{
		"user_id": 42,
	})
}

Advanced Configuration

JSON Formatting

Both Console and File adapters support JSON formatting (enabled by default).

setting.ConsoleSetting{
    Enabled:         true,
    IsJsonFormatter: &[]bool{false}[0], // Explicitly disable JSON to use Text
}
Telegram Adapter

Highly customizable with timeouts and level decorators.

composite_logger.Init(
    setting.TelegramSetting{
        Enabled:              true,
        BotKey:               "YOUR_BOT_TOKEN",
        ChatId:               12345678,
        Timeout:              10 * time.Second, // Network timeout
        LowerLevel:           composite_logger.ErrorLevel,
        UseLevelTitleWrapper: &[]bool{true}[0],
        // Optional: override default emojis (โ„น๏ธโ„น๏ธ, ๐Ÿšจ๐Ÿšจ, etc.)
        LevelWrappers: map[composite_logger.Level]string{
            composite_logger.FatalLevel: "๐Ÿ’€",
        },
        // Optional: override level names
        LevelTitles: map[composite_logger.Level]string{
            composite_logger.ErrorLevel: "ALARM",
        },
    },
)
Panic Recovery

Use Recover in your defer blocks to ensure panics are captured and logged with full stack traces.

func someDangerousOperation() {
    defer composite_logger.Recover(map[string]interface{}{
        "op": "database_sync",
    })

    panic("unexpected failure") // This will be logged as a FATAL error
}

Examples

The examples/ directory contains a structured set of lessons to help you get started:

Project Structure

  • pkg/: Public API and core types.
  • pkg/ports/: Interfaces for loggers and settings.
  • pkg/adapters/setting/: Concrete settings used for initialization.
  • internal/: Private implementations and internal logic.

Log Levels

  1. InfoLevel
  2. WarningLevel
  3. ErrorLevel
  4. FatalLevel

License

MIT

Documentation ยถ

The Go Gopher

There is no documentation for this package.

Directories ยถ

Path Synopsis
examples
composite command
console/01-text command
console/02-json command
file/01-text command
file/02-json command
pkg

Jump to

Keyboard shortcuts

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