-
Notifications
You must be signed in to change notification settings - Fork 1
/
location_logger.go
50 lines (42 loc) · 1.33 KB
/
location_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
package golog
import (
"time"
)
// A LocationLogger wraps useful methods for outputting strings by creating
// a LogMessage with the relevant metadata.
type LocationLogger interface {
Logger
LogDepth(level int, closure func() string, depth int)
}
type locationLoggerImpl struct {
Logger
getMetadata MetadataFunc
}
// Returns a new LocationLogger wrapping the associated logger, and using
// the provided function to generate the metadata. For example:
// log := NewLocationLogger(NewDefaultLogger(), NoLocation)
func NewLocationLogger(l Logger, metadataFunc MetadataFunc) LocationLogger {
return &locationLoggerImpl{l, metadataFunc}
}
// Returns a LocationLogger wrapping the DefaultLogger.
func NewDefaultLocationLogger() LocationLogger {
return NewLocationLogger(NewDefaultLogger(),
MakeMetadataFunc(DefaultMetadata))
}
func (l *locationLoggerImpl) makeLogClosure(level int, msg func() string, skip int) func() *LogMessage {
// Evaluate this early.
ns := time.Now()
// TODO(awreece) Add ns to metadata?
metadata := l.getMetadata(skip + 1)
return func() *LogMessage {
return &LogMessage{
Level: level,
Message: msg(),
Nanoseconds: ns,
Metadata: metadata,
}
}
}
func (l *locationLoggerImpl) LogDepth(level int, closure func() string, depth int) {
l.Log(level, l.makeLogClosure(level, closure, depth+1))
}