Skip to content
Open
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
8 changes: 8 additions & 0 deletions cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Option func(*Command)
// WithLogger overrides the default logger.
func WithLogger(l cloudsql.Logger) Option {
return func(c *Command) {
c.customLogger = true
c.logger = l
}
}
Expand Down Expand Up @@ -83,6 +84,13 @@ func WithQuietLogging() Option {
}
}

// WithStructuredLogging configures the Proxy to log messages using JSON.
func WithStructuredLogging() Option {
return func(c *Command) {
c.conf.StructuredLogs = true
}
}

// WithDebugLogging configures the Proxy to log debug level messages.
func WithDebugLogging() Option {
return func(c *Command) {
Expand Down
33 changes: 33 additions & 0 deletions cmd/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ func TestCommandOptions(t *testing.T) {
},
option: WithQuietLogging(),
},
{
desc: "with structured logging",
isValid: func(c *Command) error {
if !c.conf.StructuredLogs {
return errors.New("structured logs was false, but should be true")
}
return nil
},
option: WithStructuredLogging(),
},
{
desc: "with lazy refresh",
isValid: func(c *Command) error {
Expand Down Expand Up @@ -165,6 +175,7 @@ func TestCommandOptions(t *testing.T) {
}

func TestCommandOptionsOverridesCLI(t *testing.T) {
logger := log.NewStdLogger(io.Discard, io.Discard)
tcs := []struct {
desc string
isValid func(*Command) error
Expand Down Expand Up @@ -193,6 +204,28 @@ func TestCommandOptionsOverridesCLI(t *testing.T) {
option: WithQuietLogging(),
args: []string{"--quiet", "false"},
},
{
desc: "with logger overriding structured logs flag",
isValid: func(c *Command) error {
if c.logger != logger {
return errors.New("custom logger was overridden by structured logs flag")
}
return nil
},
option: WithLogger(logger),
args: []string{"--structured-logs"},
},
{
desc: "with logger overriding quiet flag",
isValid: func(c *Command) error {
if c.logger != logger {
return errors.New("custom logger was overridden by quiet flag")
}
return nil
},
option: WithLogger(logger),
args: []string{"--quiet"},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
Expand Down
13 changes: 8 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Command struct {
*cobra.Command
conf *proxy.Config
logger cloudsql.Logger
customLogger bool
dialer cloudsql.Dialer
cleanup func() error
connRefuseNotify func()
Expand Down Expand Up @@ -663,11 +664,13 @@ func loadConfig(c *Command, args []string, opts []Option) error {
o(c)
}

// Handle logger separately from config
if c.conf.StructuredLogs {
c.logger = log.NewStructuredLogger(c.conf.Quiet)
} else if c.conf.Quiet {
c.logger = log.NewStdLogger(io.Discard, os.Stderr)
// Handle logger separately from config if not overridden by WithLogger
if !c.customLogger {
if c.conf.StructuredLogs {
c.logger = 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
Loading