-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
109 lines (92 loc) · 3.84 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
package msmtpd
import (
"fmt"
"log"
)
// Logger is interface all Server loggers must satisfy
type Logger interface {
Tracef(transaction *Transaction, format string, args ...any)
Debugf(transaction *Transaction, format string, args ...any)
Infof(transaction *Transaction, format string, args ...any)
Warnf(transaction *Transaction, format string, args ...any)
Errorf(transaction *Transaction, format string, args ...any)
Fatalf(transaction *Transaction, format string, args ...any)
}
// LoggerLevel describes logging level like JournalD has by
// https://github.com/coreos/go-systemd/blob/main/journal/journal.go
// See for inspiration https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels
type LoggerLevel uint8
// TraceLevel is used when we record very verbose message like SMTP protocol raw data being sent/received
const TraceLevel LoggerLevel = 8
// DebugLevel is used when we log information that is diagnostically helpful to people more than just developers (IT, sysadmins, etc.).
const DebugLevel LoggerLevel = 7
// InfoLevel is used when we log generally useful information to log
// (service start/stop, configuration assumptions, etc).
// This is information we always want to be available but we usually don't care about
// under normal circumstances. This is my out-of-the-box config level.
const InfoLevel LoggerLevel = 6
// WarnLevel is used when we log anything that can potentially cause application oddities,
// but for which we are automatically recovering.
// Such as switching from a primary to backup server, retrying an operation, missing secondary data, etc.
const WarnLevel LoggerLevel = 4
// ErrorLevel is used for any error which is fatal to the operation, but not the service or application (can't open a required file, missing data, etc.).
// These errors will force user (administrator, or direct user) intervention.
const ErrorLevel LoggerLevel = 3
// FatalLevel is used for any error that is forcing a shutdown of the service or
// application to prevent data loss (or further data loss).
const FatalLevel LoggerLevel = 2
func (ll LoggerLevel) String() string {
switch ll {
case TraceLevel:
return "TRACE"
case DebugLevel:
return "DEBUG"
case InfoLevel:
return "INFO"
case WarnLevel:
return "WARN"
case ErrorLevel:
return "ERROR"
default:
return fmt.Sprintf("LEVEL%d", ll)
}
}
// DefaultLogger is logger by default using standard library logger as backend https://pkg.go.dev/log
type DefaultLogger struct {
*log.Logger
Level LoggerLevel
}
// Tracef sends TraceLevel message
func (d *DefaultLogger) Tracef(transaction *Transaction, format string, args ...any) {
if d.Level >= TraceLevel {
d.Printf("TRACE [%s]: %s", transaction.ID, fmt.Sprintf(format, args...))
}
}
// Debugf sends DebugLevel message
func (d *DefaultLogger) Debugf(transaction *Transaction, format string, args ...any) {
if d.Level >= DebugLevel {
d.Printf("DEBUG [%s]: %s", transaction.ID, fmt.Sprintf(format, args...))
}
}
// Infof sends InfoLevel message
func (d *DefaultLogger) Infof(transaction *Transaction, format string, args ...any) {
if d.Level >= InfoLevel {
d.Printf("INFO [%s]: %s", transaction.ID, fmt.Sprintf(format, args...))
}
}
// Warnf sends WarnLevel message
func (d *DefaultLogger) Warnf(transaction *Transaction, format string, args ...any) {
if d.Level >= WarnLevel {
d.Printf("WARN [%s]: %s", transaction.ID, fmt.Sprintf(format, args...))
}
}
// Errorf sends ErrorLevel message
func (d *DefaultLogger) Errorf(transaction *Transaction, format string, args ...any) {
if d.Level >= ErrorLevel {
d.Printf("ERROR [%s]: %s", transaction.ID, fmt.Sprintf(format, args...))
}
}
// Fatalf sends FatalLevel message and stops application with exit code 1
func (d *DefaultLogger) Fatalf(transaction *Transaction, format string, args ...any) {
d.Logger.Fatalf("FATAL [%s]: %s", transaction.ID, fmt.Sprintf(format, args...))
}