Enable json logs

This commit is contained in:
Alex Dadgar 2019-01-11 11:36:26 -08:00
parent 6f64cfab7e
commit 5621086f50
9 changed files with 23 additions and 4 deletions

View File

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

View File

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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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