-
Notifications
You must be signed in to change notification settings - Fork 22
/
logging.go
66 lines (55 loc) · 1.47 KB
/
logging.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
// Copyright 2020 The Swarm 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 logging provides the logger interface abstraction
// and implementation for Bee. It uses logrus under the hood.
package logging
import (
"io"
"github.com/sirupsen/logrus"
)
type Logger interface {
Tracef(format string, args ...interface{})
Trace(args ...interface{})
Debugf(format string, args ...interface{})
Debug(args ...interface{})
Infof(format string, args ...interface{})
Info(args ...interface{})
Warningf(format string, args ...interface{})
Warning(args ...interface{})
Errorf(format string, args ...interface{})
Error(args ...interface{})
WithField(key string, value interface{}) *logrus.Entry
WithFields(fields logrus.Fields) *logrus.Entry
WriterLevel(logrus.Level) *io.PipeWriter
NewEntry() *logrus.Entry
GetLevel() string
}
type logger struct {
*logrus.Logger
metrics metrics
}
func New(w io.Writer, level logrus.Level, lokiEndpoint string) Logger {
l := logrus.New()
l.SetOutput(w)
l.SetLevel(level)
l.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
}
metrics := newMetrics()
l.AddHook(metrics)
if lokiEndpoint != "" {
loki := newLoki(lokiEndpoint)
l.AddHook(loki)
}
return &logger{
Logger: l,
metrics: metrics,
}
}
func (l *logger) NewEntry() *logrus.Entry {
return logrus.NewEntry(l.Logger)
}
func (l *logger) GetLevel() string {
return l.Level.String()
}