-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
120 lines (96 loc) · 3.14 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
package logger
import (
"fmt"
"log"
"os"
)
type LogLevel int
const (
// LevelDebug is indented only for debugging errors and misbehavior.
// Do not use this in a production environment to improve performance.
LevelDebug LogLevel = 0 + iota
// LevelInfo is useful for application usage analysis
LevelInfo
// LevelWarn is used for non-critical situations that require immediate abort (panic).
LevelWarn
// LevelFatal indicates a critical situation requiring immediate abort (panic).
// The application is terminated with a SIGTERM and returns 1 as exit code.
LevelFatal
)
// osExit is a variable for testing purposes
var osExit = os.Exit
// A Logger allows messages with different levels/priorities to be sent to stderr/stdout
type Logger struct {
level LogLevel `min:"0" max:"3"`
loggerList [4]*log.Logger
}
// NewLogger returns a new logger at the debug level
func NewLogger() *Logger {
return &Logger{
level: LevelDebug,
loggerList: [4]*log.Logger{
log.New(os.Stdout, "[\u001B[0;37mDEBUG\u001B[0m] ", log.Ldate|log.Ltime|log.Lshortfile),
log.New(os.Stdout, "[\u001B[0;32mINFO\u001B[0m] ", log.Ldate|log.Ltime|log.Lshortfile),
log.New(os.Stderr, "[\u001B[0;33mWARNING\u001B[0m] ", log.Ldate|log.Ltime|log.Lshortfile),
log.New(os.Stderr, "[\u001B[0;31mFATAL\u001B[0m] ", log.Ldate|log.Ltime|log.Lshortfile),
},
}
}
// Level returns the current log level
func (l *Logger) Level() LogLevel {
return l.level
}
// SetLevel sets the current log level to the desired value. Avoid misbehavior by using package defined constants.
func (l *Logger) SetLevel(level LogLevel) {
l.level = level
}
// logWithLevel writes message to stderr/stdout in accordance with its level
func (l *Logger) logWithLevel(level LogLevel, format *string, v ...any) {
if l.level > level {
return
}
var err error
if format == nil {
err = l.loggerList[level].Output(3, fmt.Sprint(v...))
} else {
err = l.loggerList[level].Output(3, fmt.Sprintf(*format, v...))
}
if err != nil {
fmt.Println(err)
}
if level == LevelFatal {
osExit(1)
}
}
// Debug writes debug level messages
func (l *Logger) Debug(v ...any) {
l.logWithLevel(LevelDebug, nil, v...)
}
// Debugf writes debug level messages using formatted string
func (l *Logger) Debugf(format string, v ...any) {
l.logWithLevel(LevelDebug, &format, v...)
}
// Info writes info level messages
func (l *Logger) Info(v ...any) {
l.logWithLevel(LevelInfo, nil, v...)
}
// Infof writes info level messages using formatted string
func (l *Logger) Infof(format string, v ...any) {
l.logWithLevel(LevelInfo, &format, v...)
}
// Warn writes warn level messages
func (l *Logger) Warn(v ...any) {
l.logWithLevel(LevelWarn, nil, v...)
}
// Warnf writes warn level messages using formatted string
func (l *Logger) Warnf(format string, v ...any) {
l.logWithLevel(LevelWarn, &format, v...)
}
// Fatal writes fatal level messages and terminates the application
func (l *Logger) Fatal(v ...any) {
l.logWithLevel(LevelFatal, nil, v...)
}
// Fatalf writes fatal level messages using formatted string and terminates the application
func (l *Logger) Fatalf(format string, v ...any) {
l.logWithLevel(LevelFatal, &format, v...)
}