-
Notifications
You must be signed in to change notification settings - Fork 173
/
logger.go
124 lines (100 loc) · 2.67 KB
/
logger.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package goka
import (
"fmt"
"log"
"os"
"strings"
"github.com/IBM/sarama"
)
var defaultLogger = &std{
log: log.New(os.Stderr, "", log.LstdFlags),
}
// Logger is the interface Goka and its subpackages use for logging.
type Logger interface {
// Print will simply print the params
Print(...interface{})
// Print will simply print the params
Println(...interface{})
// Printf will be used for informational messages. These can be thought of
// having an 'Info'-level in a structured logger.
Printf(string, ...interface{})
}
type logger interface {
Logger
// Debugf is used for debugging messages, mostly for debugging goka itself.
// It is turned off unless goka is initialized
Debugf(string, ...interface{})
// PrefixedLogger returns a logger that prefixes all messages with passed prefix
Prefix(string) logger
CurrentPrefix() string
StackPrefix(prefix string) logger
}
// std bridges the logger calls to the standard library log.
type std struct {
log Logger
debug bool
prefixPath []string
prefix string
}
func (s *std) Print(msgs ...interface{}) {
s.log.Print(msgs...)
}
func (s *std) Println(msgs ...interface{}) {
s.log.Print(msgs...)
}
func (s *std) Printf(msg string, args ...interface{}) {
s.log.Printf(fmt.Sprintf("%s%s", s.prefix, msg), args...)
}
func (s *std) Debugf(msg string, args ...interface{}) {
if s.debug {
s.log.Printf(fmt.Sprintf("%s%s", s.prefix, msg), args...)
}
}
func (s *std) Prefix(prefix string) logger {
return s.StackPrefix(prefix).(*std)
}
// Default returns the standard library logger
func DefaultLogger() Logger {
return defaultLogger
}
// Debug enables or disables debug logging using the global logger.
// The goka debugging setting is applied to any custom loggers in goka components (Processors, Views, Emitters).
func Debug(gokaDebug, saramaDebug bool) {
defaultLogger.debug = gokaDebug
if saramaDebug {
SetSaramaLogger((&std{log: defaultLogger, debug: true}).Prefix("Sarama"))
}
}
func SetSaramaLogger(logger Logger) {
sarama.Logger = logger
}
// newLogger creates a new goka logger
func wrapLogger(l Logger, debug bool) logger {
return &std{
log: l,
debug: debug,
}
}
func (s *std) CurrentPrefix() string {
return s.prefix
}
func (s *std) StackPrefix(prefix string) logger {
var prefPath []string
// append existing path
prefPath = append(prefPath, s.prefixPath...)
// if new is not empty, append to path
if prefix != "" {
prefPath = append(prefPath, prefix)
}
// make new prefix
newPrefix := strings.Join(prefPath, " > ")
if newPrefix != "" {
newPrefix = "[" + newPrefix + "] "
}
return &std{
log: s.log,
prefixPath: prefPath,
prefix: newPrefix,
debug: s.debug,
}
}