forked from sdgdsffdsfff/log
/
record.go
88 lines (74 loc) · 1.83 KB
/
record.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
package log
import (
"runtime"
"strconv"
"time"
)
// Record represents a record object.
type Record interface {
Level() LevelType
AppID() string
Now() time.Time
Name() string
Fileline() string
}
// RecordFactory represents a factory of record.
type RecordFactory func(name string, calldepth int, lv LevelType, msg string) Record
// BaseRecord stands for a single record of log, usually a single line
type BaseRecord struct {
fileLine string
name string
now time.Time
lv LevelType
msg string
appID string
}
// Level return the level of BaseRecord.
func (br *BaseRecord) Level() LevelType {
return br.lv
}
// AppID return the appID of BaseRecord.
func (br *BaseRecord) AppID() string {
return br.appID
}
// Now return the now of time of BaseRecord.
func (br *BaseRecord) Now() time.Time {
return br.now
}
// Name return the name of BaseRecord.
func (br *BaseRecord) Name() string {
return br.name
}
// Fileline return the name of BaseRecord.
func (br *BaseRecord) Fileline() string {
return br.fileLine
}
// String returns the raw message of the Record
func (br *BaseRecord) String() string {
return br.msg
}
// NewBaseRecord creates a BaseRecord with given name, calldepth, LevelType and msg.
func NewBaseRecord(name string, calldepth int, lv LevelType, msg string) *BaseRecord {
fileLine := ""
_, file, line, ok := runtime.Caller(calldepth)
if !ok {
file = "???"
line = 0
}
fileLine = file + ":" + strconv.Itoa(line)
r := &BaseRecord{
fileLine: fileLine,
name: name,
now: time.Now(),
lv: lv,
msg: msg,
appID: globalAppID,
}
return r
}
// NewBaseRecordFactory return a record factory for BaseRecord.
func NewBaseRecordFactory() RecordFactory {
return func(name string, calldepth int, lv LevelType, msg string) Record {
return NewBaseRecord(name, calldepth+1, lv, msg)
}
}