Skip to content

Commit

Permalink
add non regexp log test
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Okhlopkov <[email protected]>
  • Loading branch information
Pavel Okhlopkov committed Oct 28, 2024
1 parent fe193ce commit 313fc09
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/unilogger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,47 @@ import (
)

func Test_Logger(t *testing.T) {
const (
message = "stub msg"
argKey = "stub_arg"
argValue = "arg"
)

buf := bytes.NewBuffer([]byte{})

logger := unilogger.NewLogger(unilogger.Options{
Level: slog.LevelDebug,
Output: buf,
TimeFunc: func(_ time.Time) time.Time {
parsedTime, err := time.Parse(time.DateTime, "2006-01-02 15:04:05")
if err != nil {
assert.NoError(t, err)
}

return parsedTime
},
})

logger.Trace(message, slog.String(argKey, argValue))
logger.Debug(message, slog.String(argKey, argValue))
logger.Info(message, slog.String(argKey, argValue))
logger.Warn(message, slog.String(argKey, argValue))
//test fatal
logger.Log(context.Background(), unilogger.LevelFatal.Level(), message, slog.String(argKey, argValue))

assert.Equal(t, buf.String(), `{"level":"debug","msg":"stub msg","source":"unilogger/logger_test.go:40","stub_arg":"arg","time":"2006-01-02T15:04:05Z"}`+"\n"+
`{"level":"info","msg":"stub msg","source":"unilogger/logger_test.go:41","stub_arg":"arg","time":"2006-01-02T15:04:05Z"}`+"\n"+
`{"level":"warn","msg":"stub msg","source":"unilogger/logger_test.go:42","stub_arg":"arg","time":"2006-01-02T15:04:05Z"}`+"\n"+
`{"level":"fatal","msg":"stub msg","source":"unilogger/logger_test.go:44","stub_arg":"arg","time":"2006-01-02T15:04:05Z"}`+"\n")

buf.Reset()

logger.Error(message, slog.String(argKey, argValue))

assert.Contains(t, buf.String(), `{"level":"error","msg":"stub msg","stub_arg":"arg","stacktrace":"`)
}

func Test_LoggerFormat(t *testing.T) {
t.Parallel()

const (
Expand Down

0 comments on commit 313fc09

Please sign in to comment.