Skip to content

Commit

Permalink
Merge pull request #474 from jkroepke/custom-logger
Browse files Browse the repository at this point in the history
Allow custom log.Logger
  • Loading branch information
roidelapluie committed May 11, 2023
2 parents e27922a + 01d2b4a commit f505d86
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 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))
} else {
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
return NewWithLogger(log.NewJSONLogger(log.NewSyncWriter(os.Stderr)), config)
}

return NewWithLogger(log.NewLogfmtLogger(log.NewSyncWriter(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))
} else {
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
return NewDynamicWithLogger(log.NewJSONLogger(log.NewSyncWriter(os.Stderr)), config)
}

return NewDynamicWithLogger(log.NewLogfmtLogger(log.NewSyncWriter(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 f505d86

Please sign in to comment.