Files

75 lines
1.4 KiB
Go

package logger
import (
"sync"
"github.com/sirupsen/logrus"
)
var (
logger *logrus.Logger
once sync.Once
initialized bool
)
// Initialize logger with configuration
func Initialize(level string, format string, output string) {
once.Do(func() {
logger = logrus.New()
// Set level
lvl, err := logrus.ParseLevel(level)
if err != nil {
lvl = logrus.InfoLevel
}
logger.SetLevel(lvl)
// Set format
switch format {
case "json":
logger.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: "2006-01-02T15:04:05.000Z",
})
case "text":
logger.SetFormatter(&logrus.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
}
// Set output (currently not implemented)
// TODO: Implement file output support
initialized = true
})
}
// GetLogger returns the singleton logger instance
func GetLogger() *logrus.Logger {
if !initialized {
// Initialize with defaults
Initialize("info", "json", "stdout")
}
return logger
}
// WithFields creates a logger with additional fields
func WithFields(fields map[string]interface{}) *logrus.Entry {
return GetLogger().WithFields(fields)
}
// Info logs an info message
func Info(msg string) {
GetLogger().Info(msg)
}
// Error logs an error message
func Error(msg string) {
GetLogger().Error(msg)
}
// Debug logs a debug message
func Debug(msg string) {
GetLogger().Debug(msg)
}