-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
205 lines (178 loc) · 5.11 KB
/
logger.go
File metadata and controls
205 lines (178 loc) · 5.11 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package disk
import (
"fmt"
"io"
"log"
"os"
"strings"
"time"
)
// LogLevel represents the severity level of a log message
type LogLevel int
const (
DEBUG LogLevel = iota
INFO
WARN
ERROR
SILENT // No logging
)
// String returns the string representation of a LogLevel
func (l LogLevel) String() string {
switch l {
case DEBUG:
return "DEBUG"
case INFO:
return "INFO"
case WARN:
return "WARN"
case ERROR:
return "ERROR"
case SILENT:
return "SILENT"
default:
return "UNKNOWN"
}
}
// LoggerConfig holds configuration for the logger
type LoggerConfig struct {
Level LogLevel // Minimum log level to output
Output io.Writer // Where to write logs (default: os.Stdout)
Prefix string // Prefix for log messages
TimeFormat string // Time format for timestamps
Structured bool // Enable structured logging
Verbose bool // Enable verbose mode (includes DEBUG level)
SanitizeAuth bool // Sanitize authorization headers in logs
}
// DefaultLoggerConfig returns a LoggerConfig with sensible defaults
func DefaultLoggerConfig() *LoggerConfig {
return &LoggerConfig{
Level: INFO,
Output: os.Stdout,
Prefix: "[disk] ",
TimeFormat: "2006-01-02 15:04:05",
Structured: true,
Verbose: false,
SanitizeAuth: true,
}
}
// DiskLogger provides structured logging with multiple levels
type DiskLogger struct {
config *LoggerConfig
logger *log.Logger
}
// NewLogger creates a new DiskLogger with the given configuration
func NewLogger(config *LoggerConfig) *DiskLogger {
if config == nil {
config = DefaultLoggerConfig()
}
// Set DEBUG level if verbose mode is enabled
if config.Verbose && config.Level > DEBUG {
config.Level = DEBUG
}
return &DiskLogger{
config: config,
logger: log.New(config.Output, config.Prefix, 0), // We'll handle timestamps ourselves
}
}
// shouldLog checks if a message at the given level should be logged
func (l *DiskLogger) shouldLog(level LogLevel) bool {
return level >= l.config.Level && l.config.Level != SILENT
}
// formatMessage formats a log message with timestamp and level
func (l *DiskLogger) formatMessage(level LogLevel, format string, args ...interface{}) string {
timestamp := time.Now().Format(l.config.TimeFormat)
message := fmt.Sprintf(format, args...)
if l.config.Structured {
return fmt.Sprintf("[%s] %s: %s", timestamp, level.String(), message)
}
return fmt.Sprintf("[%s] %s", timestamp, message)
}
// Debug logs a debug message
func (l *DiskLogger) Debug(format string, args ...interface{}) {
if l.shouldLog(DEBUG) {
l.logger.Print(l.formatMessage(DEBUG, format, args...))
}
}
// Info logs an info message
func (l *DiskLogger) Info(format string, args ...interface{}) {
if l.shouldLog(INFO) {
l.logger.Print(l.formatMessage(INFO, format, args...))
}
}
// Warn logs a warning message
func (l *DiskLogger) Warn(format string, args ...interface{}) {
if l.shouldLog(WARN) {
l.logger.Print(l.formatMessage(WARN, format, args...))
}
}
// Error logs an error message
func (l *DiskLogger) Error(format string, args ...interface{}) {
if l.shouldLog(ERROR) {
l.logger.Print(l.formatMessage(ERROR, format, args...))
}
}
// SetLevel updates the minimum log level
func (l *DiskLogger) SetLevel(level LogLevel) {
l.config.Level = level
}
// SetVerbose enables or disables verbose mode
func (l *DiskLogger) SetVerbose(verbose bool) {
l.config.Verbose = verbose
if verbose && l.config.Level > DEBUG {
l.config.Level = DEBUG
}
}
// SetOutput changes the output destination for logs
func (l *DiskLogger) SetOutput(output io.Writer) {
l.config.Output = output
l.logger.SetOutput(output)
}
// SanitizeValue sanitizes sensitive information for logging
func (l *DiskLogger) SanitizeValue(key, value string) string {
if !l.config.SanitizeAuth {
return value
}
lowerKey := strings.ToLower(key)
if strings.Contains(lowerKey, "auth") ||
strings.Contains(lowerKey, "token") ||
strings.Contains(lowerKey, "key") ||
strings.Contains(lowerKey, "secret") {
if len(value) <= 8 {
return "***"
}
return value[:4] + "***" + value[len(value)-2:]
}
return value
}
// LogRequest logs HTTP request details
func (l *DiskLogger) LogRequest(method, url string, headers map[string]string) {
if !l.shouldLog(DEBUG) {
return
}
l.Debug("HTTP Request: %s %s", method, url)
if l.config.Verbose {
for key, value := range headers {
sanitizedValue := l.SanitizeValue(key, value)
if sanitizedValue != value {
l.Debug(" Header: %s: %s", key, sanitizedValue)
} else {
l.Debug(" Header: %s: [sanitized]", key)
}
}
}
}
// LogResponse logs HTTP response details
func (l *DiskLogger) LogResponse(statusCode int, contentLength int64, duration time.Duration) {
if l.shouldLog(DEBUG) {
l.Debug("HTTP Response: %d (Content-Length: %d, Duration: %v)",
statusCode, contentLength, duration)
} else if l.shouldLog(INFO) && statusCode >= 400 {
l.Info("HTTP Error Response: %d (Duration: %v)", statusCode, duration)
}
}
// LogError logs an error with context
func (l *DiskLogger) LogError(operation string, err error) {
if l.shouldLog(ERROR) {
l.Error("Operation '%s' failed: %v", operation, err)
}
}