2019-07-10 13:19:29 -06:00
|
|
|
// 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"
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry"
|
|
|
|
"golang.org/x/tools/internal/telemetry/export"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/tag"
|
2019-07-10 13:19:29 -06:00
|
|
|
)
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
type Event telemetry.Event
|
2019-07-10 13:19:29 -06:00
|
|
|
|
|
|
|
// With sends a tag list to the installed loggers.
|
2019-08-14 10:51:42 -06:00
|
|
|
func With(ctx context.Context, tags ...telemetry.Tag) {
|
|
|
|
export.Log(ctx, telemetry.Event{
|
|
|
|
At: time.Now(),
|
|
|
|
Tags: tags,
|
2019-07-10 13:19:29 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 ...tag.Tagger) {
|
2019-08-14 10:51:42 -06:00
|
|
|
export.Log(ctx, telemetry.Event{
|
|
|
|
At: time.Now(),
|
|
|
|
Message: message,
|
|
|
|
Tags: tag.Tags(ctx, tags...),
|
2019-07-10 13:19:29 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-21 14:25:13 -07:00
|
|
|
// 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.
|
2019-07-10 13:19:29 -06:00
|
|
|
func Error(ctx context.Context, message string, err error, tags ...tag.Tagger) {
|
2019-08-14 10:51:42 -06:00
|
|
|
if err == nil {
|
|
|
|
err = errorString(message)
|
|
|
|
message = ""
|
2019-07-10 13:19:29 -06:00
|
|
|
}
|
2019-08-14 10:51:42 -06:00
|
|
|
export.Log(ctx, telemetry.Event{
|
|
|
|
At: time.Now(),
|
|
|
|
Message: message,
|
|
|
|
Error: err,
|
|
|
|
Tags: tag.Tags(ctx, tags...),
|
2019-07-10 13:19:29 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
type errorString string
|
2019-08-13 11:35:13 -06:00
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
// Error allows errorString to conform to the error interface.
|
|
|
|
func (err errorString) Error() string { return string(err) }
|