-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tangra.go
156 lines (130 loc) · 3.77 KB
/
tangra.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
package tangra
import (
"fmt"
"github.com/kkrypt0nn/tangra/terminal"
"os"
)
var (
CurrentLoggingLevel = NONE
CurrentDateFormat = "Jan 02, 2006"
CurrentDatetimeFormat = "Jan 02, 2006 15:04:05"
CurrentTimeFormat = "15:04:05"
ForceStyling = false
)
// Logger is represents a logger structure.
type Logger struct {
// Styling describes whether the logger should style the logged message.
Styling bool
// Prefix is the prefix before the logged message.
Prefix string
// LogFile is the log file to write the messages into.
LogFile *os.File
}
// NewLogger creates a new logger.
func NewLogger() *Logger {
return &Logger{
Styling: true,
Prefix: "${datetime} ${level:color}${level:name}${reset}: ",
LogFile: nil,
}
}
// SetForceStyling sets to whether it should force the styling render.
func (l *Logger) SetForceStyling(forceStyling bool) {
ForceStyling = forceStyling
}
// SetLogFile will set the log file to write logs into.
func (l *Logger) SetLogFile(file *os.File) {
l.LogFile = file
}
// SetDateFormat sets the format to use when logging the date.
func (l *Logger) SetDateFormat(format string) {
CurrentDateFormat = format
}
// SetDatetimeFormat sets the format to use when logging the date and time.
func (l *Logger) SetDatetimeFormat(format string) {
CurrentDatetimeFormat = format
}
// SetTimeFormat sets the format to use when logging the time.
func (l *Logger) SetTimeFormat(format string) {
CurrentTimeFormat = format
}
// SetStyling sets the styling setting of the logger.
func (l *Logger) SetStyling(styling bool) {
l.Styling = styling
}
// SetPrefix sets the prefix before the log message.
func (l *Logger) SetPrefix(prefix string) {
l.Prefix = prefix
}
// SetLevelColor sets the color of the level.
func (l *Logger) SetLevelColor(id Level, color string) {
LevelColors[id] = color
}
func (l *Logger) doLog(level Level, message string) {
CurrentLoggingLevel = level
// So that Print and Println do not keep the previous logging level.
defer l.SetLoggingLevel(NONE)
message = l.Prefix + message + terminal.RESET
message = AddVariables(message)
if (l.Styling && terminal.AreColorsSupported()) || (ForceStyling) {
message = AddStyling(message)
} else {
message = RemoveStyling(message)
}
fmt.Println(message)
if l.LogFile != nil {
message = RemoveStyling(message)
_, err := l.LogFile.WriteString(message + "\n")
if err != nil {
panic(err)
}
}
}
// Debug prints a debug message.
func (l *Logger) Debug(message string) {
l.doLog(DEBUG, message)
}
// Info prints an info message.
func (l *Logger) Info(message string) {
l.doLog(INFO, message)
}
// Warn prints a warning message.
func (l *Logger) Warn(message string) {
l.doLog(WARN, message)
}
// Error prints an error message.
func (l *Logger) Error(message string) {
l.doLog(ERROR, message)
}
// Fatal prints a fatal error message.
func (l *Logger) Fatal(message string) {
l.doLog(FATAL, message)
}
// Trace prints a tracing message.
func (l *Logger) Trace(message string) {
l.doLog(TRACE, message)
}
// Log will log with the given level.
func (l *Logger) Log(level Level, message string) {
l.doLog(level, message)
}
// SetLoggingLevel sets the logging level.
func (l *Logger) SetLoggingLevel(level Level) {
CurrentLoggingLevel = level
}
// Print simply prints the message, without logging level.
func (l *Logger) Print(message string) {
message = AddVariables(message)
if (l.Styling && terminal.AreColorsSupported()) || (ForceStyling) {
message = AddStyling(message)
}
fmt.Print(message + terminal.RESET)
}
// Println simply prints the message with a new line, without logging level.
func (l *Logger) Println(message string) {
message = AddVariables(message)
if (l.Styling && terminal.AreColorsSupported()) || (ForceStyling) {
message = AddStyling(message)
}
fmt.Println(message + terminal.RESET)
}