-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.go
142 lines (115 loc) · 3.44 KB
/
console.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
package go_logger
import (
"fmt"
"io"
"os"
"sync"
"time"
"github.com/gookit/color"
)
//------------------------------------------------------------------------------
// ConsoleOptions specifies the console logger settings to use when it is created.
type ConsoleOptions struct {
// Disable console output.
Disable bool `json:"disable,omitempty"`
// Set the initial logging level to use.
Level *LogLevel `json:"level,omitempty"`
// Set the initial logging level for debug output to use.
DebugLevel *uint `json:"debugLevel,omitempty"`
}
type consoleAdapter struct {
themedLevels [4]string
globals globalOptions
}
//------------------------------------------------------------------------------
var consoleMtx = sync.Mutex{}
//------------------------------------------------------------------------------
func createConsoleAdapter(opts ConsoleOptions, glbOpts globalOptions) internalLogger {
// Create console adapter
lg := &consoleAdapter{
globals: glbOpts,
}
if color.IsSupportColor() {
lg.themedLevels[0] = color.New(color.OpBlink, color.FgLightWhite, color.BgRed).Sprintf("[ERROR]")
lg.themedLevels[1] = color.New(color.FgLightYellow).Sprintf("[WARN]")
lg.themedLevels[2] = color.New(color.FgLightGreen).Sprintf("[INFO]")
lg.themedLevels[3] = color.New(color.FgCyan).Sprintf("[DEBUG]")
} else {
lg.themedLevels[0] = "[ERROR]"
lg.themedLevels[1] = "[WARN]"
lg.themedLevels[2] = "[INFO]"
lg.themedLevels[3] = "[DEBUG]"
}
// Set output level based on globals or overrides
if opts.Level != nil {
lg.globals.Level = *opts.Level
lg.globals.DebugLevel = 1
}
if opts.DebugLevel != nil {
lg.globals.DebugLevel = *opts.DebugLevel
}
// Done
return lg
}
func (lg *consoleAdapter) class() string {
return "console"
}
func (lg *consoleAdapter) destroy() {
// Do nothing
}
func (lg *consoleAdapter) setLevel(level LogLevel, debugLevel uint) {
lg.globals.Level = level
lg.globals.DebugLevel = debugLevel
}
func (lg *consoleAdapter) logError(now time.Time, msg string, raw bool) {
if lg.globals.Level >= LogLevelError {
if !raw {
consolePrint(os.Stderr, now, lg.themedLevels[0], msg)
} else {
consolePrintRAW(os.Stderr, msg)
}
}
}
func (lg *consoleAdapter) logWarning(now time.Time, msg string, raw bool) {
if lg.globals.Level >= LogLevelWarning {
if !raw {
consolePrint(os.Stderr, now, lg.themedLevels[1], msg)
} else {
consolePrintRAW(os.Stderr, msg)
}
}
}
func (lg *consoleAdapter) logInfo(now time.Time, msg string, raw bool) {
if lg.globals.Level >= LogLevelInfo {
if !raw {
consolePrint(os.Stdout, now, lg.themedLevels[2], msg)
} else {
consolePrintRAW(os.Stdout, msg)
}
}
}
func (lg *consoleAdapter) logDebug(level uint, now time.Time, msg string, raw bool) {
if lg.globals.Level >= LogLevelDebug && lg.globals.DebugLevel >= level {
if !raw {
consolePrint(os.Stdout, now, lg.themedLevels[3], msg)
} else {
consolePrintRAW(os.Stdout, msg)
}
}
}
func consolePrint(w io.Writer, now time.Time, themedLevel string, msg string) {
// Lock console access
consoleMtx.Lock()
// Print the message prefixed with the timestamp and level
_, _ = fmt.Fprintf(w, "%v %v %v\n", now.Format("2006-01-02 15:04:05.000"), themedLevel, msg)
// Unlock console access
consoleMtx.Unlock()
}
func consolePrintRAW(w io.Writer, msg string) {
// Lock console access
consoleMtx.Lock()
// Print the message with extra payload
_, _ = fmt.Fprintf(w, "%v\n", msg)
// Unlock console access
consoleMtx.Unlock()
}