50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
|
package logger_test
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"testing"
|
||
|
|
||
|
"git.ar21.de/yolokube/grafana-backuper/internal/logger"
|
||
|
"github.com/rs/zerolog"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestCliLoggerLayout_DebugLevel(t *testing.T) {
|
||
|
var buf bytes.Buffer
|
||
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||
|
|
||
|
log := logger.CliLoggerLayout(&buf)
|
||
|
log.Debug().Msg("test message")
|
||
|
|
||
|
output := buf.String()
|
||
|
assert.Contains(t, output, "test message")
|
||
|
assert.Contains(t, output, "DBG") // Assuming zerolog adds a short level indicator like "DBG"
|
||
|
assert.Contains(t, output, ".go:") // Ensures the caller file and line are included
|
||
|
}
|
||
|
|
||
|
func TestCliLoggerLayout_NonDebugLevel(t *testing.T) {
|
||
|
var buf bytes.Buffer
|
||
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||
|
|
||
|
log := logger.CliLoggerLayout(&buf)
|
||
|
log.Info().Msg("test message")
|
||
|
|
||
|
output := buf.String()
|
||
|
assert.Contains(t, output, "test message")
|
||
|
assert.NotContains(t, output, "DBG") // Since we're at Info level
|
||
|
assert.NotContains(t, output, ".go:") // Caller information should not be included
|
||
|
}
|
||
|
|
||
|
func TestJSONLoggerLayout(t *testing.T) {
|
||
|
var buf bytes.Buffer
|
||
|
|
||
|
log := logger.JSONLoggerLayout(&buf)
|
||
|
log.Info().Msg("test message")
|
||
|
|
||
|
output := buf.String()
|
||
|
assert.Contains(t, output, `"message":"test message"`)
|
||
|
assert.Contains(t, output, `"level":"info"`)
|
||
|
assert.Contains(t, output, `"time":`) // Timestamp should be included
|
||
|
assert.Contains(t, output, `"caller":`) // Caller should be included
|
||
|
}
|