mediamtx/internal/logger/logger_test.go
Eugene Marushchenko 9e9fae9a10
Some checks are pending
code_lint / go (push) Waiting to run
code_lint / go_mod (push) Waiting to run
code_lint / docs (push) Waiting to run
code_lint / api_docs (push) Waiting to run
code_test / test_64 (push) Waiting to run
code_test / test_32 (push) Waiting to run
code_test / test_e2e (push) Waiting to run
add structured logging (#5219)
Co-authored-by: aler9 <46489434+aler9@users.noreply.github.com>
2025-12-27 20:42:06 +01:00

77 lines
1.8 KiB
Go

package logger
import (
"bytes"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestLoggerToStdout(t *testing.T) {
for _, ca := range []string{
"plain",
"structured",
} {
t.Run(ca, func(t *testing.T) {
var buf bytes.Buffer
l := &Logger{
Destinations: []Destination{DestinationStdout},
Structured: (ca == "structured"),
timeNow: func() time.Time { return time.Date(2003, 11, 4, 23, 15, 8, 0, time.UTC) },
stdout: &buf,
}
err := l.Initialize()
require.NoError(t, err)
defer l.Close()
l.Log(Info, "test format %d", 123)
if ca == "plain" {
require.Equal(t, "2003/11/04 23:15:08 INF test format 123\n", buf.String())
} else {
require.Equal(t, `{"timestamp":"2003-11-04T23:15:08Z",`+
`"level":"INF","message":"test format 123"}`+"\n", buf.String())
}
})
}
}
func TestLoggerToFile(t *testing.T) {
for _, ca := range []string{
"plain",
"structured",
} {
t.Run(ca, func(t *testing.T) {
tempFile, err := os.CreateTemp(os.TempDir(), "mtx-logger-")
require.NoError(t, err)
defer os.Remove(tempFile.Name())
defer tempFile.Close()
l := &Logger{
Level: Debug,
Destinations: []Destination{DestinationFile},
Structured: ca == "structured",
File: tempFile.Name(),
timeNow: func() time.Time { return time.Date(2003, 11, 4, 23, 15, 8, 0, time.UTC) },
}
err = l.Initialize()
require.NoError(t, err)
defer l.Close()
l.Log(Info, "test format %d", 123)
buf, err := os.ReadFile(tempFile.Name())
require.NoError(t, err)
if ca == "plain" {
require.Equal(t, "2003/11/04 23:15:08 INF test format 123\n", string(buf))
} else {
require.Equal(t, `{"timestamp":"2003-11-04T23:15:08Z",`+
`"level":"INF","message":"test format 123"}`+"\n", string(buf))
}
})
}
}