Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow --structured-logs and --quiet flags together #1750

Merged
merged 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,8 @@ func NewCommand(opts ...Option) *Command {
}
// Handle logger separately from config
if c.conf.StructuredLogs {
c.logger, c.cleanup = log.NewStructuredLogger()
}
if c.conf.Quiet {
c.logger, c.cleanup = log.NewStructuredLogger(c.conf.Quiet)
} else if c.conf.Quiet {
c.logger = log.NewStdLogger(io.Discard, os.Stderr)
}
err := parseConfig(c, c.conf, args)
Expand Down Expand Up @@ -845,9 +844,9 @@ func runSignalWrapper(cmd *Command) (err error) {
err = <-shutdownCh
switch {
case errors.Is(err, errSigInt):
cmd.logger.Errorf("SIGINT signal received. Shutting down...")
cmd.logger.Infof("SIGINT signal received. Shutting down...")
case errors.Is(err, errSigTerm):
cmd.logger.Errorf("SIGTERM signal received. Shutting down...")
cmd.logger.Infof("SIGTERM signal received. Shutting down...")
default:
cmd.logger.Errorf("The proxy has encountered a terminal error: %v", err)
}
Expand Down
12 changes: 10 additions & 2 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (l *StructuredLogger) Debugf(format string, v ...interface{}) {
}

// NewStructuredLogger creates a Logger that logs messages using JSON.
func NewStructuredLogger() (cloudsql.Logger, func() error) {
func NewStructuredLogger(quiet bool) (cloudsql.Logger, func() error) {
// Configure structured logs to adhere to LogEntry format
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
c := zap.NewProductionEncoderConfig()
Expand All @@ -87,8 +87,16 @@ func NewStructuredLogger() (cloudsql.Logger, func() error) {
c.EncodeTime = zapcore.ISO8601TimeEncoder

enc := zapcore.NewJSONEncoder(c)

var syncer zapcore.WriteSyncer
// quiet disables writing to the info log
if quiet {
syncer = zapcore.AddSync(io.Discard)
} else {
syncer = zapcore.Lock(os.Stdout)
}
core := zapcore.NewTee(
zapcore.NewCore(enc, zapcore.Lock(os.Stdout), zap.LevelEnablerFunc(func(l zapcore.Level) bool {
zapcore.NewCore(enc, syncer, zap.LevelEnablerFunc(func(l zapcore.Level) bool {
// Anything below error, goes to the info log.
return l < zapcore.ErrorLevel
})),
Expand Down