diff --git a/command/agent/config.go b/command/agent/config.go index 0767b98f9..39259abb2 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -57,6 +57,18 @@ type Config struct { // LogJson enables log output in a JSON format LogJson bool `hcl:"log_json"` + // LogFile enables logging to a file + LogFile string `hcl:"log_file"` + + // LogRotateDuration is the time period that logs should be rotated in + LogRotateDuration string `hcl:"log_rotate_duration"` + + // LogRotateBytes is the max number of bytes that should be written to a file + LogRotateBytes int `hcl:"log_rotate_bytes"` + + // LogRotateMaxFiles is the max number of log files to keep + LogRotateMaxFiles int `hcl:"log_rotate_max_files"` + // BindAddr is the address on which all of nomad's services will // be bound. If not specified, this defaults to 127.0.0.1. BindAddr string `hcl:"bind_addr"` @@ -898,6 +910,18 @@ func (c *Config) Merge(b *Config) *Config { if b.LogJson { result.LogJson = true } + if b.LogFile != "" { + result.LogFile = b.LogFile + } + if b.LogRotateDuration != "" { + result.LogRotateDuration = b.LogRotateDuration + } + if b.LogRotateBytes != 0 { + result.LogRotateBytes = b.LogRotateBytes + } + if b.LogRotateMaxFiles != 0 { + result.LogRotateMaxFiles = b.LogRotateMaxFiles + } if b.BindAddr != "" { result.BindAddr = b.BindAddr } diff --git a/command/agent/config_parse_test.go b/command/agent/config_parse_test.go index 362bddb92..51368fd5d 100644 --- a/command/agent/config_parse_test.go +++ b/command/agent/config_parse_test.go @@ -19,6 +19,7 @@ var basicConfig = &Config{ NodeName: "my-web", DataDir: "/tmp/nomad", PluginDir: "/tmp/nomad-plugins", + LogFile: "/var/log/nomad.log", LogLevel: "ERR", LogJson: true, BindAddr: "192.168.0.1", @@ -409,14 +410,10 @@ func TestConfig_Parse(t *testing.T) { t.Run(tc.File, func(t *testing.T) { require := require.New(t) path, err := filepath.Abs(filepath.Join("./testdata", tc.File)) - if err != nil { - t.Fatalf("file: %s\n\n%s", tc.File, err) - } + require.NoError(err) actual, err := ParseConfigFile(path) - if (err != nil) != tc.Err { - t.Fatalf("file: %s\n\n%s", tc.File, err) - } + require.NoError(err) // ParseConfig used to re-merge defaults for these three objects, // despite them already being merged in LoadConfig. The test structs diff --git a/command/agent/testdata/basic.hcl b/command/agent/testdata/basic.hcl index 03ce1c749..0f569b487 100644 --- a/command/agent/testdata/basic.hcl +++ b/command/agent/testdata/basic.hcl @@ -13,6 +13,8 @@ log_level = "ERR" log_json = true +log_file = "/var/log/nomad.log" + bind_addr = "192.168.0.1" enable_debug = true diff --git a/command/agent/testdata/basic.json b/command/agent/testdata/basic.json index 6eedcb85f..22bb3a36c 100644 --- a/command/agent/testdata/basic.json +++ b/command/agent/testdata/basic.json @@ -143,6 +143,7 @@ ], "leave_on_interrupt": true, "leave_on_terminate": true, + "log_file": "/var/log/nomad.log", "log_json": true, "log_level": "ERR", "name": "my-web",