Skip to content

Commit

Permalink
agent: add config option to enable file and line log detail. (#18768)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell authored Oct 16, 2023
1 parent fe0a06e commit 1ffdd57
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .changelog/18768.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
agent: Added config option to enable file and line log detail
```
13 changes: 9 additions & 4 deletions command/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,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
Expand Down Expand Up @@ -748,10 +749,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.
Expand Down Expand Up @@ -1360,6 +1362,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=<name>
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
Expand Down
8 changes: 8 additions & 0 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -1434,6 +1439,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
}
Expand Down
21 changes: 11 additions & 10 deletions command/agent/config_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -280,6 +281,7 @@ func TestConfig_Merge(t *testing.T) {
DataDir: "/tmp/dir2",
PluginDir: "/tmp/pluginDir2",
LogLevel: "DEBUG",
LogIncludeLocation: true,
LogJson: true,
EnableDebug: true,
LeaveOnInt: true,
Expand Down
2 changes: 2 additions & 0 deletions command/agent/testdata/basic.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions command/agent/testdata/basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,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",
Expand Down
3 changes: 3 additions & 0 deletions website/content/docs/commands/agent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ via CLI arguments. The `agent` command accepts the following arguments:

- `-log-level=<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=<key=value>`: Equivalent to the Client [meta] config option.
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions website/content/docs/configuration/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1ffdd57

Please sign in to comment.