mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
a29eeb52b6
This change adds the ability to format Authelia's log output as JSON. Example below: ``` {"level":"info","msg":"Logging severity set to info","time":"2020-01-01T00:00:00+11:00"} {"level":"info","msg":"Authelia is listening for non-TLS connections on 0.0.0.0:9091","time":"2020-01-01T00:00:00+11:00"} ```
59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package logging
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestShouldWriteLogsToFile(t *testing.T) {
|
|
dir, err := ioutil.TempDir("/tmp", "logs-dir")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
path := fmt.Sprintf("%s/authelia.log", dir)
|
|
err = InitializeLogger("text", path)
|
|
require.NoError(t, err)
|
|
|
|
Logger().Info("This is a test")
|
|
|
|
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
|
require.NoError(t, err)
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
require.NoError(t, err)
|
|
|
|
assert.Contains(t, string(b), "level=info msg=\"This is a test\"\n")
|
|
}
|
|
|
|
func TestShouldFormatLogsAsJSON(t *testing.T) {
|
|
dir, err := ioutil.TempDir("/tmp", "logs-dir")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
path := fmt.Sprintf("%s/authelia.log", dir)
|
|
err = InitializeLogger("json", path)
|
|
require.NoError(t, err)
|
|
|
|
Logger().Info("This is a test")
|
|
|
|
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
|
require.NoError(t, err)
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
require.NoError(t, err)
|
|
|
|
assert.Contains(t, string(b), "{\"level\":\"info\",\"msg\":\"This is a test\",")
|
|
}
|