-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclog.go
193 lines (162 loc) · 5.26 KB
/
clog.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
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
// Package clog provides an enhanced logging system with colored output, multiple log levels,
// and debug features for Go applications.
//
// Basic usage:
//
// clog.Info("Starting application...")
// clog.Success("Server started on port %d", 8080)
// clog.Error("Failed to connect: %v", err)
//
// Log Levels (from lowest to highest):
// - PanicLevel: System is unusable, halts execution
// - ErrorLevel: Error events that might still allow the application to continue running
// - WarningLevel: Warning messages for potentially harmful situations
// - InfoLevel: General informational messages about system operation
// - DebugLevel: Detailed information for debugging
// - TraceLevel: Extremely detailed debugging information
//
// Configuration:
//
// clog.SetLogLevel(clog.DebugLevel)
// clog.SetShowFileLine(true)
// clog.SetShowGoroutineID(true)
//
// Features:
// - Colored output using emoji prefixes
// - UTC timestamp with millisecond precision
// - File and line number tracking
// - Goroutine ID tracking
// - Performance metrics logging
// - Multiple log levels with filtering
// - Panic handling with stack traces
package clog
import (
"fmt"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/fatih/color"
)
var (
// Existing prefixes
infoPrefix = color.New(color.FgCyan).Sprint("ℹ️ INFO ")
successPrefix = color.New(color.FgGreen).Sprint("✅ SUCCESS ")
initPrefix = color.New(color.FgBlue).Sprint("🚀 INIT ")
configPrefix = color.New(color.FgMagenta).Sprint("⚙️ CONFIG ")
warningPrefix = color.New(color.FgYellow).Sprint("⚠️ WARNING ")
errorPrefix = color.New(color.FgRed).Sprint("❌ ERROR ")
// New debug prefixes
debugPrefix = color.New(color.FgWhite).Sprint("🔍 DEBUG ")
tracePrefix = color.New(color.FgHiBlue).Sprint("📍 TRACE ")
panicPrefix = color.New(color.FgHiRed, color.Bold).Sprint("💥 PANIC ")
metricPrefix = color.New(color.FgHiMagenta).Sprint("📊 METRIC ")
)
// LogLevel type for controlling log output
type LogLevel int
const (
// PanicLevel logs and then calls panic()
PanicLevel LogLevel = iota
// ErrorLevel indicates error conditions
ErrorLevel
// WarningLevel indicates potentially harmful situations
WarningLevel
// InfoLevel indicates general operational information
InfoLevel
// DebugLevel indicates detailed debug information
DebugLevel
// TraceLevel indicates the most detailed debugging information
TraceLevel
)
var (
currentLogLevel = InfoLevel
showFileLine = true
showGoroutineID = true
)
// SetLogLevel sets the current logging level
func SetLogLevel(level LogLevel) {
currentLogLevel = level
}
// SetShowFileLine enables/disables file and line number in logs
func SetShowFileLine(show bool) {
showFileLine = show
}
// SetShowGoroutineID enables/disables goroutine ID in logs
func SetShowGoroutineID(show bool) {
showGoroutineID = show
}
func getFileInfo() string {
if !showFileLine {
return ""
}
_, file, line, ok := runtime.Caller(3) // Skip 3 frames to get to the caller
if !ok {
return ""
}
return fmt.Sprintf("%s:%d", filepath.Base(file), line)
}
func getGoroutineID() string {
if !showGoroutineID {
return ""
}
buf := make([]byte, 64)
n := runtime.Stack(buf, false)
id := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
return fmt.Sprintf("(goroutine %s)", id)
}
func logWithTimestamp(prefix, msg string, level LogLevel) {
if level > currentLogLevel {
return
}
timestamp := time.Now().UTC().Format("2006-01-02 15:04:05.000 UTC")
fileInfo := getFileInfo()
goroutineInfo := getGoroutineID()
// Construct the log message
logMsg := fmt.Sprintf("%s [%s]", prefix, timestamp)
if fileInfo != "" {
logMsg += fmt.Sprintf(" [%s]", fileInfo)
}
if goroutineInfo != "" {
logMsg += fmt.Sprintf(" %s", goroutineInfo)
}
logMsg += fmt.Sprintf(" %s", msg)
fmt.Println(logMsg)
}
// Existing methods with log level support
func Info(format string, args ...interface{}) {
logWithTimestamp(infoPrefix, fmt.Sprintf(format, args...), InfoLevel)
}
func Success(format string, args ...interface{}) {
logWithTimestamp(successPrefix, fmt.Sprintf(format, args...), InfoLevel)
}
func Init(format string, args ...interface{}) {
logWithTimestamp(initPrefix, fmt.Sprintf(format, args...), InfoLevel)
}
func Config(format string, args ...interface{}) {
logWithTimestamp(configPrefix, fmt.Sprintf(format, args...), InfoLevel)
}
func Warning(format string, args ...interface{}) {
logWithTimestamp(warningPrefix, fmt.Sprintf(format, args...), WarningLevel)
}
func Error(format string, args ...interface{}) {
logWithTimestamp(errorPrefix, fmt.Sprintf(format, args...), ErrorLevel)
}
// New debug methods
func Debug(format string, args ...interface{}) {
logWithTimestamp(debugPrefix, fmt.Sprintf(format, args...), DebugLevel)
}
func Trace(format string, args ...interface{}) {
logWithTimestamp(tracePrefix, fmt.Sprintf(format, args...), TraceLevel)
}
func Panic(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
logWithTimestamp(panicPrefix, msg, PanicLevel)
panic(msg)
}
func Metric(name string, value interface{}, tags ...string) {
tagStr := ""
if len(tags) > 0 {
tagStr = fmt.Sprintf(" [%s]", strings.Join(tags, ", "))
}
logWithTimestamp(metricPrefix, fmt.Sprintf("%s: %v%s", name, value, tagStr), InfoLevel)
}