Skip to content

Commit

Permalink
Allow custom log.Logger
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
  • Loading branch information
jkroepke committed Apr 22, 2023
1 parent 2f04d2e commit 769f5e1
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions promlog/log.go
Expand Up @@ -111,13 +111,16 @@ type Config struct {
// New returns a new leveled oklog logger. Each logged line will be annotated
// with a timestamp. The output always goes to stderr.
func New(config *Config) log.Logger {
var l log.Logger
if config.Format != nil && config.Format.s == "json" {
l = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
return NewWithLogger(log.NewJSONLogger(os.Stderr), config)
} else {
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
return NewWithLogger(log.NewLogfmtLogger(os.Stderr), config)
}
}

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

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

0 comments on commit 769f5e1

Please sign in to comment.