-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogTracer.go
128 lines (114 loc) · 4.29 KB
/
LogTracer.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
package trace
import (
"context"
cconf "github.com/pip-services3-gox/pip-services3-commons-gox/config"
cconv "github.com/pip-services3-gox/pip-services3-commons-gox/convert"
cref "github.com/pip-services3-gox/pip-services3-commons-gox/refer"
log "github.com/pip-services3-gox/pip-services3-components-gox/log"
)
// LogTracer tracer that dumps recorded traces to logger.
// Configuration parameters:
// - options:
// - log_level: log level to record traces (default: debug)
//
// References:
// - *:logger:*:*:1.0 [[ILogger]] components to dump the captured counters
// - *:context-info:*:*:1.0 (optional) [[ContextInfo]] to detect the context id and specify counters source
//
// See Tracer
// See CachedCounters
// See CompositeLogger
//
// Example:
// tracer = NewLogTracer();
// tracer.SetReferences(
// context.Background(),
// NewReferencesFromTuples(
// NewDescriptor("pip-services", "logger", "console", "default", "1.0"), NewConsoleLogger()
// )
// );
// timing := trcer.BeginTrace(context.Background(), "123", "mycomponent", "mymethod");
// ...
// timing.EndTrace(context.Background());
// if err != nil {
// timing.EndFailure(context.Background(), err);
// }
type LogTracer struct {
logger *log.CompositeLogger
logLevel log.LevelType
}
// NewLogTracer creates a new instance of the tracer.
func NewLogTracer() *LogTracer {
return &LogTracer{
logger: log.NewCompositeLogger(),
logLevel: log.LevelDebug,
}
}
// Configure component by passing configuration parameters.
// Parameters:
// - ctx context.Context
// - config configuration parameters to be set.
func (c *LogTracer) Configure(ctx context.Context, config *cconf.ConfigParams) {
logLvl, ok := config.GetAsObject("options.log_level")
if ok && logLvl == nil {
logLvl = c.logLevel
}
c.logLevel = log.LevelConverter.ToLogLevel(logLvl)
}
// SetReferences sets references to dependent components.
// Parameters:
// - ctx context.Context
// - references references to locate the component dependencies.
func (c *LogTracer) SetReferences(ctx context.Context, references cref.IReferences) {
c.logger.SetReferences(ctx, references)
}
func (c *LogTracer) logTrace(ctx context.Context, correlationId string, component string, operation string, err error, duration int64) {
builder := ""
if err != nil {
builder += "Failed to execute "
} else {
builder += "Executed "
}
builder += component
builder += "."
builder += operation
if duration > 0 {
builder += " in " + cconv.StringConverter.ToString(duration) + " msec"
}
if err != nil {
c.logger.Error(ctx, correlationId, err, builder)
} else {
c.logger.Log(ctx, c.logLevel, correlationId, nil, builder)
}
}
// Trace records an operation trace with its name and duration
// Parameters:
// - ctx context.Context
// - correlationId (optional) transaction id to trace execution through call chain.
// - component a name of called component
// - operation a name of the executed operation.
// - duration execution duration in milliseconds.
func (c *LogTracer) Trace(ctx context.Context, correlationId string, component string, operation string, duration int64) {
c.logTrace(ctx, correlationId, component, operation, nil, duration)
}
// Failure records an operation failure with its name, duration and error
// Parameters:
// - ctx context.Context
// - correlationId (optional) transaction id to trace execution through call chain.
// - component a name of called component
// - operation a name of the executed operation.
// - error an error object associated with this trace.
// - duration execution duration in milliseconds.
func (c *LogTracer) Failure(ctx context.Context, correlationId string, component string, operation string, err error, duration int64) {
c.logTrace(ctx, correlationId, component, operation, err, duration)
}
// BeginTrace begins recording an operation trace
// Parameters:
// - ctx context.Context
// - correlationId (optional) transaction id to trace execution through call chain.
// - component a name of called component
// - operation a name of the executed operation.
// Returns: a trace timing object.
func (c *LogTracer) BeginTrace(ctx context.Context, correlationId string, component string, operation string) *TraceTiming {
return NewTraceTiming(correlationId, component, operation, c)
}