-
Notifications
You must be signed in to change notification settings - Fork 68
/
gokit.go
73 lines (61 loc) · 2.06 KB
/
gokit.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
// Provenance-includes-location: https://github.com/weaveworks/common/blob/main/logging/gokit.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: Weaveworks Ltd.
package log
import (
"fmt"
"io"
"os"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
)
const (
LogfmtFormat = "logfmt"
JSONFormat = "json"
)
// NewGoKit creates a new GoKit logger with the given format.
// If the given format is empty or unknown, logfmt is used.
// No additional fields nor filters are added to the created logger, and
// if they are required, the caller is expected to add them.
func NewGoKit(format string) log.Logger {
writer := log.NewSyncWriter(os.Stderr)
return newGoKit(format, writer)
}
// NewGoKitWithLevel creates a new GoKit logger with the given level and format.
// If the given format is empty or unknown, logfmt is used.
func NewGoKitWithLevel(lvl Level, format string) log.Logger {
logger := NewGoKit(format)
return level.NewFilter(logger, lvl.Option)
}
// NewGoKitWithWriter creates a new GoKit logger with the given format and writer.
// The input writer must be provided, must be thread-safe, and the caller is
// expected to guarantee these requirements.
// If the given format is empty or unknown, logfmt is used.
// No additional fields nor filters are added to the created logger, and
// if they are required, the caller is expected to add them.
func NewGoKitWithWriter(format string, writer io.Writer) log.Logger {
return newGoKit(format, writer)
}
func newGoKit(format string, writer io.Writer) log.Logger {
if format == JSONFormat {
return log.NewJSONLogger(writer)
}
return log.NewLogfmtLogger(writer)
}
// stand-alone for test purposes
func addStandardFields(logger log.Logger) log.Logger {
return log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.Caller(5))
}
type Sprintf struct {
format string
args []interface{}
}
func LazySprintf(format string, args ...interface{}) *Sprintf {
return &Sprintf{
format: format,
args: args,
}
}
func (s *Sprintf) String() string {
return fmt.Sprintf(s.format, s.args...)
}