Skip to content

Commit

Permalink
Add support for json logging (#50)
Browse files Browse the repository at this point in the history
* Add support for json logging

* adjust default logging format to txt

* add unit test for log_format env

* correct code indents for command.go

* Update flags.go
  • Loading branch information
lalbers authored Feb 20, 2020
1 parent 98298fe commit 98d54a8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions deploy/injector-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ spec:
value: ":8080"
- name: AGENT_INJECT_LOG_LEVEL
value: "info"
- name: AGENT_INJECT_LOG_FORMAT
value: "standard"
- name: AGENT_INJECT_VAULT_ADDR
value: "https://vault.$(NAMESPACE).svc:8200"
- name: AGENT_INJECT_VAULT_IMAGE
Expand Down
7 changes: 5 additions & 2 deletions subcommand/injector/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Command struct {

flagListen string // Address of Vault Server
flagLogLevel string // Log verbosity
flagLogFormat string // Log format
flagCertFile string // TLS Certificate to serve
flagKeyFile string // TLS private key to serve
flagAutoName string // MutatingWebhookConfiguration for updating
Expand Down Expand Up @@ -102,8 +103,10 @@ func (c *Command) Run(args []string) int {
return 1
}

logger := hclog.Default().Named("handler")
logger.SetLevel(level)
logger := hclog.New(&hclog.LoggerOptions{
Name: "handler",
Level: level,
JSONFormat: (c.flagLogFormat == "json")})

// Build the HTTP handler and server
injector := agentInject.Handler{
Expand Down
14 changes: 11 additions & 3 deletions subcommand/injector/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

const (
DefaultLogLevel = "info"
DefaultLogLevel = "info"
DefaultLogFormat = "standard"
)

// Specification are the supported environment variables, prefixed with
Expand All @@ -25,6 +26,9 @@ type Specification struct {
// LogLevel is the AGENT_INJECT_LOG_LEVEL environment variable.
LogLevel string `split_words:"true"`

// LogFormat is the AGENT_INJECT_LOG_FORMAT environment variable
LogFormat string `split_words:"true"`

// TLSAuto is the AGENT_INJECT_TLS_AUTO environment variable.
TLSAuto string `envconfig:"tls_auto"`

Expand All @@ -45,14 +49,15 @@ type Specification struct {

// VaultAuthPath is the AGENT_INJECT_VAULT_AUTH_PATH environment variable.
VaultAuthPath string `split_words:"true"`

}

func (c *Command) init() {
c.flagSet = flag.NewFlagSet("", flag.ContinueOnError)
c.flagSet.StringVar(&c.flagListen, "listen", ":8080", "Address to bind listener to.")
c.flagSet.StringVar(&c.flagLogLevel, "log-level", DefaultLogLevel, "Log verbosity level. Supported values "+
`(in order of detail) are "trace", "debug", "info", "warn", and "err".`)
c.flagSet.StringVar(&c.flagLogFormat, "log-format", DefaultLogFormat, "Log output format. "+
`Supported log formats: "standard", "json".`)
c.flagSet.StringVar(&c.flagAutoName, "tls-auto", "",
"MutatingWebhookConfiguration name. If specified, will auto generate cert bundle.")
c.flagSet.StringVar(&c.flagAutoHosts, "tls-auto-hosts", "",
Expand Down Expand Up @@ -89,7 +94,6 @@ func (c *Command) logLevel() (hclog.Level, error) {
default:
return level, fmt.Errorf("unknown log level: %s", c.flagLogLevel)
}

return level, nil
}

Expand All @@ -109,6 +113,10 @@ func (c *Command) parseEnvs() error {
c.flagLogLevel = envs.LogLevel
}

if envs.LogFormat != "" {
c.flagLogFormat = envs.LogFormat
}

if envs.TLSAuto != "" {
c.flagAutoName = envs.TLSAuto
}
Expand Down
1 change: 1 addition & 0 deletions subcommand/injector/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func TestCommandEnvs(t *testing.T) {
{env: "AGENT_INJECT_TLS_AUTO_HOSTS", value: "foobar.com", cmdPtr: &cmd.flagAutoHosts},
{env: "AGENT_INJECT_TLS_AUTO", value: "mutationWebhook", cmdPtr: &cmd.flagAutoName},
{env: "AGENT_INJECT_LOG_LEVEL", value: "info", cmdPtr: &cmd.flagLogLevel},
{env: "AGENT_INJECT_LOG_FORMAT", value: "standard", cmdPtr: &cmd.flagLogFormat},
}

for _, tt := range tests {
Expand Down

0 comments on commit 98d54a8

Please sign in to comment.