-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
174 lines (146 loc) · 4.09 KB
/
Copy pathlogger.go
File metadata and controls
174 lines (146 loc) · 4.09 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
package logging
import (
"io"
"log/slog"
"os"
)
// Logger provides structured logging capabilities
type Logger struct {
*slog.Logger
}
// LogLevel represents log level constants
type LogLevel int
const (
LevelDebug LogLevel = iota
LevelInfo
LevelWarn
LevelError
)
// Config represents logger configuration
type Config struct {
Level LogLevel
Format string // "json" or "text"
Output io.Writer
}
// NewLogger creates a new structured logger
func NewLogger(config Config) *Logger {
var level slog.Level
switch config.Level {
case LevelDebug:
level = slog.LevelDebug
case LevelInfo:
level = slog.LevelInfo
case LevelWarn:
level = slog.LevelWarn
case LevelError:
level = slog.LevelError
default:
level = slog.LevelInfo
}
var handler slog.Handler
output := config.Output
if output == nil {
output = os.Stdout
}
opts := &slog.HandlerOptions{
Level: level,
}
if config.Format == "json" {
handler = slog.NewJSONHandler(output, opts)
} else {
handler = slog.NewTextHandler(output, opts)
}
return &Logger{
Logger: slog.New(handler),
}
}
// GetDefaultLogger returns a logger with sensible defaults
func GetDefaultLogger() *Logger {
return NewLogger(Config{
Level: LevelInfo,
Format: "text",
Output: os.Stdout,
})
}
// WithContext adds contextual fields to the logger
func (l *Logger) WithContext(args ...any) *Logger {
return &Logger{
Logger: l.With(args...),
}
}
// WithWorker adds worker ID context
func (l *Logger) WithWorker(workerID int) *Logger {
return l.WithContext("worker", workerID)
}
// WithProxy adds proxy context
func (l *Logger) WithProxy(proxy string) *Logger {
return l.WithContext("proxy", proxy)
}
// WithDuration adds duration context
func (l *Logger) WithDuration(key string, duration float64) *Logger {
return l.WithContext(key, duration)
}
// ConfigLoaded logs successful configuration loading
func (l *Logger) ConfigLoaded(file string) {
l.Info("Configuration loaded", "file", file)
}
// ConfigNotFound logs when config file is not found
func (l *Logger) ConfigNotFound(file string) {
l.Warn("Config file not found, using defaults", "file", file)
}
// ProxiesLoaded logs successful proxy loading
func (l *Logger) ProxiesLoaded(count int, file string) {
l.Info("Proxies loaded", "count", count, "file", file)
}
// ProxyCheckStart logs start of proxy checking
func (l *Logger) ProxyCheckStart(total int, concurrency int) {
l.Info("Starting proxy checks", "total", total, "concurrency", concurrency)
}
// ProxyCheckComplete logs completion of proxy checking
func (l *Logger) ProxyCheckComplete() {
l.Info("Proxy checking complete")
}
// ProxySuccess logs successful proxy check
func (l *Logger) ProxySuccess(proxy string, duration float64, anonymous bool, cloudProvider string) {
logger := l.WithProxy(proxy).WithDuration("duration_seconds", duration)
if anonymous {
logger = logger.WithContext("anonymous", true)
}
if cloudProvider != "" {
logger = logger.WithContext("cloud_provider", cloudProvider)
}
logger.Info("Proxy check successful")
}
// ProxyFailure logs failed proxy check
func (l *Logger) ProxyFailure(proxy string, err error) {
l.WithProxy(proxy).Error("Proxy check failed", "error", err)
}
// WorkerStart logs worker startup
func (l *Logger) WorkerStart(workerID int) {
l.WithWorker(workerID).Debug("Worker started")
}
// WorkerStop logs worker shutdown
func (l *Logger) WorkerStop(workerID int) {
l.WithWorker(workerID).Debug("Worker stopped")
}
// ShutdownReceived logs shutdown signal
func (l *Logger) ShutdownReceived() {
l.Info("Shutdown signal received, cleaning up...")
}
// ShutdownComplete logs shutdown completion
func (l *Logger) ShutdownComplete() {
l.Info("Shutdown complete")
}
// ResultsSaved logs when results are saved to file
func (l *Logger) ResultsSaved(file string, format string) {
l.Info("Results saved", "file", file, "format", format)
}
// SummaryStats logs summary statistics
func (l *Logger) SummaryStats(total, working, anonymous int, successRate float64) {
l.Info("Summary statistics",
"total_proxies", total,
"working_proxies", working,
"anonymous_proxies", anonymous,
"success_rate_percent", successRate,
)
}