-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce logging adapter for jobqueue
- Loading branch information
1 parent
c43b499
commit a8afca4
Showing
5 changed files
with
166 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package slogger | ||
|
||
import ( | ||
"github.com/osbuild/osbuild-composer/pkg/jobqueue" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type simpleLogrus struct { | ||
logger *logrus.Logger | ||
} | ||
|
||
func NewLogrusLogger(logger *logrus.Logger) jobqueue.SimpleLogger { | ||
return &simpleLogrus{logger: logger} | ||
} | ||
|
||
func (s *simpleLogrus) log(level logrus.Level, err error, msg string, args ...string) { | ||
if len(args)%2 != 0 { | ||
panic("log arguments must be even (key value pairs)") | ||
} | ||
var fields = make(logrus.Fields, len(args)/2+1) | ||
for i := 0; i < len(args); i += 2 { | ||
k := args[i] | ||
v := args[i+1] | ||
fields[k] = v | ||
} | ||
if err != nil { | ||
fields["error"] = err.Error() | ||
} | ||
s.logger.WithFields(fields).Log(level, msg) | ||
} | ||
|
||
func (s *simpleLogrus) Info(msg string, args ...string) { | ||
s.log(logrus.InfoLevel, nil, msg, args...) | ||
} | ||
|
||
func (s *simpleLogrus) Error(err error, msg string, args ...string) { | ||
s.log(logrus.ErrorLevel, err, msg, args...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package slogger | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func makeLogrus(buf *bytes.Buffer) *logrus.Logger { | ||
return &logrus.Logger{ | ||
Out: buf, | ||
Formatter: &logrus.TextFormatter{ | ||
DisableTimestamp: true, | ||
DisableColors: true, | ||
}, | ||
Hooks: make(logrus.LevelHooks), | ||
Level: logrus.DebugLevel, | ||
} | ||
|
||
} | ||
|
||
func TestInfo(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
l := makeLogrus(buf) | ||
sl := NewLogrusLogger(l) | ||
sl.Info("test") | ||
require.Equal(t, "level=info msg=test\n", buf.String()) | ||
} | ||
|
||
func TestError(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
l := makeLogrus(buf) | ||
sl := NewLogrusLogger(l) | ||
sl.Error(errors.New("e"), "test") | ||
require.Equal(t, "level=error msg=test error=e\n", buf.String()) | ||
} | ||
|
||
func TestErrorIsNil(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
l := makeLogrus(buf) | ||
sl := NewLogrusLogger(l) | ||
sl.Error(nil, "test") | ||
require.Equal(t, "level=error msg=test\n", buf.String()) | ||
} | ||
|
||
func TestInfoWithFields(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
l := makeLogrus(buf) | ||
sl := NewLogrusLogger(l) | ||
sl.Info("test", "key", "value") | ||
require.Equal(t, "level=info msg=test key=value\n", buf.String()) | ||
} | ||
|
||
func TestErrorWithFields(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
l := makeLogrus(buf) | ||
sl := NewLogrusLogger(l) | ||
sl.Error(errors.New("e"), "test", "key", "value") | ||
require.Equal(t, "level=error msg=test error=e key=value\n", buf.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package slogger | ||
|
||
import ( | ||
"github.com/osbuild/osbuild-composer/pkg/jobqueue" | ||
) | ||
|
||
type noopLogger struct { | ||
} | ||
|
||
func NewNoopLogger() jobqueue.SimpleLogger { | ||
return &noopLogger{} | ||
} | ||
|
||
func (s *noopLogger) Info(_ string, _ ...string) { | ||
} | ||
|
||
func (s *noopLogger) Error(_ error, _ string, _ ...string) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters