Skip to content

Commit 0450bee

Browse files
committed
Allow HTTP server to output logs with proper logger
Still, due to how the HTTP server is designed, not all errors are logged. For example, when Sshwifty is serving under plain HTTP mode, and the client sends a TLS handshake, no log will be added. On the other hand, if Sshwifty is serving under TLS mode, and the client sends a plain HTTP handshake, a log will be added to indicate the fault.
1 parent af8412f commit 0450bee

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

application/server/server.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package server
1919

2020
import (
21+
"bytes"
2122
"context"
2223
"crypto/tls"
2324
"errors"
@@ -33,10 +34,14 @@ import (
3334
"github.com/nirui/sshwifty/application/log"
3435
)
3536

36-
type dumpWrite struct{}
37+
// loggerWriter writes log to given log.Logger
38+
type loggerWriter struct {
39+
l log.Logger
40+
}
3741

38-
func (d dumpWrite) Write(b []byte) (int, error) {
39-
return len(b), nil
42+
// Write implements io.Writer
43+
func (d loggerWriter) Write(b []byte) (int, error) {
44+
return d.l.Write(bytes.TrimSpace(b))
4045
}
4146

4247
// Errors
@@ -96,7 +101,7 @@ func (s Server) Serve(
96101
WriteTimeout: ssCfg.WriteTimeout,
97102
IdleTimeout: ssCfg.ReadTimeout,
98103
MaxHeaderBytes: http.DefaultMaxHeaderBytes,
99-
ErrorLog: goLog.New(dumpWrite{}, "", 0),
104+
ErrorLog: goLog.New(loggerWriter{l: l}, "", 0),
100105
},
101106
shutdownWait: s.shutdownWait,
102107
}
@@ -153,7 +158,8 @@ func (s *Serving) run(
153158
s.shutdownWait.Done()
154159
closeCallback(err)
155160
}()
156-
ls, err := s.buildListener(
161+
var ls listener
162+
ls, err = s.buildListener(
157163
cfg.ListenInterface,
158164
cfg.ListenPort,
159165
cfg.ReadTimeout,

0 commit comments

Comments
 (0)