/
log.go
59 lines (50 loc) · 1.65 KB
/
log.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
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package log is a context based logging package, designed to interact well
// with both the lsp protocol and the other telemetry packages.
package log
import (
"context"
"time"
"github.com/anz-bank/sysl/pkg/lspimpl/lspframework/telemetry"
"github.com/anz-bank/sysl/pkg/lspimpl/lspframework/telemetry/export"
)
type Event telemetry.Event
// With sends a tag list to the installed loggers.
func With(ctx context.Context, tags ...telemetry.Tag) {
export.ProcessEvent(ctx, telemetry.Event{
Type: telemetry.EventLog,
At: time.Now(),
Tags: tags,
})
}
// Print takes a message and a tag list and combines them into a single tag
// list before delivering them to the loggers.
func Print(ctx context.Context, message string, tags ...telemetry.Tag) {
export.ProcessEvent(ctx, telemetry.Event{
Type: telemetry.EventLog,
At: time.Now(),
Message: message,
Tags: tags,
})
}
// Error takes a message and a tag list and combines them into a single tag
// list before delivering them to the loggers. It captures the error in the
// delivered event.
func Error(ctx context.Context, message string, err error, tags ...telemetry.Tag) {
if err == nil {
err = errorString(message)
message = ""
}
export.ProcessEvent(ctx, telemetry.Event{
Type: telemetry.EventLog,
At: time.Now(),
Message: message,
Error: err,
Tags: tags,
})
}
type errorString string
// Error allows errorString to conform to the error interface.
func (err errorString) Error() string { return string(err) }