-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
161 lines (127 loc) · 2.95 KB
/
logging.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
package pg
import (
"fmt"
"os"
"strings"
"github.com/sirupsen/logrus"
)
const layoutDate = "2006-01-02T15:04:05-07:00"
type logger struct {
log bool
*logrus.Logger
}
// Log global variable to use as a logging interface
var Log Logging
// Logging define all method to be implemented as logging
// only support print, warn and error
type Logging interface {
// Print write debug message without format into stdout
Print(args ...interface{})
// Printf write debug message with format into stdout
Printf(format string, args ...interface{})
// Println write debug message with new line into stdout
Println(args ...interface{})
// Warn write warn message without format into stdout
Warn(args ...interface{})
// Warnf write warn message with format into stdout
Warnf(format string, args ...interface{})
// Error write error message without format into stdout
Error(args ...interface{})
// Errorf print error message with format
Errorf(format string, args ...interface{})
}
func (l *logger) Print(args ...interface{}) {
if !l.log {
return
}
l.Logger.Print(args...)
}
func (l *logger) Printf(format string, args ...interface{}) {
if !l.log {
return
}
l.Logger.Printf(format, args...)
}
func (l *logger) Println(args ...interface{}) {
if !l.log {
return
}
l.Logger.Println(args...)
}
func (l *logger) Warn(args ...interface{}) {
if !l.log {
return
}
l.Logger.Warn(args...)
}
func (l *logger) Warnf(format string, args ...interface{}) {
if !l.log {
return
}
l.Logger.Warnf(format, args...)
}
func (l *logger) Error(args ...interface{}) {
if !l.log {
return
}
l.Logger.Error(args...)
}
func (l *logger) Errorf(format string, args ...interface{}) {
if !l.log {
return
}
l.Logger.Errorf(format, args...)
}
type logFormatter struct {
logrus.TextFormatter
}
// setLogger create a new logrus.Logger
func setLogger() *logrus.Logger {
l := &logrus.Logger{
Out: os.Stderr,
ReportCaller: true,
Level: logrus.DebugLevel,
Formatter: &logFormatter{
logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: layoutDate,
DisableLevelTruncation: false,
},
},
}
// set logrus into Log variable
Log = &logger{true, l}
return l
}
// NewLogger create instance of Logging
func NewLogger() *logrus.Logger {
return setLogger()
}
// DisableLogging set configuration for disable logging
func DisableLogging() {
Log = &logger{log: false}
}
// Format create formatted logging
func (l *logFormatter) Format(e *logrus.Entry) ([]byte, error) {
var (
levelColor int
)
switch e.Level {
case logrus.WarnLevel:
levelColor = 33 // yellow
case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
levelColor = 31 // red
default:
levelColor = 34 // blue
}
return []byte(
fmt.Sprintf(
"time=%s level=\u001B[%dm%s\u001B[0m %s\n",
e.Time.Format(l.TimestampFormat),
levelColor,
strings.ToUpper(e.Level.String()),
e.Message,
),
), nil
}