0d4ae949a8
* 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
82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
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
|
|
}
|
|
}
|