Skip to content

Commit 769f5e1

Browse files
committed
Allow custom log.Logger
Signed-off-by: Jan-Otto Kröpke <[email protected]>
1 parent 2f04d2e commit 769f5e1

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

promlog/log.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,16 @@ type Config struct {
111111
// New returns a new leveled oklog logger. Each logged line will be annotated
112112
// with a timestamp. The output always goes to stderr.
113113
func New(config *Config) log.Logger {
114-
var l log.Logger
115114
if config.Format != nil && config.Format.s == "json" {
116-
l = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
115+
return NewWithLogger(log.NewJSONLogger(os.Stderr), config)
117116
} else {
118-
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
117+
return NewWithLogger(log.NewLogfmtLogger(os.Stderr), config)
119118
}
119+
}
120120

121+
// NewWithLogger returns a new leveled oklog logger with a custom log.Logger.
122+
// Each logged line will be annotated with a timestamp.
123+
func NewWithLogger(l log.Logger, config *Config) log.Logger {
121124
if config.Level != nil {
122125
l = log.With(l, "ts", timestampFormat, "caller", log.Caller(5))
123126
l = level.NewFilter(l, config.Level.o)
@@ -131,13 +134,17 @@ func New(config *Config) log.Logger {
131134
// with a timestamp. The output always goes to stderr. Some properties can be
132135
// changed, like the level.
133136
func NewDynamic(config *Config) *logger {
134-
var l log.Logger
135137
if config.Format != nil && config.Format.s == "json" {
136-
l = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
138+
return NewDynamicWithLogger(log.NewJSONLogger(os.Stderr), config)
137139
} else {
138-
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
140+
return NewDynamicWithLogger(log.NewLogfmtLogger(os.Stderr), config)
139141
}
142+
}
140143

144+
// NewDynamicWithLogger returns a new leveled logger with a custom io.Writer.
145+
// Each logged line will be annotated with a timestamp.
146+
// Some properties can be changed, like the level.
147+
func NewDynamicWithLogger(l log.Logger, config *Config) *logger {
141148
lo := &logger{
142149
base: l,
143150
leveled: l,

0 commit comments

Comments
 (0)