mirror of
https://github.com/golang/go
synced 2024-11-19 04:54:41 -07:00
5d16bdd7b5
Also use it for errors that were otherwise silently dropped This makes it much easier to debug problems. Also added command line control over whether the rpc trace messages are printed, which allows you to read the log, otherwise the file edit messages swamp the log. Change-Id: I7b70fd18034a87b2964e6d6d5f6f33dcaf7d8ea8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/170178 Reviewed-by: Rebecca Stambler <rstambler@golang.org>
72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
// Copyright 2018 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 xlog
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
)
|
|
|
|
// Logger is a wrapper over a sink to provide a clean API over the core log
|
|
// function.
|
|
type Logger struct {
|
|
sink Sink
|
|
}
|
|
|
|
// Level indicates the severity of the logging message.
|
|
type Level int
|
|
|
|
const (
|
|
ErrorLevel = Level(iota)
|
|
InfoLevel
|
|
DebugLevel
|
|
)
|
|
|
|
// Sink is the interface to something that consumes logging messages.
|
|
// This can be implemented and then registered with a context to control the
|
|
// destination or formatting of logging.
|
|
type Sink interface {
|
|
Log(ctx context.Context, level Level, message string)
|
|
}
|
|
|
|
// StdSink is a Sink that writes to the standard log package.
|
|
type StdSink struct{}
|
|
|
|
// New returns a logger for the provided sink.
|
|
func New(sink Sink) Logger {
|
|
return Logger{sink: sink}
|
|
}
|
|
|
|
// Errorf is intended for the logging of errors that we could not easily return
|
|
// to the client but that caused problems internally.
|
|
func (l Logger) Errorf(ctx context.Context, format string, args ...interface{}) {
|
|
l.sink.Log(ctx, ErrorLevel, fmt.Sprintf(format, args...))
|
|
}
|
|
|
|
// Infof is intended for logging of messages that may help the user understand
|
|
// the behavior or be useful in a bug report.
|
|
func (l Logger) Infof(ctx context.Context, format string, args ...interface{}) {
|
|
l.sink.Log(ctx, InfoLevel, fmt.Sprintf(format, args...))
|
|
}
|
|
|
|
// Debugf is intended to be used only while debugging.
|
|
func (l Logger) Debugf(ctx context.Context, format string, args ...interface{}) {
|
|
l.sink.Log(ctx, DebugLevel, fmt.Sprintf(format, args...))
|
|
}
|
|
|
|
// Log implements Sink for the StdSink.
|
|
// It writes the message using log.Print with a level based prefix.
|
|
func (StdSink) Log(ctx context.Context, level Level, message string) {
|
|
switch level {
|
|
case ErrorLevel:
|
|
log.Print("Error: ", message)
|
|
case InfoLevel:
|
|
log.Print("Info: ", message)
|
|
case DebugLevel:
|
|
log.Print("Debug: ", message)
|
|
}
|
|
}
|