Skip to content

Commit

Permalink
Set correct call-depth for logger
Browse files Browse the repository at this point in the history
When using the logger with `log.Lshortfile` or `log.Llongfile`, we need to make sure that correct number of frames are skipped from the call-stack when deriving file-name and line-number.
  • Loading branch information
codemedic committed Jul 15, 2021
1 parent 595074a commit 1c502e8
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions internal/log/stdlogger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package log

import stdlog "log"
import (
"fmt"
stdlog "log"
)

var (
debugPrefix = "DEBUG: "
Expand All @@ -9,36 +12,40 @@ var (
errorPrefix = "ERROR: "
)

func logf(prefix string, format string, value ...interface{}) {
_ = stdlog.Output(3, fmt.Sprintf(prefix+format+"\n", value...))
}

type StdLogger struct{}

func (s StdLogger) Debugf(format string, value ...interface{}) {
stdlog.Printf(debugPrefix+format+"\n", value...)
logf(debugPrefix, format, value...)
}

func (s StdLogger) Debug(message string) {
s.Debugf("%s", message)
logf(debugPrefix, "%s", message)
}

func (s StdLogger) Infof(format string, value ...interface{}) {
stdlog.Printf(infoPrefix+format+"\n", value...)
logf(infoPrefix, format, value...)
}

func (s StdLogger) Info(message string) {
s.Infof("%s", message)
logf(infoPrefix, "%s", message)
}

func (s StdLogger) Warningf(format string, value ...interface{}) {
stdlog.Printf(warnPrefix+format+"\n", value...)
logf(warnPrefix, format, value...)
}

func (s StdLogger) Warning(message string) {
s.Warningf("%s", message)
logf(warnPrefix, "%s", message)
}

func (s StdLogger) Errorf(format string, value ...interface{}) {
stdlog.Printf(errorPrefix+format+"\n", value...)
logf(errorPrefix, format, value...)
}

func (s StdLogger) Error(message string) {
s.Errorf("%s", message)
logf(errorPrefix, "%s", message)
}

0 comments on commit 1c502e8

Please sign in to comment.