-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_user_level.go
285 lines (264 loc) · 8.2 KB
/
logger_user_level.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package gologster
import (
"errors"
"log"
"strings"
"text/template"
"time"
)
// Logger : основной объект. Является пользовательским интерфейсом.
// Хранит все объекты реализующие интерфейс 'iLogger'.
// Создаётся один раз, на все приложение.
//
// main object. Is the user interface. Stores all objects
// that implement the 'iLogger' interface.
// Created once, for the entire application.
//
type Logger struct {
base *loggerBase
baseFile *loggerBaseFile
baseConsole *loggerBaseConsole
modeConsole *loggerConsoleSimple
modeFileMulti *loggerFileMultithreading
modeFileMutex *loggerFileMutex
pckgs map[string][]Option
}
type DefaultInstaller func(logger *Logger) error
type DefaultConfigurator func(templateString string, params ...map[string]string) DefaultInstaller
type PackageInstaller func(logger *Logger, pckg string) error
type PackageConfigurator func(templateString string, isConcurrency concurrency, params ...string) PackageInstaller
type concurrency bool
const SingleThreading concurrency = false
const MultiThreading concurrency = true
func DefaultConsoleSimple(templateString string, params ...map[string]string) DefaultInstaller {
return func(logger *Logger) error {
packages := make(map[string]struct{})
tmpl, err := template.New("console_simple").Parse(templateString)
if err != nil {
tmpl, _ = template.New("console_simple").Parse(BaseLogTemplate)
}
logger.baseConsole = newBaseConsole(logger.base, packages, tmpl)
logger.modeConsole = newLoggerConsoleSimple(logger.baseConsole)
return nil
}
}
func DefaultFileMutex(templateString string, params ...map[string]string) DefaultInstaller {
return func(logger *Logger) error {
if logger.baseFile == nil {
if len(params) == 0 && len(params[0]) == 0 {
return errors.New("DefaultFileMutex : File map isn't exist. ")
}
}
tmpl, err := template.New("file_mutex").Parse(templateString)
if err != nil {
tmpl, _ = template.New("file_mutex").Parse(BaseLogTemplate)
}
if logger.baseFile == nil {
logger.baseFile = newBaseFile(logger.base, params[0], tmpl)
}
logger.modeFileMutex = newLoggerFileMutex(logger.baseFile)
return nil
}
}
func DefaultFileMulti(templateString string, params ...map[string]string) DefaultInstaller {
return func(logger *Logger) error {
if logger.baseFile == nil {
if len(params) == 0 && len(params[0]) == 0 {
return errors.New("DefaultFileMulti : File map isn't exist. ")
}
}
tmpl, err := template.New("file_mutex").Parse(templateString)
if err != nil {
tmpl, _ = template.New("file_mutex").Parse(BaseLogTemplate)
}
if logger.baseFile == nil {
logger.baseFile = newBaseFile(logger.base, params[0], tmpl)
}
logger.modeFileMulti = newLoggerFileMultithreading(logger.baseFile)
return nil
}
}
func PackageConsoleSimple(templateString string, isConcurrency concurrency, params ...string) PackageInstaller {
return func(logger *Logger, pckg string) error {
tmpl, err := template.New("console_simple").Parse(templateString)
if err != nil {
tmpl, _ = template.New("console_simple").Parse(BaseLogTemplate)
}
//
if logger.modeConsole == nil {
packages := make(map[string]struct{})
packages[pckg] = struct{}{}
logger.baseConsole = newBaseConsole(logger.base, packages, tmpl)
logger.modeConsole = newLoggerConsoleSimple(logger.baseConsole)
} else {
logger.baseConsole.packages[pckg] = struct{}{}
}
//
if isConcurrency {
logger.pckgs[pckg] = append(logger.pckgs[pckg], GoOptionConsole)
} else {
logger.pckgs[pckg] = append(logger.pckgs[pckg], OptionConsole)
}
//
return nil
}
}
func PackageFileMutex(templateString string, isConcurrency concurrency, params ...string) PackageInstaller {
return func(logger *Logger, pckg string) error {
//
tmpl, err := template.New("console_simple").Parse(templateString)
if err != nil {
tmpl, _ = template.New("console_simple").Parse(BaseLogTemplate)
}
//
if logger.modeFileMutex == nil {
packages := make(map[string]string, 0)
for _, file := range params {
packages[pckg] = file
}
logger.baseFile = newBaseFile(logger.base, packages, tmpl)
logger.modeFileMutex = newLoggerFileMutex(logger.baseFile)
} else {
for _, file := range params {
logger.modeFileMutex.newFile(pckg, file)
}
}
//
if isConcurrency {
logger.pckgs[pckg] = append(logger.pckgs[pckg], GoOptionFileMutex)
} else {
logger.pckgs[pckg] = append(logger.pckgs[pckg], OptionFileMutex)
}
//
return nil
}
}
func PackageFileMulti(templateString string, isConcurrency concurrency, params ...string) PackageInstaller {
return func(logger *Logger, pckg string) error {
//
tmpl, err := template.New("console_simple").Parse(templateString)
if err != nil {
tmpl, _ = template.New("console_simple").Parse(BaseLogTemplate)
}
//
if logger.modeFileMulti == nil {
packages := make(map[string]string, 0)
for _, file := range params {
packages[pckg] = file
}
logger.baseFile = newBaseFile(logger.base, packages, tmpl)
logger.modeFileMulti = newLoggerFileMultithreading(logger.baseFile)
} else {
for _, file := range params {
logger.modeFileMulti.newFile(pckg, file)
}
}
//
if isConcurrency {
logger.pckgs[pckg] = append(logger.pckgs[pckg], GoOptionFileMutex)
} else {
logger.pckgs[pckg] = append(logger.pckgs[pckg], OptionFileMutex)
}
//
return nil
}
}
// Default : создаёт базовый пользовательский интерфейс, с выводом в консоль.
// filledTemplate a base user interface, with output to the console.
//
func Default(installers ...DefaultInstaller) *Logger {
logger := new(Logger)
logger.base = newBase()
for _, mode := range installers {
err := mode(logger)
if err != nil {
log.Println(err)
}
}
return logger
}
func Packages(packages map[string][]PackageInstaller) *Logger {
logger := new(Logger)
logger.base = newBase()
logger.pckgs = make(map[string][]Option, 0)
for name, installers := range packages {
for _ , mode := range installers {
err := mode(logger, name)
if err != nil {
log.Println(err)
}
}
}
return logger
}
// Info : логирование уровня 'info'.
// logging level 'info'.
//
func (logger *Logger) Info(value interface{}, modes ...Mode) {
date := time.Now().Format("Mon Jan _2 15:04:05 2006")
data := newLogData(value, levelInfo, date).setRuntimeInfo(3)
_ = data.marshal(logger.base)
if len(modes) != 0 {
data.IsOption = true
logger.callingMode(data, modes...)
} else {
for pckg, options := range logger.pckgs {
if strings.Contains(data.Package, pckg) {
data.Package = pckg
logger.callingOption(data, options...)
break
}
}
}
}
// Error : логирование уровня 'error'.
// logging level 'error'.
//
func (logger *Logger) Error(value interface{}, modes ...Mode) {
date := time.Now().Format("Mon Jan _2 15:04:05 2006")
data := newLogData(value, levelError, date).setRuntimeInfo(4)
_ = data.marshal(logger.base)
if len(modes) != 0 {
data.IsOption = true
logger.callingMode(data, modes...)
} else {
for pckg, options := range logger.pckgs {
if strings.Contains(data.Package, pckg) {
data.Package = pckg
logger.callingOption(data, options...)
break
}
}
}
}
// Panic : логирование уровня 'panic'.
// logging level 'panic'.
//
func (logger *Logger) Panic(value interface{}, modes ...Mode) {
date := time.Now().Format("Mon Jan _2 15:04:05 2006")
data := newLogData(value, levelPanic, date).setRuntimeInfo(4)
_ = data.marshal(logger.base)
if len(modes) != 0 {
data.IsOption = true
logger.callingMode(data, modes...)
} else {
for pckg, options := range logger.pckgs {
if strings.Contains(data.Package, pckg) {
data.Package = pckg
logger.callingOption(data, options...)
break
}
}
}
}
func (logger *Logger) callingOption(log *logData, options ...Option) {
for _, option := range options {
mode := option()
mode(logger, log)
}
}
func (logger *Logger) callingMode(log *logData, modes ...Mode) {
for _, mode := range modes {
mode(logger, log)
}
}