-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
872 lines (720 loc) · 24.7 KB
/
logger.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
// sysadmServer
// @Author Wayne Wang <net_use@bzhy.com>
// @Copyright Bzhy Network
// @HomePage http://www.sysadm.cn
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// @License GNU Lesser General Public License https://www.sysadm.cn/lgpl.html
// @Modified on Sep 19 2021
package sysadmServer
import (
"fmt"
"io"
"os"
"strings"
log "github.com/wangyysde/sysadmLog"
)
type LogFields map[string]interface{}
type LoggerConfig struct {
// DefaultLogger is a instance of logger. We use defaultLogger to log log message
// when AccessLogger or ErrorLogger is nil
DefaultLogger *log.Logger
// AccessLogger is a instance of logrus
// and this instance if for logging access log
AccessLogger *log.Logger
// ErrorLogger is a instance of logrus
// and this instance if for logging error log
ErrorLogger *log.Logger
// Kind specifies the format of the log where be log to
// kind is one of text or json
Kind string
// AccessLogFile records the path of log file for access
// if the access log and error log log into difference files
AccessLogFile string
// ErrorLogFile records the path of log file for error
// if the access log and error log log into difference files
ErrorLogFile string
// Level specifies which level log will be logged
Level string
// SplitAccessAndError identify if log access log and error log
// into difference io.Writer.
// Logs will be log into AccessLogger if SplitAccessAndError is false
// otherwise access logs will be log into AccessLogger and error logs will be log into ErrorLogger.
SplitAccessAndError bool
// Specifies the format of the log timestamp,like: "2021/09/02 - 15:04:05"
TimeStampFormat string
// Flag for whether to log caller info (off by default)
ReportCaller bool
// SkipPaths is a url path array which logs are not written.
// Optional.
SkipPaths []string
}
var Levels = []string{
"trace",
"debug",
"info",
"warning",
"error",
"fatal",
"panic",
}
var TimestampFormat = map[string]string{
"ANSIC": "Mon Jan _2 15:04:05 2006",
"UnixDate": "Mon Jan _2 15:04:05 MST 2006",
"RubyDate": "Mon Jan 02 15:04:05 -0700 2006",
"RFC822": "02 Jan 06 15:04 MST",
"RFC822Z": "02 Jan 06 15:04 -0700", // 使用数字表示时区的RFC822
"RFC850": "Monday, 02-Jan-06 15:04:05 MST",
"RFC1123": "Mon, 02 Jan 2006 15:04:05 MST",
"RFC1123Z": "Mon, 02 Jan 2006 15:04:05 -0700", // 使用数字表示时区的RFC1123
"RFC3339": "2006-01-02T15:04:05Z07:00",
"RFC3339Nano": "2006-01-02T15:04:05.999999999Z07:00",
"Kitchen": "3:04PM",
"Stamp": "Jan _2 15:04:05",
"StampMilli": "Jan _2 15:04:05.000",
"StampMicro": "Jan _2 15:04:05.000000",
"StampNano": "Jan _2 15:04:05.000000000",
"DateTime": "2006-01-02 15:04:05",
}
var LoggerConfigVar = LoggerConfig{
DefaultLogger: nil,
AccessLogger: nil,
ErrorLogger: nil,
Kind: "text",
AccessLogFile: "",
ErrorLogFile: "",
Level: "debug",
SplitAccessAndError: false,
TimeStampFormat: TimestampFormat["RFC3339"],
ReportCaller: true,
SkipPaths: nil,
}
var textFormatter = &log.TextFormatter{
ForceColors: false,
DisableColors: false,
ForceQuote: false,
DisableQuote: true,
EnvironmentOverrideColors: true,
DisableTimestamp: false,
FullTimestamp: true,
TimestampFormat: TimestampFormat["RFC3339"],
DisableSorting: true,
DisableLevelTruncation: true,
PadLevelText: false,
QuoteEmptyFields: true,
}
var jsonFormatter = &log.JSONFormatter{
TimestampFormat: TimestampFormat["RFC3339"],
DisableTimestamp: false,
DisableHTMLEscape: true,
}
func init() {
logger := LoggerConfigVar.DefaultLogger
if logger == nil {
logger = log.New()
}
LoggerConfigVar.DefaultLogger = logger
logger.Out = DefaultWriter
setLoggerLevel()
setLoggerKind()
}
// SetLogLevel set the value of LoggerConfigVar.Level to "debug" if the value of it is ""
// Then SetLogLevel sets the the levels for DefaultLoger, AccessLogger and ErrorLogger
// This function should be called during LoggerConfigVar.Level is setting and a new logger is initating.
func setLoggerLevel() {
if _, err := log.ParseLevel(LoggerConfigVar.Level); err != nil {
LoggerConfigVar.Level = "debug"
}
loggerLevel, _ := log.ParseLevel(LoggerConfigVar.Level)
if LoggerConfigVar.DefaultLogger != nil {
LoggerConfigVar.DefaultLogger.SetLevel(loggerLevel)
}
if LoggerConfigVar.AccessLogger != nil {
LoggerConfigVar.AccessLogger.SetLevel(loggerLevel)
}
if LoggerConfigVar.ErrorLogger != nil {
LoggerConfigVar.ErrorLogger.SetLevel(loggerLevel)
}
}
// SetLogLevel set the vavlue of level to LoggerConfigVar and set the value of level of Loggers
func SetLogLevel(level string) error {
if strings.TrimSpace(level) == "" {
return fmt.Errorf("The value of log level is null")
}
found := false
for _, value := range Levels {
if strings.ToLower(level) == value {
LoggerConfigVar.Level = value
found = true
setLoggerLevel()
}
}
if found {
return nil
}
return fmt.Errorf("The value of log level: %s is invalid",level)
}
func (*Engine) SetLogLevel(level string) error {
return SetLogLevel(level)
}
// setLoggerKind set the value of LoggerConfigVar.Kind to "text" if the value of it is ""
// Then setLoggerKind sets the the formatter for DefaultLoger, AccessLogger and ErrorLogger
// This function should be called during LoggerConfigVar.kind is setting and a new logger is initating.
func setLoggerKind() {
if strings.ToLower(LoggerConfigVar.Kind) != "text" && strings.ToLower(LoggerConfigVar.Kind) != "json" {
LoggerConfigVar.Kind = "text"
}
if logger := LoggerConfigVar.DefaultLogger; logger != nil {
if strings.ToLower(LoggerConfigVar.Kind) == "text" {
formatter := *textFormatter
formatter.DisableColors = false
logger.SetFormatter(&formatter)
} else {
formatter := *jsonFormatter
logger.SetFormatter(&formatter)
}
}
if logger := LoggerConfigVar.AccessLogger; logger != nil {
if strings.ToLower(LoggerConfigVar.Kind) == "text" {
logger.SetFormatter(textFormatter)
} else {
logger.SetFormatter(jsonFormatter)
}
}
if logger := LoggerConfigVar.ErrorLogger; logger != nil {
if strings.ToLower(LoggerConfigVar.Kind) == "text" {
logger.SetFormatter(textFormatter)
} else {
logger.SetFormatter(jsonFormatter)
}
}
}
// SetLogLevel set the vavlue of Kind to LoggerConfigVar and set the value of Kind of Loggers
func SetLoggerKind(kind string) error {
if strings.ToLower(kind) != "text" && strings.ToLower(kind) != "json" {
return fmt.Errorf("The kind(%s) of log is invalid", kind)
}
LoggerConfigVar.Kind = kind
setLoggerKind()
return nil
}
func (*Engine) SetLoggerKind(kind string) error {
return SetLoggerKind(kind)
}
// SetAccessLogFile set file to access log file and then open the file for access logger output.
// This function returns func (fd *os.File) should be called by defer following this function calling to
// close the file descriptor which opened by this function. *os.File: the file descriptor by this function opened.
func SetAccessLogFile(file string) (func (fd *os.File) error, *os.File, error) {
if strings.TrimSpace(file) == "" {
return nil, nil, fmt.Errorf("The length of access log file should be larger 1")
}
file = strings.TrimSpace(file)
fp, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
return nil, nil, fmt.Errorf("Open log file %s error: %s", file, fmt.Sprintf("%s", err))
}
logger := log.New()
logger.Out = fp
if LoggerConfigVar.AccessLogger != nil {
oldfp := LoggerConfigVar.AccessLogger.Out
switch v := oldfp.(type) {
case *os.File:
_ = v.Close()
default:
LoggerConfigVar.AccessLogger.Out = nil
}
}
LoggerConfigVar.AccessLogger = logger
setLoggerLevel()
setLoggerKind()
retFun := func(fd *os.File) error {
err := fd.Close()
LoggerConfigVar.AccessLogger = nil
LoggerConfigVar.AccessLogFile = ""
return err
}
return retFun, fp, nil
}
// SetAccessLoggerWithFp set fp(writer) to access logger
// the function which close the fp should be called by defer following this function calling
func SetAccessLoggerWithFp(fp *os.File) (error) {
if fp == nil {
return fmt.Errorf("can not set nil(writer) to logger")
}
if LoggerConfigVar.AccessLogger == nil {
logger := log.New()
logger.Out = fp
LoggerConfigVar.AccessLogger = logger
setLoggerLevel()
setLoggerKind()
}
return nil
}
// (*Engine)SetAccessLogFile is a method to SetAccessLogFile function
func (*Engine)SetAccessLogFile(file string) (func (fd *os.File) error, *os.File, error) {
return SetAccessLogFile(file)
}
// SetErrorLogFile set file to error log file and then open the file for error logger output.
// This function returns func (fd *os.File) should be called by defer following this function calling to
// close the file descriptor which opened by this function. *os.File: the file descriptor by this function opened.
func SetErrorLogFile(file string) (func (fd *os.File) error, *os.File, error) {
if strings.TrimSpace(file) == "" {
return nil, nil, fmt.Errorf("The length of error log file should be larger 1")
}
file = strings.TrimSpace(file)
fp, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
return nil, nil, fmt.Errorf("Open log file %s error: %s", file, fmt.Sprintf("%s", err))
}
logger := log.New()
logger.Out = fp
if LoggerConfigVar.ErrorLogger != nil {
oldfp := LoggerConfigVar.ErrorLogger.Out
switch v := oldfp.(type) {
case *os.File:
_ = v.Close()
default:
LoggerConfigVar.ErrorLogger.Out = nil
}
}
LoggerConfigVar.ErrorLogger = logger
setLoggerLevel()
setLoggerKind()
retFun := func(fd *os.File) error {
err := fd.Close()
LoggerConfigVar.ErrorLogger = nil
LoggerConfigVar.ErrorLogFile = ""
return err
}
return retFun, fp, nil
}
// SetErrorLoggerWithFp set fp(writer) to error logger
// the function which close the fp should be called by defer following this function calling
func SetErrorLoggerWithFp(fp *os.File) (error) {
if fp == nil {
return fmt.Errorf("can not set nil(writer) to logger")
}
if LoggerConfigVar.ErrorLogger == nil {
logger := log.New()
logger.Out = fp
LoggerConfigVar.ErrorLogger = logger
setLoggerLevel()
setLoggerKind()
}
return nil
}
// (*Engine)SetErrorLogFile is a method to SetErrorLogFile function
func (*Engine)SetErrorLogFile(file string) (func (fd *os.File) error, *os.File, error) {
return SetErrorLogFile(file)
}
// SetIsSplitLog set IsSplitLog to Logger configuration
func SetIsSplitLog(isSplit bool){
if isSplit {
if LoggerConfigVar.AccessLogger == nil || LoggerConfigVar.ErrorLogger == nil {
Log("You try to set SplitAccessAndError to true, but access log or error logger have not opened.", "warning")
}
} else {
if LoggerConfigVar.AccessLogger == nil {
Log("You try to set SplitAccessAndError to false, but access logger have not opened. All log message will be log to defaultOutput and error logger", "warning")
}
}
LoggerConfigVar.SplitAccessAndError = isSplit
}
func (*Engine)SetIsSplitLog(isSplit bool){
LoggerConfigVar.SplitAccessAndError = isSplit
}
// SetTimestampFormat set timeStampFormat to the LoggerConfig and then set it to all the Loggers
// The value of format is one of constants of time package and "DateTime"
func SetTimestampFormat(format string) error {
if strings.TrimSpace(format) == "" {
return fmt.Errorf("The length of format should be larger 1")
}
for k, v := range TimestampFormat {
if strings.ToLower(k) == strings.ToLower(format) {
LoggerConfigVar.TimeStampFormat = v
textFormatter.TimestampFormat = v
jsonFormatter.TimestampFormat = v
fmt.Printf("v:%s\n",v)
setLoggerKind()
return nil
}
}
return fmt.Errorf("The TimeStampFormat(%s) is invalid.",format)
}
// (*Engine)SetTimestampFormat is the method to SetTimestampFormat function
func (*Engine)SetTimestampFormat(format string) error {
return SetTimestampFormat(format)
}
// if isDisable is false, then timestamp message will be added to log messages automatically.
// Otherwise no timestamp will be added.
func DisableTimestamp(isDisable bool) {
textFormatter.DisableTimestamp = isDisable
jsonFormatter.DisableTimestamp = isDisable
setLoggerKind()
}
// (*Engine)DisableTimestamp is the method to DisableTimestamp function
func (*Engine)DisableTimestamp(isDisable bool){
DisableTimestamp(isDisable)
}
// SetReportCaller sets ReportCaller of LoggerConfig to true or false.
// if the value of ReportCaller is true, then callers name and the file path information which the caller in will be
// added to log messages.
func SetReportCaller(isReportCaller bool){
LoggerConfigVar.ReportCaller = isReportCaller
if LoggerConfigVar.DefaultLogger != nil {
LoggerConfigVar.DefaultLogger.ReportCaller = isReportCaller
}
if LoggerConfigVar.AccessLogger != nil {
LoggerConfigVar.AccessLogger.ReportCaller = isReportCaller
}
if LoggerConfigVar.ErrorLogger != nil {
LoggerConfigVar.ErrorLogger.ReportCaller = isReportCaller
}
}
// (*Engine)SetReportCaller is the method to SetReportCaller function
func (*Engine) SetReportCaller(isReportCaller bool){
SetReportCaller(isReportCaller)
}
// WriteLogWithFields write fields to the logger. logLevel will be set to "info" when its value is ""
// the value of logLevel will be set to "error" if fields["ErrorMessage"] is not ""
// fields["ErrorMessage"] will write to logger when fields["ErrorMessage"] and fields["Message"] are not ""
// the value of fields["Message"] will be igored.
func WriteLogWithFields(logger *log.Logger, fields LogFields, logLevel string){
if logger == nil {
return
}
if strings.TrimSpace(logLevel) == "" {
logLevel = "info"
}
found := false
for _,value := range Levels {
if strings.ToLower(strings.TrimSpace(logLevel)) == value {
found = true
}
}
errormsg := ""
if v,ok := fields["ErrorMessage"]; ok {
errormsg = v.(string)
}
if strings.TrimSpace(errormsg) != "" && !found {
logLevel = "error"
} else if !found {
logLevel = "info"
}
switch strings.ToLower(logLevel){
case "trace":
logger.WithFields(log.Fields(fields)).Trace("")
case "debug":
logger.WithFields(log.Fields(fields)).Debug("")
case "info":
logger.WithFields(log.Fields(fields)).Info("")
case "warning":
logger.WithFields(log.Fields(fields)).Warn("")
case "error":
logger.WithFields(log.Fields(fields)).Error("")
case "fatal":
fd := logger.Out
switch v := fd.(type) {
case *os.File:
_ = v.Close()
}
logger.WithFields(log.Fields(fields)).Fatal("")
case "panic":
logger.WithFields(log.Fields(fields)).Panic("")
panic("")
}
}
// (*Engine)WriteLogWithFields is the method to WriteLogWithFields function
func (*Engine)WriteLogWithFields(logger *log.Logger, fields LogFields, logLevel string){
WriteLogWithFields(logger, fields,logLevel)
}
// LoggerWithConfig instance a Logger middleware with config.
func Logger() HandlerFunc {
notlogged := LoggerConfigVar.SkipPaths
var skip map[string]struct{}
if length := len(notlogged); length > 0 {
skip = make(map[string]struct{}, length)
for _, path := range notlogged {
skip[path] = struct{}{}
}
}
return func(c *Context) {
fields := make(LogFields)
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
// Process request
c.Next()
// Log only when path is not being skipped
if _, ok := skip[path]; !ok {
fields["Request"] = c.Request
fields["Keys"] = c.Keys
fields["ClientIP"] = c.ClientIP()
fields["Method"] = c.Request.Method
fields["StatusCode"] = c.Writer.Status()
fields["ErrorMessage"] = c.Errors.ByType(ErrorTypePrivate).String()
fields["BodySize"] = c.Writer.Size()
if raw != "" {
path = path + "?" + raw
}
fields["Path"] = path
LogWithFields(fields,"info")
}
}
}
// LoggerWithConfig instance a Logger middleware with config.
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
var skip map[string]struct{}
if length := len(notlogged); length > 0 {
skip = make(map[string]struct{}, length)
for _, path := range notlogged {
skip[path] = struct{}{}
}
}
return func(c *Context) {
fields := make(LogFields)
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
// Process request
c.Next()
// Log only when path is not being skipped
if _, ok := skip[path]; !ok {
fields["Request"] = c.Request
fields["Keys"] = c.Keys
fields["ClientIP"] = c.ClientIP()
fields["Method"] = c.Request.Method
fields["StatusCode"] = c.Writer.Status()
fields["ErrorMessage"] = c.Errors.ByType(ErrorTypePrivate).String()
fields["BodySize"] = c.Writer.Size()
if raw != "" {
path = path + "?" + raw
}
fields["Path"] = path
logger := log.New()
logger.Out = out
loggerLevel, _ := log.ParseLevel("debug")
logger.SetLevel(loggerLevel)
formatter := *textFormatter
formatter.DisableColors = false
logger.SetFormatter(&formatter)
WriteLogWithFields(logger, fields, "info")
}
}
}
// WriteLog write message to the logger. logLevel will be set to "error" when its value is ""
func WriteLog(logger *log.Logger, message string, logLevel string){
if logger == nil {
return
}
if strings.TrimSpace(logLevel) == "" {
logLevel = "info"
}
found := false
for _,value := range Levels {
if strings.ToLower(strings.TrimSpace(logLevel)) == value {
found = true
}
}
if !found {
logLevel = "error"
}
switch strings.ToLower(logLevel){
case "trace":
logger.Trace(message)
case "debug":
logger.Debug(message)
case "info":
logger.Info(message)
case "warning":
logger.Warn(message)
case "error":
logger.Error(message)
case "fatal":
logger.Fatal(message)
case "panic":
logger.Panic(message)
panic("")
}
}
// (*Engine)WriteLog is the method to WriteLog function
func (*Engine)WriteLog(logger *log.Logger, message string, logLevel string){
WriteLog(logger,message,logLevel)
}
// LogWithFields log LogFields to DefaultLogger, AccessLogger and ErrorLogger.
// if SplitAccessAndError is true, then log info log message to AccessLogger and other log message to ErrorLogger.
// otherwise all log message will be log to AccessLogger. If SplitAccessAndError is true but ErrorLogger is nil
// then all log message will be log to AccessLogger and a additional warning log message will be log to AccessLogger.
// If SplitAccessAndError is false but AccessLogger is nil then all log message will be log to ErrorLogger and a additional
// warning log message will be log to ErrorLogger.
func LogWithFields(fields LogFields, logLevel string){
if strings.TrimSpace(logLevel) == "" {
if _,ok := fields["ErrorMessage"]; ok {
logLevel = "error"
} else {
logLevel = "info"
}
}
found := false
for _,value := range Levels {
if strings.ToLower(logLevel) == strings.ToLower(value) {
found = true
}
}
if !found {
if _,ok := fields["ErrorMessage"]; ok {
logLevel = "error"
} else {
logLevel = "info"
}
}
logger := LoggerConfigVar.DefaultLogger
if logger != nil {
WriteLogWithFields(logger,fields,logLevel)
}
accessLogger := LoggerConfigVar.AccessLogger
errorLogger := LoggerConfigVar.ErrorLogger
if strings.ToLower(logLevel) == "info" {
if accessLogger != nil {
WriteLogWithFields(accessLogger,fields,logLevel)
}
} else {
if LoggerConfigVar.SplitAccessAndError {
if errorLogger != nil {
WriteLogWithFields(errorLogger,fields,logLevel)
} else if accessLogger != nil {
WriteLogWithFields(accessLogger,fields,logLevel)
f := make(LogFields)
f["ErrorMessage"] = "SplitAccessAndError has be set to true, but error log file has not be set."
logLevel = "warning"
WriteLogWithFields(accessLogger,f,logLevel)
}
} else {
if accessLogger != nil {
WriteLogWithFields(accessLogger,fields,logLevel)
} else if errorLogger != nil {
WriteLogWithFields(errorLogger,fields,logLevel)
f := make(LogFields)
f["ErrorMessage"] = "SplitAccessAndError has be set to false, but access log file has not be set."
logLevel = "warning"
WriteLogWithFields(errorLogger,f,logLevel)
}
}
}
}
// (*Engine)LogWithFields is the method to LogWithFields function
func (*Engine)LogWithFields(fields LogFields, logLevel string){
LogWithFields(fields,logLevel)
}
// Log log message to DefaultLogger, AccessLogger and ErrorLogger.
// if SplitAccessAndError is true, then log info log message to AccessLogger and other log message to ErrorLogger.
// otherwise all log message will be log to AccessLogger. If SplitAccessAndError is true but ErrorLogger is nil
// then all log message will be log to AccessLogger and a additional warning log message will be log to AccessLogger.
// If SplitAccessAndError is false but AccessLogger is nil then all log message will be log to ErrorLogger and a additional
// warning log message will be log to ErrorLogger.
func Log(message string, logLevel string){
if strings.TrimSpace(logLevel) == "" {
logLevel = "info"
}
found := false
for _,value := range Levels {
if strings.ToLower(logLevel) == strings.ToLower(value) {
found = true
}
}
if !found {
logLevel = "info"
}
logger := LoggerConfigVar.DefaultLogger
if logger != nil {
WriteLog(logger,message,logLevel)
}
accessLogger := LoggerConfigVar.AccessLogger
errorLogger := LoggerConfigVar.ErrorLogger
if strings.ToLower(logLevel) == "info" {
if accessLogger != nil {
WriteLog(accessLogger,message,logLevel)
}
} else {
if LoggerConfigVar.SplitAccessAndError {
if errorLogger != nil {
WriteLog(errorLogger,message,logLevel)
} else if accessLogger != nil {
WriteLog(accessLogger,message,logLevel)
logLevel = "warning"
WriteLog(accessLogger,"SplitAccessAndError has be set to true, but error log file has not be set.",logLevel)
}
} else {
if accessLogger != nil {
WriteLog(accessLogger,message,logLevel)
} else if errorLogger != nil {
WriteLog(errorLogger,message,logLevel)
logLevel = "warning"
WriteLog(errorLogger,"SplitAccessAndError has be set to false, but access log file has not be set.",logLevel)
}
}
}
}
// (*Engine)Log is the method to Log function
func (*Engine)Log(message string, logLevel string){
Log(message, logLevel )
}
// Logf log message to DefaultLogger, AccessLogger and ErrorLogger.
// if SplitAccessAndError is true, then log info log message to AccessLogger and other log message to ErrorLogger.
// otherwise all log message will be log to AccessLogger. If SplitAccessAndError is true but ErrorLogger is nil
// then all log message will be log to AccessLogger and a additional warning log message will be log to AccessLogger.
// If SplitAccessAndError is false but AccessLogger is nil then all log message will be log to ErrorLogger and a additional
// warning log message will be log to ErrorLogger.
func Logf(logLevel string , format string , a ...interface{}){
if strings.TrimSpace(logLevel) == "" {
logLevel = "info"
}
found := false
for _,value := range Levels {
if strings.ToLower(logLevel) == strings.ToLower(value) {
found = true
}
}
if !found {
logLevel = "info"
}
logMsg := fmt.Sprintf(format, a...)
logger := LoggerConfigVar.DefaultLogger
if logger != nil {
WriteLog(logger,logMsg,logLevel)
}
accessLogger := LoggerConfigVar.AccessLogger
errorLogger := LoggerConfigVar.ErrorLogger
if strings.ToLower(logLevel) == "info" {
if accessLogger != nil {
WriteLog(accessLogger,logMsg,logLevel)
}
} else {
if LoggerConfigVar.SplitAccessAndError {
if errorLogger != nil {
WriteLog(errorLogger,logMsg,logLevel)
} else if accessLogger != nil {
WriteLog(accessLogger,logMsg,logLevel)
logLevel = "warning"
WriteLog(accessLogger,"SplitAccessAndError has be set to true, but error log file has not be set.",logLevel)
}
} else {
if accessLogger != nil {
WriteLog(accessLogger,logMsg,logLevel)
} else if errorLogger != nil {
WriteLog(errorLogger,logMsg,logLevel)
logLevel = "warning"
WriteLog(errorLogger,"SplitAccessAndError has be set to false, but access log file has not be set.",logLevel)
}
}
}
}
// (*Engine)Logf is the method to Log function
func (*Engine)Logf(logLevel string , format string , a ...interface{}){
Logf(logLevel,format, a... )
}