Skip to content

Commit

Permalink
logging: Pogreb now logs into our logger, not stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
mitjat committed Feb 1, 2024
1 parent 988762b commit 22991dc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ package common
import (
"fmt"
"io"
stdLog "log"
"os"

"github.com/akrylysov/pogreb"
coreLogging "github.com/oasisprotocol/oasis-core/go/common/logging"

"github.com/oasisprotocol/nexus/config"
Expand Down Expand Up @@ -50,6 +52,10 @@ func Init(cfg *config.Config) error {
return err
}

// Initialize pogreb logging.
pogrebLogger := RootLogger().WithModule("pogreb").WithCallerUnwind(7)
pogreb.SetLogger(stdLog.New(log.WriterIntoLogger(*pogrebLogger), "", 0))

// Initialize Prometheus service.
if cfg.Metrics != nil {
promServer, err := metrics.NewPullService(cfg.Metrics.PullEndpoint, rootLogger)
Expand Down
33 changes: 33 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,40 @@ func (l *Logger) WithModule(module string) *Logger {
}
}

// WithCallerUnwind returns a clone of the logger where the `caller`
// value will show the n-th entry on the call stack at the time of logging.
func (l *Logger) WithCallerUnwind(n int) *Logger {
return &Logger{
logger: log.With(l.logger, "caller", log.Caller(n)),
level: l.level,
module: l.module,
}
}

// Level is the logging level.
func (l *Logger) Level() Level {
return l.level
}

// An io.Writer impl that writes every string to the backing logger `l` at Info level.
type writerIntoLoggerImpl struct {
Logger
}

var _ io.Writer = (*writerIntoLoggerImpl)(nil)

func (l *writerIntoLoggerImpl) Write(msg []byte) (n int, err error) {
// Strip trailing newlines from message.
origLen := len(msg)
for len(msg) > 0 && msg[len(msg)-1] == '\n' {
msg = msg[:len(msg)-1]
}

// Write message to backing logger.
l.Info(string(msg))
return origLen, nil
}

func WriterIntoLogger(l Logger) io.Writer {
return &writerIntoLoggerImpl{l}
}

0 comments on commit 22991dc

Please sign in to comment.