/
msg.go
198 lines (177 loc) · 5.12 KB
/
msg.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
194
195
196
197
198
package ng
import (
"bytes"
"fmt"
"os"
"regexp"
"sync"
"time"
"github.com/colt3k/nglog/internal/pkg/enum"
)
var (
clrStart = regexp.MustCompile(`\[..m`)
clrEnd = regexp.MustCompile(`\[0m`)
clrTab = regexp.MustCompile(`\t`)
)
// Fields type, used to pass to `WithFields`.
type Fields map[string]interface{}
var bufferPool *sync.Pool
func init() {
bufferPool = &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
}
//Create a pool for reuse in order to speed up process of logging
type LogMsg struct {
Logger *StdLogger
//None, Fatal, Error, Warn, Info, Debug
Level enum.LogLevel
//Message passed into logger
Message string
//PlainMessage no colors no returns at end of line, etc..
PlainMessage string
//Fields passed to template i.e. for JSON output
Fields []Fields
// Time entry was created
Time time.Time
// When formatter is called in entry.log(), an Buffer may be set to entry
Buffer *bytes.Buffer
caller string
}
func NewEntry(logger *StdLogger) *LogMsg {
t := &LogMsg{
Logger: logger,
// Default is three fields, give a little extra room
Fields: make([]Fields, 5),
}
return t
}
// Add a map of fields to the Entry.
func (entry *LogMsg) WithFields(fields []Fields) *LogMsg {
t := make([]Fields, 0)
//data := make(Fields, len(entry.Fields)+len(fields))
for _, d := range entry.Fields {
t = append(t, d)
}
for _, d := range fields {
t = append(t, d)
}
return &LogMsg{Logger: entry.Logger, Fields: t}
}
func (entry *LogMsg) Info(args ...interface{}) {
if entry.Logger.level >= enum.INFO {
entry.log(enum.INFO, fmt.Sprint(args...))
}
}
func (entry *LogMsg) Error(args ...interface{}) {
if entry.Logger.level >= enum.ERROR {
entry.log(enum.ERROR, fmt.Sprint(args...))
}
}
func (entry *LogMsg) Fatal(args ...interface{}) {
if entry.Logger.level >= enum.FATAL {
entry.log(enum.FATAL, fmt.Sprint(args...))
}
Exit(1)
}
func (entry *LogMsg) LogEnt(level enum.LogLevel, format, caller string, rtn bool, args ...interface{}) {
entry.LogEntWithPlainText(level, format, caller, nil, rtn, args...)
}
func (entry *LogMsg) LogEntWithPlainText(level enum.LogLevel, format, caller string, plaintext *string, rtn bool, args ...interface{}) {
entry.caller = caller
//has format but doesn't perform a return
// https://golang.org/pkg/fmt/
if len(format) > 0 && !rtn {
tmp := fmt.Sprint(fmt.Sprintf(format, args...))
if plaintext == nil || len(*plaintext) == 0 {
// Create a plaintext by removing colors
c1 := clrTab.ReplaceAllString(tmp, "")
s1 := clrStart.ReplaceAllString(c1, "")
entry.PlainMessage = clrEnd.ReplaceAllString(s1, "")
} else {
entry.PlainMessage = *plaintext
}
entry.log(level, tmp)
} else if len(format) == 0 && !rtn { // no format and no return
tmp := fmt.Sprint(args...)
if plaintext == nil || len(*plaintext) == 0 {
// Create a plaintext by removing colors
c1 := clrTab.ReplaceAllString(tmp, "")
s1 := clrStart.ReplaceAllString(c1, "")
entry.PlainMessage = clrEnd.ReplaceAllString(s1, "")
} else {
entry.PlainMessage = *plaintext
}
entry.log(level, tmp)
} else if len(format) == 0 && rtn { // no format and a return
tmp := fmt.Sprint(entry.sprintlnn(args...))
if plaintext == nil || len(*plaintext) == 0 {
// Create a plaintext by removing colors
c1 := clrTab.ReplaceAllString(tmp, "")
s1 := clrStart.ReplaceAllString(c1, "")
entry.PlainMessage = clrEnd.ReplaceAllString(s1, "")
} else {
entry.PlainMessage = *plaintext
}
entry.log(level, tmp)
}
if level == enum.FATAL {
Exit(1)
}
}
// Sprintlnn => Sprint no newline. This is to get the behavior of how
// fmt.Sprintln where spaces are always added between operands, regardless of
// their type. Instead of vendoring the Sprintln implementation to spare a
// string allocation, we do the simplest thing.
func (entry *LogMsg) sprintlnn(args ...interface{}) string {
msg := fmt.Sprintln(args...)
return msg[:len(msg)-1]
}
func (entry LogMsg) log(level enum.LogLevel, msg string) {
var buffer *bytes.Buffer
entry.Time = time.Now()
entry.Level = level
entry.Message = msg
//entry.fireHooks()
buffer = bufferPool.Get().(*bytes.Buffer)
buffer.Reset()
defer bufferPool.Put(buffer)
entry.Buffer = buffer
//@TODO Send to Appenders instead
entry.write()
entry.Buffer = nil
// To avoid Entry#log() returning a value that only would make sense for
// panic() to use in Entry#Panic(), we avoid the allocation by checking
// directly here.
//if level <= PanicLevel {
// panic(&entry)
//}
}
func (entry *LogMsg) write() {
entry.Logger.MU.Lock()
defer entry.Logger.MU.Unlock()
//Process to appenders HERE!!!
//o, _ := os.Stdout.Stat()
//if (o.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
// // Terminal
// fmt.Println("On Terminal -------------------")
//} else {
// // Not Terminal
// fmt.Println("NOT On Terminal -------------------")
//}
for _, d := range entry.Logger.appenders {
serialized, err := entry.Logger.Formatter.Format(entry, d.DisableColor())
if err != nil {
fmt.Fprintf(os.Stderr, "failed to obtain reader, \n%+v", err)
}
if d.Applicable(entry.caller) {
d.Process(serialized)
}
}
}
func Exit(code int) {
//runHandlers()
os.Exit(code)
}