backport of commit 1ffdd576bbcea1f32aa179934b63450808c022c4 (#18772)

Co-authored-by: James Rasell <jrasell@users.noreply.github.com>
This commit is contained in:
hc-github-team-nomad-core 2023-10-16 10:19:58 -05:00 committed by GitHub
parent fe612a4d18
commit 657c430e0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 42 additions and 14 deletions

3
.changelog/18768.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
agent: Added config option to enable file and line log detail
```

View File

@ -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=<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

View File

@ -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
}

View File

@ -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,

View File

@ -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,

View File

@ -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

View File

@ -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",

View File

@ -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.
@ -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

View File

@ -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