Skip to content

Commit

Permalink
Enable json logs
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Jan 11, 2019
1 parent 16e8cdf commit f171a72
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
5 changes: 5 additions & 0 deletions command/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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=<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
1 change: 1 addition & 0 deletions command/agent/config-test-fixtures/basic.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion command/agent/config_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions command/agent/config_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
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 @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion website/source/docs/commands/agent.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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>`: Address of another agent to join upon starting up. This can
be specified multiple times to specify multiple agents to join.
* `-log-level=<level>`: Equivalent to the [log_level](#log_level) config option.
* `-log-level=<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=<key=value>`: Equivalent to the Client [meta](#meta) config option.
* `-network-interface=<interface>`: Equivalent to the Client
[network_interface](#network_interface) config option.
Expand Down
2 changes: 2 additions & 0 deletions website/source/docs/configuration/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f171a72

Please sign in to comment.