This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
77 lines (65 loc) · 1.74 KB
/
entity.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
package logger
import (
"fmt"
"os"
"reflect"
)
type entity struct {
log Sender
ctx Fields
}
func newEntity(log Sender) *entity {
return &entity{
log: log,
ctx: Fields{},
}
}
func (e *entity) Reset() {
e.ctx = Fields{}
}
func (e *entity) WithFields(fields Fields) LogWriter {
for key, value := range fields {
ref := reflect.TypeOf(value)
if ref != nil {
switch ref.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Ptr, reflect.Struct:
e.ctx[key] = fmt.Sprintf("unsupported field value: %#v", value)
continue
}
}
e.ctx[key] = value
}
return e
}
func (e *entity) prepareMessage(format string, args ...interface{}) func(v *message) {
return func(v *message) {
v.Message = fmt.Sprintf(format, args...)
for key, value := range e.ctx {
v.Ctx[key] = value
}
e.log.PutEntity(e)
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Infof info message
func (e *entity) Infof(format string, args ...interface{}) {
e.log.SendMessage(LevelInfo, e.prepareMessage(format, args...))
}
// Warnf warning message
func (e *entity) Warnf(format string, args ...interface{}) {
e.log.SendMessage(LevelWarn, e.prepareMessage(format, args...))
}
// Errorf error message
func (e *entity) Errorf(format string, args ...interface{}) {
e.log.SendMessage(LevelError, e.prepareMessage(format, args...))
}
// Debugf debug message
func (e *entity) Debugf(format string, args ...interface{}) {
e.log.SendMessage(LevelDebug, e.prepareMessage(format, args...))
}
// Fatalf fatal message and exit
func (e *entity) Fatalf(format string, args ...interface{}) {
e.log.SendMessage(levelFatal, e.prepareMessage(format, args...))
e.log.Close()
os.Exit(1)
}