0d4ae949a8
* Read config before creating logger when booting vault server * Allow for specifying log output in JSON format in a config file, via a 'log_level' flag * Create parser for log format flag * Allow for specifying log format in a config file, via a 'log_format' flag. Also, get rid of 'log_json' flag. * Add 'log-format' command line flag * Update documentation to include description of log_format setting * Tweak comment for VAULT_LOG_FORMAT environment variable * add test for ParseEnvLogFormat() * clarify how log format is set * fix typos in documentation
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package logging
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func Test_ParseLogFormat(t *testing.T) {
|
|
|
|
type testData struct {
|
|
format string
|
|
expected LogFormat
|
|
expectedErr error
|
|
}
|
|
|
|
tests := []testData{
|
|
{format: "", expected: UnspecifiedFormat, expectedErr: nil},
|
|
{format: " ", expected: UnspecifiedFormat, expectedErr: nil},
|
|
{format: "standard", expected: StandardFormat, expectedErr: nil},
|
|
{format: "STANDARD", expected: StandardFormat, expectedErr: nil},
|
|
{format: "json", expected: JSONFormat, expectedErr: nil},
|
|
{format: " json ", expected: JSONFormat, expectedErr: nil},
|
|
{format: "bogus", expected: UnspecifiedFormat, expectedErr: errors.New("Unknown log format: bogus")},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
result, err := ParseLogFormat(test.format)
|
|
if test.expected != result {
|
|
t.Errorf("expected %s, got %s", test.expected, result)
|
|
}
|
|
if !reflect.DeepEqual(test.expectedErr, err) {
|
|
t.Errorf("expected error %v, got %v", test.expectedErr, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func Test_ParseEnv_VAULT_LOG_FORMAT(t *testing.T) {
|
|
|
|
oldVLF := os.Getenv("VAULT_LOG_FORMAT")
|
|
defer os.Setenv("VAULT_LOG_FORMAT", oldVLF)
|
|
|
|
testParseEnvLogFormat(t, "VAULT_LOG_FORMAT")
|
|
}
|
|
|
|
func Test_ParseEnv_LOGXI_FORMAT(t *testing.T) {
|
|
|
|
oldVLF := os.Getenv("VAULT_LOG_FORMAT")
|
|
defer os.Setenv("VAULT_LOG_FORMAT", oldVLF)
|
|
|
|
oldLogxi := os.Getenv("LOGXI_FORMAT")
|
|
defer os.Setenv("LOGXI_FORMAT", oldLogxi)
|
|
|
|
os.Setenv("VAULT_LOG_FORMAT", "")
|
|
testParseEnvLogFormat(t, "LOGXI_FORMAT")
|
|
}
|
|
|
|
func testParseEnvLogFormat(t *testing.T, name string) {
|
|
|
|
env := []string{
|
|
"json", "vauLT_Json", "VAULT-JSON", "vaulTJSon",
|
|
"standard", "STANDARD",
|
|
"bogus"}
|
|
|
|
formats := []LogFormat{
|
|
JSONFormat, JSONFormat, JSONFormat, JSONFormat,
|
|
StandardFormat, StandardFormat,
|
|
UnspecifiedFormat}
|
|
|
|
for i, e := range env {
|
|
os.Setenv(name, e)
|
|
if lf := ParseEnvLogFormat(); formats[i] != lf {
|
|
t.Errorf("expected %s, got %s", formats[i], lf)
|
|
}
|
|
}
|
|
}
|