-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
123 lines (105 loc) · 3.04 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package getstream
import (
"io"
"log"
"os"
"sync"
)
// LogLevel represents the severity of a log message.
type LogLevel int
const (
// LogLevelDebug is the lowest severity.
LogLevelDebug LogLevel = iota
// LogLevelInfo is for general information.
LogLevelInfo
// LogLevelWarn is for warning messages.
LogLevelWarn
// LogLevelError is for error messages.
LogLevelError
)
// Logger is an interface that clients can implement to provide custom logging.
type Logger interface {
Debug(format string, v ...interface{})
Info(format string, v ...interface{})
Warn(format string, v ...interface{})
Error(format string, v ...interface{})
}
// DefaultLogger is the default implementation of the Logger interface.
type DefaultLogger struct {
logger *log.Logger
level LogLevel
mu sync.Mutex
}
// NewDefaultLogger creates a new DefaultLogger instance.
func NewDefaultLogger(out io.Writer, prefix string, flag int, level LogLevel) *DefaultLogger {
return &DefaultLogger{
logger: log.New(out, prefix, flag),
level: level,
}
}
// SetLevel sets the logging level.
func (l *DefaultLogger) SetLevel(level LogLevel) {
l.mu.Lock()
defer l.mu.Unlock()
l.level = level
}
// Debug logs a debug message.
func (l *DefaultLogger) Debug(format string, v ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
if l.level <= LogLevelDebug {
l.logger.Printf("[DEBUG] "+format, v...)
}
}
// Info logs an info message.
func (l *DefaultLogger) Info(format string, v ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
if l.level <= LogLevelInfo {
l.logger.Printf("[INFO] "+format, v...)
}
}
// Warn logs a warning message.
func (l *DefaultLogger) Warn(format string, v ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
if l.level <= LogLevelWarn {
l.logger.Printf("[WARN] "+format, v...)
}
}
// Error logs an error message.
func (l *DefaultLogger) Error(format string, v ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
if l.level <= LogLevelError {
l.logger.Printf("[ERROR] "+format, v...)
}
}
// DefaultLoggerInstance is the default logger instance.
var DefaultLoggerInstance Logger = NewDefaultLogger(os.Stderr, "", log.LstdFlags, LogLevelInfo)
// SetDefaultLogger sets the default logger.
func SetDefaultLogger(logger Logger) {
DefaultLoggerInstance = logger
}
// SetDefaultLogLevel sets the log level for the default logger if it is a DefaultLogger.
func SetDefaultLogLevel(level LogLevel) {
if logger, ok := DefaultLoggerInstance.(*DefaultLogger); ok {
logger.SetLevel(level)
}
}
// Debug logs a debug message using the default logger.
func Debug(format string, v ...interface{}) {
DefaultLoggerInstance.Debug(format, v...)
}
// Info logs an info message using the default logger.
func Info(format string, v ...interface{}) {
DefaultLoggerInstance.Info(format, v...)
}
// Warn logs a warning message using the default logger.
func Warn(format string, v ...interface{}) {
DefaultLoggerInstance.Warn(format, v...)
}
// Error logs an error message using the default logger.
func Error(format string, v ...interface{}) {
DefaultLoggerInstance.Error(format, v...)
}