-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'log-format' CLI flag, along with associated config flag, for 'va…
…ult server' command. (#6840) * Read config before creating logger when booting vault server * Allow for specifying log output in JSON format in a config file, via a 'log_level' flag * Create parser for log format flag * Allow for specifying log format in a config file, via a 'log_format' flag. Also, get rid of 'log_json' flag. * Add 'log-format' command line flag * Update documentation to include description of log_format setting * Tweak comment for VAULT_LOG_FORMAT environment variable * add test for ParseEnvLogFormat() * clarify how log format is set * fix typos in documentation
- Loading branch information
Showing
7 changed files
with
269 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package logging | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
log "github.com/hashicorp/go-hclog" | ||
) | ||
|
||
type LogFormat int | ||
|
||
const ( | ||
UnspecifiedFormat LogFormat = iota | ||
StandardFormat | ||
JSONFormat | ||
) | ||
|
||
// Stringer implementation | ||
func (l LogFormat) String() string { | ||
switch l { | ||
case UnspecifiedFormat: | ||
return "unspecified" | ||
case StandardFormat: | ||
return "standard" | ||
case JSONFormat: | ||
return "json" | ||
} | ||
|
||
// unreachable | ||
return "unknown" | ||
} | ||
|
||
// NewVaultLogger creates a new logger with the specified level and a Vault | ||
// formatter | ||
func NewVaultLogger(level log.Level) log.Logger { | ||
return NewVaultLoggerWithWriter(log.DefaultOutput, level) | ||
} | ||
|
||
// NewVaultLoggerWithWriter creates a new logger with the specified level and | ||
// writer and a Vault formatter | ||
func NewVaultLoggerWithWriter(w io.Writer, level log.Level) log.Logger { | ||
opts := &log.LoggerOptions{ | ||
Level: level, | ||
Output: w, | ||
JSONFormat: ParseEnvLogFormat() == JSONFormat, | ||
} | ||
return log.New(opts) | ||
} | ||
|
||
// ParseLogFormat parses the log format from the provided string. | ||
func ParseLogFormat(format string) (LogFormat, error) { | ||
|
||
switch strings.ToLower(strings.TrimSpace(format)) { | ||
case "": | ||
return UnspecifiedFormat, nil | ||
case "standard": | ||
return StandardFormat, nil | ||
case "json": | ||
return JSONFormat, nil | ||
default: | ||
return UnspecifiedFormat, fmt.Errorf("Unknown log format: %s", format) | ||
} | ||
} | ||
|
||
// ParseEnvLogFormat parses the log format from an environment variable. | ||
func ParseEnvLogFormat() LogFormat { | ||
logFormat := os.Getenv("VAULT_LOG_FORMAT") | ||
if logFormat == "" { | ||
logFormat = os.Getenv("LOGXI_FORMAT") | ||
} | ||
switch strings.ToLower(logFormat) { | ||
case "json", "vault_json", "vault-json", "vaultjson": | ||
return JSONFormat | ||
case "standard": | ||
return StandardFormat | ||
default: | ||
return UnspecifiedFormat | ||
} | ||
} |
Oops, something went wrong.