I would like to log errors from net/http in my own format. In net/http package I have found Server struct:
type Server struct {
//...
ErrorLog *log.Logger
}
I would like to substitute logger with my own implementation:
type AppLogger struct {
log *zap.SugaredLogger
}
func (l *AppLogger) Error(message string, keyAndValues ...interface{}) {
l.log.Errorw(message, keyAndValues...)
}
What is the correct way of implementing this?
Update:
I have zap logger with following config:
cfg := zap.Config{
Encoding: encoding,
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stdout"},
EncoderConfig: encCfg,
}
logger, err := cfg.Build()
It configured to write in json format. I would like errors from net/http
be written in the same way as zap. I create following:
type serverJsonWriter struct {
io.Writer
}
// ListenAndServeTLS - with custom log Writer
func ListenAndServeTLS(addr, certFile, keyFile string, handler http.Handler) error {
server := &http.Server{
Addr: addr,
Handler: handler,
ErrorLog: logger.New(serverJsonWriter{}, "", 0),
}
}
func (w serverJsonWriter) Write(p []byte) (n int, err error){
// {"error":{"type":"net/http error","message":"header too long"}}
}
Questions:
- What should be the body of
serverJsonWriter
method? - Should I retrieve
zap
io.Writer in order to pass itlog.Logger
? How to do this?