/
logger.go
157 lines (130 loc) · 5.28 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package logger
import (
"log"
keybase1 "github.com/adamwalz/keybase-client/go/protocol/keybase1"
"golang.org/x/net/context"
)
type ExternalHandler interface {
Log(level keybase1.LogLevel, format string, args []interface{})
}
type BaseLogger interface {
Debug(format string, args ...interface{})
Info(format string, args ...interface{})
Warning(format string, args ...interface{})
Error(format string, args ...interface{})
}
type ContextInterface interface {
BaseLogger
Ctx() context.Context
UpdateContextToLoggerContext(context.Context) ContextInterface
}
type Logger interface {
BaseLogger
// CDebugf logs a message at debug level, with a context and
// formatting args.
CDebugf(ctx context.Context, format string, args ...interface{})
// CInfo logs a message at info level, with a context and formatting args.
CInfof(ctx context.Context, format string, args ...interface{})
// Notice logs a message at notice level, with formatting args.
Notice(format string, args ...interface{})
// CNoticef logs a message at notice level, with a context and
// formatting args.
CNoticef(ctx context.Context, format string, args ...interface{})
// Warning logs a message at warning level, with formatting args.
CWarningf(ctx context.Context, format string, args ...interface{})
// Error logs a message at error level, with formatting args
Errorf(format string, args ...interface{})
// CErrorf logs a message at error level, with a context and
// formatting args.
CErrorf(ctx context.Context, format string, args ...interface{})
// Critical logs a message at critical level, with formatting args.
Critical(format string, args ...interface{})
// CCriticalf logs a message at critical level, with a context and
// formatting args.
CCriticalf(ctx context.Context, format string, args ...interface{})
// Fatalf logs a message at fatal level, with formatting args.
Fatalf(format string, args ...interface{})
// Fatalf logs a message at fatal level, with a context and formatting args.
CFatalf(ctx context.Context, format string, args ...interface{})
// Profile logs a profile message, with formatting args.
Profile(fmts string, arg ...interface{})
// Returns a logger that is like the current one, except with
// more logging depth added on.
CloneWithAddedDepth(depth int) Logger
// Configure sets the style, debug level, and filename of the
// logger. Output isn't redirected to the file until
// the log file rotation is configured.
Configure(style string, debug bool, filename string)
// SetExternalHandler sets a handler that will be called with every log message.
SetExternalHandler(handler ExternalHandler)
}
type InternalLogger struct {
log *log.Logger
}
func NewInternalLogger(log *log.Logger) *InternalLogger {
return &InternalLogger{
log: log,
}
}
func (l *InternalLogger) Debug(fmt string, arg ...interface{}) {
l.log.Printf(fmt, arg...)
}
func (l *InternalLogger) CDebugf(ctx context.Context, fmt string,
arg ...interface{}) {
l.log.Printf(prepareString(ctx, fmt), arg...)
}
func (l *InternalLogger) Info(fmt string, arg ...interface{}) {
l.log.Printf(keybase1.LogLevel_INFO.String()+fmt, arg...)
}
func (l *InternalLogger) CInfof(ctx context.Context, fmt string,
arg ...interface{}) {
l.log.Printf(keybase1.LogLevel_INFO.String()+prepareString(ctx, fmt), arg...)
}
func (l *InternalLogger) Notice(fmt string, arg ...interface{}) {
l.log.Printf(keybase1.LogLevel_NOTICE.String()+fmt, arg...)
}
func (l *InternalLogger) CNoticef(ctx context.Context, fmt string,
arg ...interface{}) {
l.log.Printf(keybase1.LogLevel_NOTICE.String()+prepareString(ctx, fmt), arg...)
}
func (l *InternalLogger) Warning(fmt string, arg ...interface{}) {
l.log.Printf(keybase1.LogLevel_WARN.String()+fmt, arg...)
}
func (l *InternalLogger) CWarningf(ctx context.Context, fmt string,
arg ...interface{}) {
l.log.Printf(keybase1.LogLevel_WARN.String()+prepareString(ctx, fmt), arg...)
}
func (l *InternalLogger) Error(fmt string, arg ...interface{}) {
l.log.Printf(keybase1.LogLevel_ERROR.String()+fmt, arg...)
}
func (l *InternalLogger) Errorf(fmt string, arg ...interface{}) {
l.log.Printf(keybase1.LogLevel_ERROR.String()+fmt, arg...)
}
func (l *InternalLogger) CErrorf(ctx context.Context, fmt string,
arg ...interface{}) {
l.log.Printf(keybase1.LogLevel_ERROR.String()+prepareString(ctx, fmt), arg...)
}
func (l *InternalLogger) Critical(fmt string, arg ...interface{}) {
l.log.Printf(keybase1.LogLevel_CRITICAL.String()+fmt, arg...)
}
func (l *InternalLogger) CCriticalf(ctx context.Context, fmt string,
arg ...interface{}) {
l.log.Printf(keybase1.LogLevel_CRITICAL.String()+prepareString(ctx, fmt), arg...)
}
func (l *InternalLogger) Fatalf(fmt string, arg ...interface{}) {
l.log.Fatalf(fmt, arg...)
}
func (l *InternalLogger) CFatalf(ctx context.Context, fmt string,
arg ...interface{}) {
l.log.Fatalf(prepareString(ctx, fmt), arg...)
}
func (l *InternalLogger) Profile(fmts string, arg ...interface{}) {
l.log.Printf(keybase1.LogLevel_DEBUG.String()+fmts, arg...)
}
func (l *InternalLogger) Configure(style string, debug bool, filename string) {}
func (l *InternalLogger) SetExternalHandler(handler ExternalHandler) {}
func (l *InternalLogger) CloneWithAddedDepth(depth int) Logger {
return l
}