2020-03-09 19:57:53 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-01 13:14:15 +00:00
|
|
|
"io"
|
2020-03-09 19:57:53 +00:00
|
|
|
"os"
|
2022-03-03 11:20:43 +00:00
|
|
|
"runtime"
|
2020-03-09 19:57:53 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-03-03 11:20:43 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-03-09 19:57:53 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-08-03 09:55:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2020-03-09 19:57:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldWriteLogsToFile(t *testing.T) {
|
2022-12-04 09:57:28 +00:00
|
|
|
dir := t.TempDir()
|
2020-03-09 19:57:53 +00:00
|
|
|
|
|
|
|
path := fmt.Sprintf("%s/authelia.log", dir)
|
2022-12-04 09:57:28 +00:00
|
|
|
err := InitializeLogger(schema.LogConfiguration{Format: "text", FilePath: path, KeepStdout: false}, false)
|
2021-05-31 04:02:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
Logger().Info("This is a test")
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-12-01 13:14:15 +00:00
|
|
|
b, err := io.ReadAll(f)
|
2021-05-31 04:02:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Contains(t, string(b), "level=info msg=\"This is a test\"\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldWriteLogsToFileAndStdout(t *testing.T) {
|
2022-12-04 09:57:28 +00:00
|
|
|
dir := t.TempDir()
|
2021-05-31 04:02:54 +00:00
|
|
|
|
|
|
|
path := fmt.Sprintf("%s/authelia.log", dir)
|
2022-12-04 09:57:28 +00:00
|
|
|
err := InitializeLogger(schema.LogConfiguration{Format: "text", FilePath: path, KeepStdout: true}, false)
|
2020-03-09 19:57:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
Logger().Info("This is a test")
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-12-01 13:14:15 +00:00
|
|
|
b, err := io.ReadAll(f)
|
2020-03-09 19:57:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Contains(t, string(b), "level=info msg=\"This is a test\"\n")
|
|
|
|
}
|
2020-11-24 23:46:41 +00:00
|
|
|
|
|
|
|
func TestShouldFormatLogsAsJSON(t *testing.T) {
|
2022-12-04 09:57:28 +00:00
|
|
|
dir := t.TempDir()
|
2020-11-24 23:46:41 +00:00
|
|
|
|
|
|
|
path := fmt.Sprintf("%s/authelia.log", dir)
|
2022-12-04 09:57:28 +00:00
|
|
|
err := InitializeLogger(schema.LogConfiguration{Format: "json", FilePath: path, KeepStdout: false}, false)
|
2020-11-24 23:46:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
Logger().Info("This is a test")
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-12-01 13:14:15 +00:00
|
|
|
b, err := io.ReadAll(f)
|
2020-11-24 23:46:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Contains(t, string(b), "{\"level\":\"info\",\"msg\":\"This is a test\",")
|
|
|
|
}
|
2022-03-03 11:20:43 +00:00
|
|
|
|
|
|
|
func TestShouldRaiseErrorOnInvalidFile(t *testing.T) {
|
|
|
|
err := InitializeLogger(schema.LogConfiguration{FilePath: "/not/a/valid/path/to.log"}, false)
|
|
|
|
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
assert.EqualError(t, err, "open /not/a/valid/path/to.log: The system cannot find the path specified.")
|
|
|
|
default:
|
|
|
|
assert.EqualError(t, err, "open /not/a/valid/path/to.log: no such file or directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetLevels(t *testing.T) {
|
|
|
|
assert.Equal(t, logrus.InfoLevel, logrus.GetLevel())
|
|
|
|
|
|
|
|
setLevelStr("error", false)
|
|
|
|
assert.Equal(t, logrus.ErrorLevel, logrus.GetLevel())
|
|
|
|
|
|
|
|
setLevelStr("warn", false)
|
|
|
|
assert.Equal(t, logrus.WarnLevel, logrus.GetLevel())
|
|
|
|
|
|
|
|
setLevelStr("info", false)
|
|
|
|
assert.Equal(t, logrus.InfoLevel, logrus.GetLevel())
|
|
|
|
|
|
|
|
setLevelStr("debug", false)
|
|
|
|
assert.Equal(t, logrus.DebugLevel, logrus.GetLevel())
|
|
|
|
|
|
|
|
setLevelStr("trace", false)
|
|
|
|
assert.Equal(t, logrus.TraceLevel, logrus.GetLevel())
|
|
|
|
}
|