-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxzlog.go
113 lines (100 loc) · 2.37 KB
/
xzlog.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
package xzlog
import (
"fmt"
"log"
"os"
"time"
)
type LogLevel int
const (
Verbose LogLevel = iota
Debug
Info
Warning
Error
Fatal
)
var logLevelNames = map[LogLevel]string{
Verbose: "Verbose",
Debug: "Debug",
Info: "Info",
Warning: "Warning",
Error: "Error",
Fatal: "Fatal",
}
type Logger struct {
Output *os.File
LogLevel LogLevel
LogToFile bool
LogFilePath string
}
var DefaultLogger = &Logger{
Output: os.Stderr,
LogLevel: Info,
LogToFile: true, // Default to not log to a file
}
type LogCategory struct {
Name string
}
func DeclareLogCategory(name string) *LogCategory {
return &LogCategory{
Name: name,
}
}
// ANSI escape codes for colors
const (
Cyan = "\033[36m"
Red = "\033[31m"
Yellow = "\033[33m"
Reset = "\033[0m"
Purple = "\033[35m"
)
func (l *Logger) Log(category *LogCategory, level LogLevel, v ...interface{}) {
if level >= l.LogLevel {
levelName := logLevelNames[level]
now := time.Now().Format("2006-01-02 15:04:05.999")
rawMessage := fmt.Sprintf("[%s] %s: %s: %v\n", now, category.Name, levelName, fmt.Sprint(v...))
var cmessage string
switch level {
case Verbose:
cmessage = Cyan + rawMessage + Reset
case Debug:
cmessage = Purple + rawMessage + Reset
case Warning:
cmessage = Yellow + rawMessage + Reset
case Error:
cmessage = Red + rawMessage + Reset
case Fatal:
cmessage = Red + rawMessage + "Exiting... " + Reset
l.Output.Write([]byte(cmessage))
os.Exit(1)
}
l.Output.Write([]byte(cmessage))
// Wow that was some garbage tier golang, but it works
// Now on to the logfile functionality
// If the user specified their own path, create a file there
// If not, create a file in the current directory
if l.LogToFile {
if l.LogFilePath == "" {
// Default to the date
l.LogFilePath = time.Now().Format("2006-01-02") + ".log"
}
f, err := os.OpenFile(l.LogFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if _, err := f.Write([]byte(rawMessage)); err != nil {
log.Fatal(err)
}
}
}
}
// Log with the default logger
func Log(level LogLevel, category *LogCategory, v ...interface{}) {
DefaultLogger.Log(category, level, v...)
}
// There is no reason for this to exist, but I'm keeping it here for completeness
func SetDefaultLogLevel(level LogLevel) {
DefaultLogger.LogLevel = level
}