I'm using Google Wire for dependency injection and I want 2 loggers (error and info). So I created the following provider:
type errorLogger *log.Logger
type infoLogger *log.Logger
type Logger struct {
Error errorLogger
Info infoLogger
}
func ProvideLogger() *Logger {
return &Logger{
Error: log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
Info: log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime),
}
}
In my code I am referencing the loggers as so
h.Logger.Error
However, this doesn't give me access to the logger
methods as I would assume it would (such as Println
, Fatalf
etc)
I assume I am referencing something incorrectly, just not sure what.
The new types defined as
type errorLogger *log.Logger
don't inherit the methods of the underlying type.See the Go specs, Type Declarations > Type Definitions:
It follows that
Printf
and other*log.Logger
methods are not in the method set oferrorLogger
andinfoLogger
.You can use composition:
and then you can initialize it with: