feat: some refactoring
This commit is contained in:
76
internal/apiserver/logger/logger.go
Normal file
76
internal/apiserver/logger/logger.go
Normal file
@@ -0,0 +1,76 @@
|
||||
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
|
||||
if output != "" {
|
||||
//TODO: Use files
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user