loggerrific is a logging interface for abstracting the choice of logging framework for Go applications. It provides a unified API that allows you to switch between different logging implementations without changing your application code.
- Framework Agnostic: Works with any logging framework
- Structured Logging: Support for fields and key-value pairs
- Level Control: Dynamic log level management
- Zero Dependencies: Pure Go with no external dependencies
- Testing Support: Built-in testing logger for unit tests
- Performance: Includes no-op implementation for high-performance scenarios
package main
import (
"github.com/CallumKerson/loggerrific"
"github.com/CallumKerson/loggerrific/noop"
)
func main() {
// Use the no-op logger for high performance
logger := noop.New()
// Or use your preferred logging framework implementation
// logger := mylogger.New()
// Log with structured data
logger.WithField("user_id", 12345).
WithField("action", "login").
Infof("User logged in successfully")
// Set log levels dynamically
logger.SetLevelDebug()
// Check if logging is enabled before expensive operations
if logger.IsDebugEnabled() {
logger.Debugf("Debug info: %+v", expensiveDebugData())
}
}Perfect for production environments where logging overhead needs to be minimized:
import "github.com/CallumKerson/loggerrific/noop"
logger := noop.New()
logger.Infof("This won't output anything") // Zero overheadIntegrates with Go's testing framework for unit tests:
import (
"testing"
"github.com/CallumKerson/loggerrific/tlogger"
)
func TestMyFunction(t *testing.T) {
logger := tlogger.NewTLogger(t)
// Logs will appear in test output
myFunction(logger)
}The core Logger interface provides:
type Logger interface {
// Structured logging
WithField(key string, value interface{}) Entry
WithFields(fields map[string]interface{}) Entry
WithError(err error) Entry
// Level management
SetLevelDebug()
SetLevelInfo()
SetLevelWarn()
SetLevelError()
// Formatted logging
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
// Line logging
Debugln(args ...interface{})
Infoln(args ...interface{})
Warnln(args ...interface{})
Errorln(args ...interface{})
// Level checking
IsDebugEnabled() bool
IsInfoEnabled() bool
IsWarnEnabled() bool
IsErrorEnabled() bool
}An example implementation for Logrus:
package logrus
import (
"github.com/sirupsen/logrus"
"github.com/CallumKerson/loggerrific"
)
type Logger struct {
*logrus.Logger
}
func NewLogger() *Logger {
logrusLogger := &Logger{
logrus.New(),
}
logrusLogger.SetLevel(logrus.InfoLevel)
return logrusLogger
}
func (l *Logger) WithField(key string, value interface{}) loggerrific.Entry {
return l.Logger.WithField(key, value)
}
func (l *Logger) WithFields(fields map[string]interface{}) loggerrific.Entry {
return l.Logger.WithFields(fields)
}
func (l *Logger) WithError(err error) loggerrific.Entry {
return l.Logger.WithError(err)
}
func (l *Logger) SetLevelDebug() {
l.SetLevel(logrus.DebugLevel)
}
func (l *Logger) SetLevelInfo() {
l.SetLevel(logrus.InfoLevel)
}
func (l *Logger) SetLevelWarn() {
l.SetLevel(logrus.WarnLevel)
}
func (l *Logger) SetLevelError() {
l.SetLevel(logrus.ErrorLevel)
}
func (l *Logger) IsDebugEnabled() bool {
return l.IsLevelEnabled(logrus.DebugLevel)
}
func (l *Logger) IsInfoEnabled() bool {
return l.IsLevelEnabled(logrus.InfoLevel)
}
func (l *Logger) IsWarnEnabled() bool {
return l.IsLevelEnabled(logrus.WarnLevel)
}
func (l *Logger) IsErrorEnabled() bool {
return l.IsLevelEnabled(logrus.ErrorLevel)
}
go get github.com/CallumKerson/loggerrificThis project uses Task for development workflows:
# Run tests
task test
# Run tests with coverage
task test:cover
# Check code formatting
task fmt:check
# Run all quality checks
task check
# Run pre-commit workflow (format + checks)
task precommit
# See all available tasks
task --list- Fork the repository
- Create a feature branch
- Make your changes
- Run
task precommitto ensure quality - Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.