-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogger.go
More file actions
210 lines (173 loc) · 5.33 KB
/
logger.go
File metadata and controls
210 lines (173 loc) · 5.33 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
206
207
208
209
210
package koryxserv
import (
"fmt"
"io"
"log"
"os"
"time"
)
// Logger manages application logs
type Logger struct {
config *LoggingConfig
accessLog *log.Logger
errorLog *log.Logger
infoLog *log.Logger
debugLog *log.Logger
colorOutput bool
}
// ANSI colors
const (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorBlue = "\033[34m"
colorPurple = "\033[35m"
colorCyan = "\033[36m"
colorWhite = "\033[37m"
colorGray = "\033[90m"
)
// NewLogger creates a new logger
func NewLogger(config *LoggingConfig) (*Logger, error) {
logger := &Logger{
config: config,
colorOutput: config.ColorOutput,
}
var writer io.Writer = os.Stdout
// If a log file is specified, write to it as well
if config.LogFile != "" {
file, err := os.OpenFile(config.LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
writer = io.MultiWriter(os.Stdout, file)
logger.colorOutput = false // Disable colors in files
}
logger.accessLog = log.New(writer, "", 0)
logger.errorLog = log.New(writer, "", 0)
logger.infoLog = log.New(writer, "", 0)
logger.debugLog = log.New(writer, "", 0)
return logger, nil
}
// colorize adds color to text when enabled
func (l *Logger) colorize(color, text string) string {
if l.colorOutput {
return color + text + colorReset
}
return text
}
// formatTime formats the timestamp
func (l *Logger) formatTime() string {
return time.Now().Format("2006-01-02 15:04:05")
}
// Access records an access log entry
func (l *Logger) Access(method, path string, status int, duration time.Duration, remoteAddr string) {
if !l.config.Enabled || !l.config.AccessLog {
return
}
statusColor := colorGreen
if status >= 400 && status < 500 {
statusColor = colorYellow
} else if status >= 500 {
statusColor = colorRed
}
timestamp := l.colorize(colorGray, l.formatTime())
methodStr := l.colorize(colorBlue, method)
pathStr := l.colorize(colorCyan, path)
statusStr := l.colorize(statusColor, fmt.Sprintf("%d", status))
durationStr := l.colorize(colorGray, duration.String())
remoteStr := l.colorize(colorGray, remoteAddr)
l.accessLog.Printf("[%s] %s %s - %s - %s - %s\n",
timestamp, methodStr, pathStr, statusStr, durationStr, remoteStr)
}
// Error records an error log entry
func (l *Logger) Error(format string, v ...interface{}) {
if !l.config.Enabled || !l.config.ErrorLog {
return
}
timestamp := l.colorize(colorGray, l.formatTime())
level := l.colorize(colorRed, "ERROR")
message := fmt.Sprintf(format, v...)
l.errorLog.Printf("[%s] [%s] %s\n", timestamp, level, message)
}
// Info records an informational log entry
func (l *Logger) Info(format string, v ...interface{}) {
if !l.config.Enabled {
return
}
if l.config.Level == "error" || l.config.Level == "warn" {
return
}
timestamp := l.colorize(colorGray, l.formatTime())
level := l.colorize(colorGreen, "INFO")
message := fmt.Sprintf(format, v...)
l.infoLog.Printf("[%s] [%s] %s\n", timestamp, level, message)
}
// Warn records a warning log entry
func (l *Logger) Warn(format string, v ...interface{}) {
if !l.config.Enabled {
return
}
if l.config.Level == "error" {
return
}
timestamp := l.colorize(colorGray, l.formatTime())
level := l.colorize(colorYellow, "WARN")
message := fmt.Sprintf(format, v...)
l.infoLog.Printf("[%s] [%s] %s\n", timestamp, level, message)
}
// Debug records a debug log entry
func (l *Logger) Debug(format string, v ...interface{}) {
if !l.config.Enabled || l.config.Level != "debug" {
return
}
timestamp := l.colorize(colorGray, l.formatTime())
level := l.colorize(colorPurple, "DEBUG")
message := fmt.Sprintf(format, v...)
l.debugLog.Printf("[%s] [%s] %s\n", timestamp, level, message)
}
// PrintBanner prints the startup banner
func (l *Logger) PrintBanner(config *Config) {
if !l.config.Enabled {
return
}
banner := `
╔═══════════════════════════════════════╗
║ ║
║ KORYX SERV - File Server ║
║ ║
╚═══════════════════════════════════════╝
`
fmt.Println(l.colorize(colorCyan, banner))
protocol := "HTTP"
if config.Security.EnableHTTPS {
protocol = "HTTPS"
}
l.Info("Server starting...")
l.Info("Protocol: %s", protocol)
l.Info("Host: %s", config.Server.Host)
l.Info("Port: %d", config.Server.Port)
l.Info("Root Directory: %s", config.Server.RootDir)
l.Info("Directory Listing: %v", config.Features.DirectoryListing)
l.Info("SPA Mode: %v", config.Features.SPAMode)
if config.Security.BasicAuth != nil && config.Security.BasicAuth.Enabled {
l.Info("Basic Auth: Enabled")
}
if config.Security.CORS != nil && config.Security.CORS.Enabled {
l.Info("CORS: Enabled")
}
if config.Security.RateLimit != nil && config.Security.RateLimit.Enabled {
l.Info("Rate Limit: %d req/min", config.Security.RateLimit.RequestsPerIP)
}
if config.Performance.EnableCompression {
l.Info("Compression: Enabled (level %d)", config.Performance.CompressionLevel)
}
fmt.Println()
l.Info("%s Server running at %s://%s:%d",
l.colorize(colorGreen, "✓"),
protocol,
config.Server.Host,
config.Server.Port)
l.Info("Press Ctrl+C to stop")
fmt.Println()
}