-
-
Notifications
You must be signed in to change notification settings - Fork 1
Documentation
LAGRA is a lightweight, fast, and extensible Go logging library inspired by high-performance loggers like Zerolog. It supports structured logging, colorized output, JSON, log levels, asynchronous logging, custom hooks, and more.
β
Minimal and fast
β
Log levels filtering (Info, Warn, Error)
β
Colorized terminal output (customizable)
β
JSON log formatting
β
Contextual fields in all logs
β
Custom timestamp format
β
Hooks for additional processing
β
Optional asynchronous logging
β
File logging support
Since it's your own project, simply import it:
import "github.com/simplyYan/LAGRA"
All options are set through the LagraConfig
struct:
type LagraConfig struct {
LogFile string // Path to log file, "" for no file logging.
EnableColor bool // Colorize terminal output.
MinLevel LogType // Minimum log level to output.
ContextFields map[string]string // Key-value pairs to attach to all logs.
JSONFormat bool // Output logs in JSON.
TimeFormat string // Timestamp format using Go's time layout.
CustomColors map[LogType]string// ANSI escape codes for custom colors.
Async bool // Enable asynchronous logging.
}
const (
Info LogType = "INFO"
Warn LogType = "WARN"
Error LogType = "ERROR"
)
The logger will only output logs at or above the configured MinLevel
.
lagra, err := lagra.NewLagra(lagra.LagraConfig{
LogFile: "app.log",
EnableColor: true,
MinLevel: lagra.Info,
ContextFields: map[string]string{"app": "LAGRA", "env": "production"},
JSONFormat: false,
TimeFormat: "2006-01-02 15:04:05",
Async: true,
})
if err != nil {
log.Fatal(err)
}
β
If Async
is true
, logs are processed in a separate goroutine.
β
Use Close()
to safely shut down the logger.
Use the provided methods for different log levels:
lagra.Info("Application started")
lagra.Warn("Cache miss")
lagra.Error("Failed to connect to DB")
Each method automatically handles log formatting, colorization, context, and output.
To override default colors:
lagra.CustomColors: map[lagra.LogType]string{
lagra.Info: "\033[34m", // Blue
lagra.Warn: "\033[35m", // Magenta
lagra.Error: "\033[31m", // Red
}
If EnableColor
is false, output will be plain text.
Attach custom hooks to execute logic every time a log is emitted:
lagra.AddHook(func(level lagra.LogType, msg string) {
// Example: send log to a remote service
fmt.Println("HOOK:", level, msg)
})
Multiple hooks are supported and executed in the order they were added.
2025-05-23 14:30:00 [INFO] Application started
With colors if EnableColor = true
.
If JSONFormat
is true
, logs will look like:
{
"time": "2025-05-23 14:30:00",
"level": "INFO",
"message": "Application started",
"app": "LAGRA",
"env": "production"
}
β Useful for structured logging and log aggregation systems like ELK stack.
Use Go's time.Format
layout:
TimeFormat: "2006-01-02 15:04:05"
For ISO8601:
TimeFormat: time.RFC3339
If LogFile
is set, logs are also written to the specified file.
Example:
LogFile: "app.log"
β Supports concurrent writes in async mode.
If Async = true
:
- Logs are pushed to a buffered channel.
- Background goroutine processes and writes logs.
- Improves performance in high-throughput systems.
Make sure to call:
lagra.Close()
To gracefully stop the goroutine and close files.
Always close LAGRA when shutting down your application:
lagra.Close()
This ensures all buffered logs are flushed.
package main
import (
"fmt"
"github.com/simplyYan/LAGRA"
)
func main() {
logger, _ := lagra.NewLagra(lagra.LagraConfig{
LogFile: "logfile.log",
EnableColor: true,
MinLevel: lagra.Info,
ContextFields: map[string]string{"service": "auth"},
JSONFormat: false,
TimeFormat: "2006-01-02 15:04:05",
Async: true,
})
logger.AddHook(func(level lagra.LogType, msg string) {
fmt.Println("Custom Hook ->", level, msg)
})
logger.Info("Server started")
logger.Warn("Memory usage high")
logger.Error("Server crashed")
logger.Close()
}
- Fork the repository
- Add new features or improvements
- Submit a pull request with description and examples
For questions or suggestions, feel free to reach out or open an issue in your project's repository.
LAGRA provides an elegant balance between simplicity and power:
β Structured logs β Fast performance β Highly configurable β Suitable for both CLI tools and web services
Happy logging with LAGRA! πποΈ