ionos-cloud / sdk-go

Apache License 2.0
8 stars 1 forks source link

Allow configuring logger #24

Closed avorima closed 2 years ago

avorima commented 2 years ago

Current SDK Version

v6.0.2

Use-cases

The client supports logging debug information via go's standard logger. This logger should be configurable to ensure things like buffering work and the log output of an application stays consistent.

Attempted Solutions

One could write an adapter for stdlog that implements io.Writer and writes to the logger used in that application. This has 2 downsides IMO:

Proposal

Use an interface as logger and allow users to set it. The logger could be a package variable or a field in the client struct. The default would remain stdlog.

Here's a rough draft:

import "log"

type Logger interface {
        Printf(format string, args ...interface{})
}

var logger Logger = log.Default()

func SetLogger(l Logger) {
        logger = l
}

func (c *APIClient) SetLogger(l Logger) {
        c.logger = l
}

...

if c.cfg.Debug {
        c.logger.Printf("debug output")
}

Loggers that don't implement a Printf would still need an adapter, but at least the output would be uniform.

References

cristiGuranIonos commented 2 years ago

Good idea, we will add an interface for the logger.

cristiGuranIonos commented 2 years ago

solved here