Skip to content

Commit

Permalink
[RBR-1571]: allow log-level to be set on json logger (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmull3n authored Sep 17, 2024
1 parent 8b1aefe commit c727002
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
20 changes: 17 additions & 3 deletions logger/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type JSONLogEntry struct {
Metadata map[string]interface{} `json:"metadata,omitempty"`
// Logs Explorer allows filtering and display of this as `jsonPayload.component`.
Component string `json:"component,omitempty"`
logLevel LogLevel
}

// String renders an entry structure to the JSON format expected by Cloud Logging.
Expand All @@ -42,6 +43,7 @@ type jsonLogger struct {
sinkLogLevel LogLevel
noConsole bool
ts *time.Time // for unit testing
logLevel LogLevel
}

var _ Logger = (*jsonLogger)(nil)
Expand Down Expand Up @@ -95,6 +97,7 @@ func (c *jsonLogger) With(metadata map[string]interface{}) Logger {
noConsole: c.noConsole,
sink: c.sink,
sinkLogLevel: c.sinkLogLevel,
logLevel: c.logLevel,
}
}

Expand All @@ -112,6 +115,9 @@ func (c *jsonLogger) tokenize(val string) string {
}

func (c *jsonLogger) Log(level LogLevel, severity string, msg string, args ...interface{}) {
if level < c.logLevel && level < c.sinkLogLevel {
return
}
_msg := msg
if len(args) > 0 {
_msg = fmt.Sprintf(msg, args...)
Expand All @@ -124,7 +130,7 @@ func (c *jsonLogger) Log(level LogLevel, severity string, msg string, args ...in
Component: c.tokenize(c.component),
Timestamp: time.Now(),
}
if !c.noConsole {
if !c.noConsole && level >= c.logLevel {
log.Println(entry)
}
if c.sink != nil && level >= c.sinkLogLevel {
Expand Down Expand Up @@ -163,9 +169,17 @@ func (c *jsonLogger) Fatal(msg string, args ...interface{}) {
c.Log(LevelError, "ERROR", msg, args...)
}

func (c *jsonLogger) SetLogLevel(level LogLevel) {
c.logLevel = level
}

// NewJSONLogger returns a new Logger instance which can be used for structured logging
func NewJSONLogger() Logger {
return &jsonLogger{}
func NewJSONLogger(levels ...LogLevel) Logger {
if len(levels) > 0 {
return &jsonLogger{logLevel: levels[0]}
}
return &jsonLogger{logLevel: LevelDebug}

}

// NewJSONLoggerWithSink returns a new Logger instance using a sink and suppressing the console logging
Expand Down
54 changes: 54 additions & 0 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,57 @@ func TestCombinedLogger(t *testing.T) {
assert.Len(t, log.Logs, 1)
assert.Equal(t, `{"timestamp":"2023-10-22T12:30:00Z","message":"Ayyyyyy","severity":"INFO"}`, string(sink.buf))
}

func TestJSONLogger(t *testing.T) {

logger := NewJSONLogger().(*jsonLogger)

tests := []struct {
level LogLevel
shouldContain []string
shouldNotContain []string
}{
{
level: LevelTrace,
shouldContain: []string{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"},
shouldNotContain: []string{},
},
{
level: LevelDebug,
shouldContain: []string{"DEBUG", "INFO", "WARN", "ERROR"},
shouldNotContain: []string{"TRACE"},
},
{
level: LevelInfo,
shouldContain: []string{"INFO", "WARN", "ERROR"},
shouldNotContain: []string{"TRACE", "DEBUG"},
},
{
level: LevelWarn,
shouldContain: []string{"WARN", "ERROR"},
shouldNotContain: []string{"TRACE", "DEBUG", "INFO"},
},
{
level: LevelError,
shouldContain: []string{"ERROR"},
shouldNotContain: []string{"TRACE", "DEBUG", "INFO", "WARN"},
},
}

for _, tt := range tests {
logger.SetLogLevel(tt.level)
output := captureOutput(func() {
logger.Trace("This is a trace message")
logger.Debug("This is a debug message")
logger.Info("This is an info message")
logger.Warn("This is a warn message")
logger.Error("This is an error message")
})
for _, shouldContain := range tt.shouldContain {
assert.Contains(t, output, shouldContain)
}
for _, shouldNotContain := range tt.shouldNotContain {
assert.NotContains(t, output, shouldNotContain)
}
}
}

0 comments on commit c727002

Please sign in to comment.