From f171a723cb0ed2609a470bbad9feddcb63abf2c7 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Fri, 11 Jan 2019 11:36:26 -0800 Subject: [PATCH] Enable json logs --- command/agent/agent.go | 2 +- command/agent/command.go | 5 +++++ command/agent/config-test-fixtures/basic.hcl | 1 + command/agent/config.go | 8 +++++++- command/agent/config_parse.go | 3 ++- command/agent/config_parse_test.go | 1 + command/agent/config_test.go | 2 ++ website/source/docs/commands/agent.html.md.erb | 3 ++- website/source/docs/configuration/index.html.md | 2 ++ 9 files changed, 23 insertions(+), 4 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index a936282ad13..d64e26fe31e 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -99,7 +99,7 @@ func NewAgent(config *Config, logOutput io.Writer, inmem *metrics.InmemSink) (*A Name: "agent", Level: log.LevelFromString(config.LogLevel), Output: logOutput, - JSONFormat: false, // TODO(alex,hclog) Add a config option + JSONFormat: config.LogJson, }) a.httpLogger = a.logger.ResetNamed("http") diff --git a/command/agent/command.go b/command/agent/command.go index c30f46caf8c..39998a7eaf5 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -109,6 +109,7 @@ func (c *Command) readConfig() *Config { flags.StringVar(&cmdConfig.PluginDir, "plugin-dir", "", "") flags.StringVar(&cmdConfig.Datacenter, "dc", "", "") flags.StringVar(&cmdConfig.LogLevel, "log-level", "", "") + flags.BoolVar(&cmdConfig.LogJson, "log-json", false, "") flags.StringVar(&cmdConfig.NodeName, "node", "", "") // Consul options @@ -494,6 +495,7 @@ func (c *Command) AutocompleteFlags() complete.Flags { "-plugin-dir": complete.PredictDirs("*"), "-dc": complete.PredictAnything, "-log-level": complete.PredictAnything, + "-json-logs": complete.PredictNothing, "-node": complete.PredictAnything, "-consul-auth": complete.PredictAnything, "-consul-auto-advertise": complete.PredictNothing, @@ -1127,6 +1129,9 @@ General Options (clients and servers): DEBUG, INFO, and WARN, in decreasing order of verbosity. The default is INFO. + -log-json + Output logs in a JSON format. 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-test-fixtures/basic.hcl b/command/agent/config-test-fixtures/basic.hcl index 8faba064101..96bf9d9c621 100644 --- a/command/agent/config-test-fixtures/basic.hcl +++ b/command/agent/config-test-fixtures/basic.hcl @@ -4,6 +4,7 @@ name = "my-web" data_dir = "/tmp/nomad" plugin_dir = "/tmp/nomad-plugins" log_level = "ERR" +log_json = true bind_addr = "192.168.0.1" enable_debug = true ports { diff --git a/command/agent/config.go b/command/agent/config.go index 54a7c3049b5..d730bb4c582 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -40,9 +40,12 @@ type Config struct { // PluginDir is the directory to lookup plugins. PluginDir string `mapstructure:"plugin_dir"` - // LogLevel is the level of the logs to putout + // LogLevel is the level of the logs to put out LogLevel string `mapstructure:"log_level"` + // LogJson enables log output in a JSON format + LogJson bool `mapstructure:"log_json"` + // 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 `mapstructure:"bind_addr"` @@ -722,6 +725,9 @@ func (c *Config) Merge(b *Config) *Config { if b.LogLevel != "" { result.LogLevel = b.LogLevel } + if b.LogJson { + result.LogJson = true + } if b.BindAddr != "" { result.BindAddr = b.BindAddr } diff --git a/command/agent/config_parse.go b/command/agent/config_parse.go index 641a10c2cdc..6df9e83d437 100644 --- a/command/agent/config_parse.go +++ b/command/agent/config_parse.go @@ -9,7 +9,7 @@ import ( "time" multierror "github.com/hashicorp/go-multierror" - "github.com/hashicorp/go-version" + version "github.com/hashicorp/go-version" "github.com/hashicorp/hcl" "github.com/hashicorp/hcl/hcl/ast" "github.com/hashicorp/nomad/helper" @@ -80,6 +80,7 @@ func parseConfig(result *Config, list *ast.ObjectList) error { "data_dir", "plugin_dir", "log_level", + "log_json", "bind_addr", "enable_debug", "ports", diff --git a/command/agent/config_parse_test.go b/command/agent/config_parse_test.go index 649339d6203..3ead69cf1ff 100644 --- a/command/agent/config_parse_test.go +++ b/command/agent/config_parse_test.go @@ -26,6 +26,7 @@ func TestConfig_Parse(t *testing.T) { DataDir: "/tmp/nomad", PluginDir: "/tmp/nomad-plugins", LogLevel: "ERR", + LogJson: true, BindAddr: "192.168.0.1", EnableDebug: true, Ports: &Ports{ diff --git a/command/agent/config_test.go b/command/agent/config_test.go index 4c0d97f6e4e..7d7128d5f3d 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -47,6 +47,7 @@ func TestConfig_Merge(t *testing.T) { DataDir: "/tmp/dir1", PluginDir: "/tmp/pluginDir1", LogLevel: "INFO", + LogJson: false, EnableDebug: false, LeaveOnInt: false, LeaveOnTerm: false, @@ -193,6 +194,7 @@ func TestConfig_Merge(t *testing.T) { DataDir: "/tmp/dir2", PluginDir: "/tmp/pluginDir2", LogLevel: "DEBUG", + LogJson: true, EnableDebug: true, LeaveOnInt: true, LeaveOnTerm: true, diff --git a/website/source/docs/commands/agent.html.md.erb b/website/source/docs/commands/agent.html.md.erb index bdff3929918..474ec7f1490 100644 --- a/website/source/docs/commands/agent.html.md.erb +++ b/website/source/docs/commands/agent.html.md.erb @@ -59,7 +59,8 @@ via CLI arguments. The `agent` command accepts the following arguments: * `-encrypt`: Set the Serf encryption key. See the [Encryption Overview](/guides/security/encryption.html) for more details. * `-join=
`: Address of another agent to join upon starting up. This can be specified multiple times to specify multiple agents to join. -* `-log-level=`: Equivalent to the [log_level](#log_level) config option. +* `-log-level=`: Equivalent to the [log_level](/docs/configuration/index.html#log_level) config option. +* `-log-json`: Equivalent to the [log_json](/docs/configuration/index.html#log_json) config option. * `-meta=`: Equivalent to the Client [meta](#meta) config option. * `-network-interface=`: Equivalent to the Client [network_interface](#network_interface) config option. diff --git a/website/source/docs/configuration/index.html.md b/website/source/docs/configuration/index.html.md index 73ef36703f9..e70a92939ea 100644 --- a/website/source/docs/configuration/index.html.md +++ b/website/source/docs/configuration/index.html.md @@ -172,6 +172,8 @@ testing. agent will output. Valid log levels include `WARN`, `INFO`, or `DEBUG` in increasing order of verbosity. +- `log_json` `(bool: false)` - Output logs in a JSON format. + - `name` `(string: [hostname])` - Specifies the name of the local node. This value is used to identify individual agents. When specified on a server, the name must be unique within the region.