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.
- ๐ 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
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
}
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
InfoLevel
WarningLevel
ErrorLevel
FatalLevel
License
MIT