From 657c430e0b00ff6c9826110f02471482b307d22a Mon Sep 17 00:00:00 2001 From: hc-github-team-nomad-core <82989552+hc-github-team-nomad-core@users.noreply.github.com> Date: Mon, 16 Oct 2023 10:19:58 -0500 Subject: [PATCH] backport of commit 1ffdd576bbcea1f32aa179934b63450808c022c4 (#18772) Co-authored-by: James Rasell --- .changelog/18768.txt | 3 +++ command/agent/command.go | 13 ++++++++---- command/agent/config.go | 8 ++++++++ command/agent/config_parse_test.go | 21 ++++++++++---------- command/agent/config_test.go | 2 ++ command/agent/testdata/basic.hcl | 2 ++ command/agent/testdata/basic.json | 1 + website/content/docs/commands/agent.mdx | 3 +++ website/content/docs/configuration/index.mdx | 3 +++ 9 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 .changelog/18768.txt diff --git a/.changelog/18768.txt b/.changelog/18768.txt new file mode 100644 index 000000000..736b3f617 --- /dev/null +++ b/.changelog/18768.txt @@ -0,0 +1,3 @@ +```release-note:improvement +agent: Added config option to enable file and line log detail +``` diff --git a/command/agent/command.go b/command/agent/command.go index c28b01430..d33dc21e8 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -121,6 +121,7 @@ func (c *Command) readConfig() *Config { flags.StringVar(&cmdConfig.Datacenter, "dc", "", "") flags.StringVar(&cmdConfig.LogLevel, "log-level", "", "") flags.BoolVar(&cmdConfig.LogJson, "log-json", false, "") + flags.BoolVar(&cmdConfig.LogIncludeLocation, "log-include-location", false, "") flags.StringVar(&cmdConfig.NodeName, "node", "", "") // Consul options @@ -734,10 +735,11 @@ func (c *Command) Run(args []string) int { // Create logger logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{ - Name: "agent", - Level: hclog.LevelFromString(config.LogLevel), - Output: logOutput, - JSONFormat: config.LogJson, + Name: "agent", + Level: hclog.LevelFromString(config.LogLevel), + Output: logOutput, + JSONFormat: config.LogJson, + IncludeLocation: config.LogIncludeLocation, }) // Wrap log messages emitted with the 'log' package. @@ -1346,6 +1348,9 @@ General Options (clients and servers): -log-json Output logs in a JSON format. The default is false. + -log-include-location + Include file and line information in each log line. The default is false. + -node= The name of the local agent. This name is used to identify the node in the cluster. The name must be unique per region. The default is diff --git a/command/agent/config.go b/command/agent/config.go index 8772d952e..0c6dfd684 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -68,6 +68,11 @@ type Config struct { // LogFile enables logging to a file LogFile string `hcl:"log_file"` + // LogIncludeLocation dictates whether the logger includes file and line + // information on each log line. This is useful for Nomad development and + // debugging. + LogIncludeLocation bool `hcl:"log_include_location"` + // LogRotateDuration is the time period that logs should be rotated in LogRotateDuration string `hcl:"log_rotate_duration"` @@ -1410,6 +1415,9 @@ func (c *Config) Merge(b *Config) *Config { if b.LogFile != "" { result.LogFile = b.LogFile } + if b.LogIncludeLocation { + result.LogIncludeLocation = true + } if b.LogRotateDuration != "" { result.LogRotateDuration = b.LogRotateDuration } diff --git a/command/agent/config_parse_test.go b/command/agent/config_parse_test.go index d023a90c0..a16e4b50c 100644 --- a/command/agent/config_parse_test.go +++ b/command/agent/config_parse_test.go @@ -18,16 +18,17 @@ import ( ) var basicConfig = &Config{ - Region: "foobar", - Datacenter: "dc2", - NodeName: "my-web", - DataDir: "/tmp/nomad", - PluginDir: "/tmp/nomad-plugins", - LogFile: "/var/log/nomad.log", - LogLevel: "ERR", - LogJson: true, - BindAddr: "192.168.0.1", - EnableDebug: true, + Region: "foobar", + Datacenter: "dc2", + NodeName: "my-web", + DataDir: "/tmp/nomad", + PluginDir: "/tmp/nomad-plugins", + LogFile: "/var/log/nomad.log", + LogLevel: "ERR", + LogIncludeLocation: true, + LogJson: true, + BindAddr: "192.168.0.1", + EnableDebug: true, Ports: &Ports{ HTTP: 1234, RPC: 2345, diff --git a/command/agent/config_test.go b/command/agent/config_test.go index b998b347f..dbea08c70 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -57,6 +57,7 @@ func TestConfig_Merge(t *testing.T) { DataDir: "/tmp/dir1", PluginDir: "/tmp/pluginDir1", LogLevel: "INFO", + LogIncludeLocation: false, LogJson: false, EnableDebug: false, LeaveOnInt: false, @@ -244,6 +245,7 @@ func TestConfig_Merge(t *testing.T) { DataDir: "/tmp/dir2", PluginDir: "/tmp/pluginDir2", LogLevel: "DEBUG", + LogIncludeLocation: true, LogJson: true, EnableDebug: true, LeaveOnInt: true, diff --git a/command/agent/testdata/basic.hcl b/command/agent/testdata/basic.hcl index c3637c680..d159f1cb2 100644 --- a/command/agent/testdata/basic.hcl +++ b/command/agent/testdata/basic.hcl @@ -18,6 +18,8 @@ log_json = true log_file = "/var/log/nomad.log" +log_include_location = true + bind_addr = "192.168.0.1" enable_debug = true diff --git a/command/agent/testdata/basic.json b/command/agent/testdata/basic.json index 653c1b4d5..04606a2f7 100644 --- a/command/agent/testdata/basic.json +++ b/command/agent/testdata/basic.json @@ -181,6 +181,7 @@ "leave_on_interrupt": true, "leave_on_terminate": true, "log_file": "/var/log/nomad.log", + "log_include_location": true, "log_json": true, "log_level": "ERR", "name": "my-web", diff --git a/website/content/docs/commands/agent.mdx b/website/content/docs/commands/agent.mdx index aa971d132..02c114042 100644 --- a/website/content/docs/commands/agent.mdx +++ b/website/content/docs/commands/agent.mdx @@ -109,6 +109,8 @@ via CLI arguments. The `agent` command accepts the following arguments: - `-log-level=`: Equivalent to the [log_level] config option. +- `-log-include-location`: Equivalent to the [log_include_location] config option. + - `-log-json`: Equivalent to the [log_json] config option. - `-meta=`: Equivalent to the Client [meta] config option. @@ -202,6 +204,7 @@ via CLI arguments. The `agent` command accepts the following arguments: [enabled]: /nomad/docs/configuration/acl#enabled [encryption overview]: /nomad/tutorials/transport-security/security-gossip-encryption [key_file]: /nomad/docs/configuration/consul#key_file +[log_include_location]: /nomad/docs/configuration#log_include_location [log_json]: /nomad/docs/configuration#log_json [log_level]: /nomad/docs/configuration#log_level [meta]: /nomad/docs/configuration/client#meta diff --git a/website/content/docs/configuration/index.mdx b/website/content/docs/configuration/index.mdx index 6a2cd0eae..58e94bc91 100644 --- a/website/content/docs/configuration/index.mdx +++ b/website/content/docs/configuration/index.mdx @@ -248,6 +248,9 @@ testing. agent will output. Valid log levels include `WARN`, `INFO`, or `DEBUG` in increasing order of verbosity. +- `log_include_location` `(bool: false)` - Include file and line information in + each log line. + - `log_json` `(bool: false)` - Output logs in a JSON format. - `log_file` `(string: "")` - Specifies the path for logging. If the path