From 051fe607182a16a31eb2798c17497ab3bdaf1ea2 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Wed, 14 Jul 2021 16:48:33 -0700 Subject: [PATCH] Duplicate logging instead of using from shared secure libs, as it's going out of there and we'll be moving away from hclog anyways --- go.mod | 1 - go.sum | 2 - internal/cmd/base/logging.go | 2 +- internal/cmd/base/logging/logging.go | 76 ++++++++++++++++++++++++++++ internal/cmd/base/servers.go | 2 +- 5 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 internal/cmd/base/logging/logging.go diff --git a/go.mod b/go.mod index e671d220a..c27f73804 100644 --- a/go.mod +++ b/go.mod @@ -34,7 +34,6 @@ require ( github.com/hashicorp/hcl v1.0.0 github.com/hashicorp/shared-secure-libs v0.0.7 github.com/hashicorp/shared-secure-libs/base62 v0.1.0 - github.com/hashicorp/shared-secure-libs/logging v0.1.0 github.com/hashicorp/shared-secure-libs/mlock v0.1.0 github.com/hashicorp/shared-secure-libs/parseutil v0.1.0 github.com/hashicorp/shared-secure-libs/password v0.1.0 diff --git a/go.sum b/go.sum index b9ecfab9e..52196c6a5 100644 --- a/go.sum +++ b/go.sum @@ -524,8 +524,6 @@ github.com/hashicorp/shared-secure-libs v0.0.7 h1:vdoIg+ahLPlwTELM7eA4OWws54Hf0e github.com/hashicorp/shared-secure-libs v0.0.7/go.mod h1:PH51kNtHA6kYV5Z8E3TOJM634iyp7IIxAMfLh0Dp35o= github.com/hashicorp/shared-secure-libs/base62 v0.1.0 h1:MZpHgL9yms0DVhYJ+cVpQ5tp8IbyoVufyMvCi6stJnU= github.com/hashicorp/shared-secure-libs/base62 v0.1.0/go.mod h1:yQ1EUBxJZ654Ivhjmr1pq2wt6YCoeTCuZgX2Ysc4IMg= -github.com/hashicorp/shared-secure-libs/logging v0.1.0 h1:a2FchfFIKU+jo0xWIjBGSA8n80af16CbvQAAcOlMejU= -github.com/hashicorp/shared-secure-libs/logging v0.1.0/go.mod h1:ogsWOAtBzEu0YYDWKgdahFkmjnu8cKzQ+ejg5Ibm8L8= github.com/hashicorp/shared-secure-libs/mlock v0.1.0 h1:7xw1ZVNW41PGRiRysdA8SBaRN2bNE+Bf+Uce/+JWsu4= github.com/hashicorp/shared-secure-libs/mlock v0.1.0/go.mod h1:lOpv9YZPgTDtfSdhetd25YVO3uxRrD8KD0erct1PdP0= github.com/hashicorp/shared-secure-libs/parseutil v0.1.0 h1:R5IyN4zkqbxkPLhR7WSvF7Jhoc7AtlvlVK8QZKs8N9c= diff --git a/internal/cmd/base/logging.go b/internal/cmd/base/logging.go index 5c987fe07..468f3fd8d 100644 --- a/internal/cmd/base/logging.go +++ b/internal/cmd/base/logging.go @@ -5,8 +5,8 @@ import ( "os" "strings" + "github.com/hashicorp/boundary/internal/cmd/base/logging" "github.com/hashicorp/go-hclog" - "github.com/hashicorp/shared-secure-libs/logging" ) func ProcessLogLevelAndFormat(flagLogLevel, flagLogFormat, configLogLevel, configLogFormat string) (hclog.Level, logging.LogFormat, error) { diff --git a/internal/cmd/base/logging/logging.go b/internal/cmd/base/logging/logging.go new file mode 100644 index 000000000..8ee0d9c64 --- /dev/null +++ b/internal/cmd/base/logging/logging.go @@ -0,0 +1,76 @@ +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" +} + +// NewLogger creates a new logger with the specified level and a formatter +func NewLogger(level log.Level) log.Logger { + return NewLoggerWithWriter(log.DefaultOutput, level) +} + +// NewLoggerWithWriter creates a new logger with the specified level and +// writer and a formatter +func NewLoggerWithWriter(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("BOUNDARY_LOG_FORMAT") + switch strings.ToLower(logFormat) { + case "json": + return JSONFormat + case "standard": + return StandardFormat + default: + return UnspecifiedFormat + } +} diff --git a/internal/cmd/base/servers.go b/internal/cmd/base/servers.go index 0560d8a57..bfe3c0f6d 100644 --- a/internal/cmd/base/servers.go +++ b/internal/cmd/base/servers.go @@ -21,6 +21,7 @@ import ( berrors "github.com/hashicorp/boundary/internal/errors" "github.com/hashicorp/boundary/globals" + "github.com/hashicorp/boundary/internal/cmd/base/logging" "github.com/hashicorp/boundary/internal/cmd/config" "github.com/hashicorp/boundary/internal/db" "github.com/hashicorp/boundary/internal/kms" @@ -33,7 +34,6 @@ import ( "github.com/hashicorp/go-multierror" "github.com/hashicorp/shared-secure-libs/configutil" "github.com/hashicorp/shared-secure-libs/gatedwriter" - "github.com/hashicorp/shared-secure-libs/logging" "github.com/hashicorp/shared-secure-libs/mlock" "github.com/hashicorp/shared-secure-libs/reloadutil" "github.com/hashicorp/shared-secure-libs/strutil"