Skip to content

Commit

Permalink
Merge pull request #55 from hashicorp/dani/f-level-arg
Browse files Browse the repository at this point in the history
Add a Log function for logging at a given level
  • Loading branch information
evanphx authored Jan 11, 2020
2 parents e8a977f + 574aaa6 commit 0e86804
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
13 changes: 13 additions & 0 deletions interceptlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ func NewInterceptLogger(opts *LoggerOptions) InterceptLogger {
return intercept
}

func (i *interceptLogger) Log(level Level, msg string, args ...interface{}) {
i.Logger.Log(level, msg, args...)
if atomic.LoadInt32(i.sinkCount) == 0 {
return
}

i.mu.Lock()
defer i.mu.Unlock()
for s := range i.Sinks {
s.Accept(i.Name(), level, msg, i.retrieveImplied(args...)...)
}
}

// Emit the message and args at TRACE level to log and sinks
func (i *interceptLogger) Trace(msg string, args ...interface{}) {
i.Logger.Trace(msg, args...)
Expand Down
23 changes: 14 additions & 9 deletions intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func newLogger(opts *LoggerOptions) *intLogger {

// Log a message and a set of key/value pairs if the given level is at
// or more severe that the threshold configured in the Logger.
func (l *intLogger) Log(name string, level Level, msg string, args ...interface{}) {
func (l *intLogger) log(name string, level Level, msg string, args ...interface{}) {
if level < Level(atomic.LoadInt32(l.level)) {
return
}
Expand All @@ -133,7 +133,7 @@ func (l *intLogger) Log(name string, level Level, msg string, args ...interface{
if l.json {
l.logJSON(t, name, level, msg, args...)
} else {
l.log(t, name, level, msg, args...)
l.logPlain(t, name, level, msg, args...)
}

l.writer.Flush(level)
Expand Down Expand Up @@ -171,7 +171,7 @@ func trimCallerPath(path string) string {
var logImplFile = regexp.MustCompile(`github.com/hashicorp/go-hclog/.+logger.go$`)

// Non-JSON logging format function
func (l *intLogger) log(t time.Time, name string, level Level, msg string, args ...interface{}) {
func (l *intLogger) logPlain(t time.Time, name string, level Level, msg string, args ...interface{}) {
l.writer.WriteString(t.Format(l.timeFormat))
l.writer.WriteByte(' ')

Expand Down Expand Up @@ -431,29 +431,34 @@ func (l intLogger) jsonMapEntry(t time.Time, name string, level Level, msg strin
return vals
}

// Emit the message and args at the provided level
func (l *intLogger) Log(level Level, msg string, args ...interface{}) {
l.log(l.Name(), level, msg, args...)
}

// Emit the message and args at DEBUG level
func (l *intLogger) Debug(msg string, args ...interface{}) {
l.Log(l.Name(), Debug, msg, args...)
l.log(l.Name(), Debug, msg, args...)
}

// Emit the message and args at TRACE level
func (l *intLogger) Trace(msg string, args ...interface{}) {
l.Log(l.Name(), Trace, msg, args...)
l.log(l.Name(), Trace, msg, args...)
}

// Emit the message and args at INFO level
func (l *intLogger) Info(msg string, args ...interface{}) {
l.Log(l.Name(), Info, msg, args...)
l.log(l.Name(), Info, msg, args...)
}

// Emit the message and args at WARN level
func (l *intLogger) Warn(msg string, args ...interface{}) {
l.Log(l.Name(), Warn, msg, args...)
l.log(l.Name(), Warn, msg, args...)
}

// Emit the message and args at ERROR level
func (l *intLogger) Error(msg string, args ...interface{}) {
l.Log(l.Name(), Error, msg, args...)
l.log(l.Name(), Error, msg, args...)
}

// Indicate that the logger would emit TRACE level logs
Expand Down Expand Up @@ -593,7 +598,7 @@ func (l *intLogger) checkWriterIsFile() *os.File {

// Accept implements the SinkAdapter interface
func (i *intLogger) Accept(name string, level Level, msg string, args ...interface{}) {
i.Log(name, level, msg, args...)
i.log(name, level, msg, args...)
}

// ImpliedArgs returns the loggers implied args
Expand Down
3 changes: 3 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ type Logger interface {
// Args are alternating key, val pairs
// keys must be strings
// vals can be any type, but display is implementation specific
// Emit a message and key/value pairs at a provided log level
Log(level Level, msg string, args ...interface{})

// Emit a message and key/value pairs at the TRACE level
Trace(msg string, args ...interface{})

Expand Down
2 changes: 2 additions & 0 deletions nulllogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func NewNullLogger() Logger {

type nullLogger struct{}

func (l *nullLogger) Log(level Level, msg string, args ...interface{}) {}

func (l *nullLogger) Trace(msg string, args ...interface{}) {}

func (l *nullLogger) Debug(msg string, args ...interface{}) {}
Expand Down

0 comments on commit 0e86804

Please sign in to comment.