LFG
Some checks failed
CI/CD Pipeline / Run Tests (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled
CI/CD Pipeline / Build Docker Image (push) Has been cancelled
CI/CD Pipeline / Security Scan (push) Has been cancelled
CI/CD Pipeline / Create Release (push) Has been cancelled
Some checks failed
CI/CD Pipeline / Run Tests (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled
CI/CD Pipeline / Build Docker Image (push) Has been cancelled
CI/CD Pipeline / Security Scan (push) Has been cancelled
CI/CD Pipeline / Create Release (push) Has been cancelled
This commit is contained in:
124
internal/logger/logger.go
Normal file
124
internal/logger/logger.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Logger represents the application logger
|
||||
type Logger struct {
|
||||
debugLogger *log.Logger
|
||||
infoLogger *log.Logger
|
||||
warnLogger *log.Logger
|
||||
errorLogger *log.Logger
|
||||
}
|
||||
|
||||
// Field represents a log field
|
||||
type Field struct {
|
||||
Key string
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
// Option represents a logger option
|
||||
type Option func(*Logger)
|
||||
|
||||
// NewLogger creates a new logger with default settings
|
||||
func NewLogger(opts ...Option) *Logger {
|
||||
logger := &Logger{
|
||||
debugLogger: log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile),
|
||||
infoLogger: log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile),
|
||||
warnLogger: log.New(os.Stdout, "WARN: ", log.Ldate|log.Ltime|log.Lshortfile),
|
||||
errorLogger: log.New(os.Stdout, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile),
|
||||
}
|
||||
|
||||
// Apply options
|
||||
for _, opt := range opts {
|
||||
opt(logger)
|
||||
}
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
// Debug logs a debug message
|
||||
func (l *Logger) Debug(msg string, fields ...Field) {
|
||||
l.debugLogger.Printf(formatMessage(msg, fields...))
|
||||
}
|
||||
|
||||
// Info logs an info message
|
||||
func (l *Logger) Info(msg string, fields ...Field) {
|
||||
l.infoLogger.Printf(formatMessage(msg, fields...))
|
||||
}
|
||||
|
||||
// Warn logs a warning message
|
||||
func (l *Logger) Warn(msg string, fields ...Field) {
|
||||
l.warnLogger.Printf(formatMessage(msg, fields...))
|
||||
}
|
||||
|
||||
// Error logs an error message
|
||||
func (l *Logger) Error(msg string, fields ...Field) {
|
||||
l.errorLogger.Printf(formatMessage(msg, fields...))
|
||||
}
|
||||
|
||||
// Fatal logs a fatal message and exits
|
||||
func (l *Logger) Fatal(msg string, fields ...Field) {
|
||||
l.errorLogger.Printf(formatMessage(msg, fields...))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// String creates a string field
|
||||
func String(key, value string) Field {
|
||||
return Field{Key: key, Value: value}
|
||||
}
|
||||
|
||||
// Int creates an int field
|
||||
func Int(key string, value int) Field {
|
||||
return Field{Key: key, Value: value}
|
||||
}
|
||||
|
||||
// Bool creates a bool field
|
||||
func Bool(key string, value bool) Field {
|
||||
return Field{Key: key, Value: value}
|
||||
}
|
||||
|
||||
// Error creates an error field
|
||||
func Error(err error) Field {
|
||||
return Field{Key: "error", Value: err}
|
||||
}
|
||||
|
||||
// formatMessage formats a log message with fields
|
||||
func formatMessage(msg string, fields ...Field) string {
|
||||
if len(fields) == 0 {
|
||||
return msg
|
||||
}
|
||||
|
||||
result := msg + " ["
|
||||
for i, field := range fields {
|
||||
if i > 0 {
|
||||
result += ", "
|
||||
}
|
||||
result += field.Key + "="
|
||||
result += toString(field.Value)
|
||||
}
|
||||
result += "]"
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// toString converts a value to string
|
||||
func toString(value interface{}) string {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
return v
|
||||
case int:
|
||||
return string(v)
|
||||
case bool:
|
||||
if v {
|
||||
return "true"
|
||||
}
|
||||
return "false"
|
||||
case error:
|
||||
return v.Error()
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user