A simple, flexible, and feature-rich structured logging library for Go.
- Multiple log levels (Debug, Info, Warn, Error, Fatal, Panic, Stat)
- Built-in default stdout logger with ANSI colors
- Support for multiple writers (stdout, file, HTTP, and custom channels)
- ANSI color formatting for terminal output
- JSON support for structured logging
- File rotation support
- Buffer system for log management
- Flexible argument handling with both standard and formatted logging
- HTTP logging with API key support
- Channel-based logging for custom log processing
- Concurrent-safe logging
go get github.com/AR1011/slogThe library comes with a default stdout logger, so you can start logging immediately:
package main
import "github.com/AR1011/slog"
func main() {
// Basic logging
slog.Debug("Debug message")
slog.Info("Info message")
slog.Warn("Warning message")
slog.Error("Error message")
// Logging with context
slog.Info("User logged in", "user_id", 123, "ip", "192.168.1.1")
// Format string logging
slog.InfoF("User %s logged in from %s", []interface{}{"john", "192.168.1.1"})
// Don't forget to close the logger when you're done
defer slog.Close()
}The library supports the following log levels:
Debug: Detailed information for debuggingInfo: General information about program executionWarn: Warning messages for potentially harmful situationsError: Error messages for serious problemsFatal: Critical errors that terminate the programPanic: Critical errors that trigger a panicStat: Statistical or metric information
The stdout writer is the default writer, but you can configure it explicitly:
logger, _ := slog.NewLogger(slog.WithStdIoWriter(&slog.ToStdStreamWriterOptions{
Stream: slog.StdOut, // or slog.StdErr
Format: slog.FormatAnsi,
Level: slog.DebugLevel,
}))Write logs to a file with optional rotation:
slog.AddWriter(slog.WithToFileWriter(&slog.ToFileWriterOptions{
FileName: "app.log",
Format: slog.FormatJson,
Level: slog.InfoLevel,
RotateSize: 1024 * 1024 * 100, // 100MB
}))Send logs to a remote endpoint:
slog.AddWriter(slog.WithToHttpWriter(&slog.ToHttpWriterOptions{
URL: "http://localhost:8080/logs",
Method: "POST",
Format: slog.FormatJson,
Level: slog.InfoLevel,
APIKey: "your-api-key-here",
}))Process logs using a custom channel handler:
logChan := make(chan *slog.Log, 100)
logger, _ := slog.NewLogger(slog.WithToChanWriter(&slog.ToChanWriterOptions{
Ch: logChan,
Level: slog.InfoLevel,
}))
// Process logs in a separate goroutine
go func() {
for log := range logChan {
// Custom log processing
fmt.Printf("Custom handler: [%s] %s\n", log.Type, log.Msg)
}
}()You can combine multiple writers to send logs to different destinations:
logger, _ := slog.NewLogger(
// Console output with ANSI colors
slog.WithStdIoWriter(&slog.ToStdStreamWriterOptions{
Stream: slog.StdOut,
Format: slog.FormatAnsi,
Level: slog.DebugLevel,
}),
// JSON file output
slog.WithToFileWriter(&slog.ToFileWriterOptions{
FileName: "app.log",
Format: slog.FormatJson,
Level: slog.InfoLevel,
}),
)
slog.SetLogger(logger)The library supports two main formatting options:
FormatAnsi: Colored output for terminal (default for stdout)FormatJson: JSON structured logging (ideal for file and HTTP writers)
For more detailed examples, check out the examples directory:
examples/stdout/: Basic logging with stdoutexamples/file/: File logging with rotationexamples/http/: Remote logging via HTTPexamples/channel/: Custom log processing with channelsexamples/multiple/: Using multiple writersexamples/advanced/: Advanced usage patterns
This project is licensed under the MIT License - see the LICENSE file for details.