Skip to content

Commit e57d1f6

Browse files
committed
Mute unhelpful messages printed to log
1 parent 4153fc1 commit e57d1f6

File tree

5 files changed

+134
-7
lines changed

5 files changed

+134
-7
lines changed

.github/dependabot.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ updates:
44
directory: "/"
55
schedule:
66
interval: "weekly"
7+
- package-ecosystem: "github-actions"
8+
directory: "/"
9+
schedule:
10+
interval: "weekly"

.github/workflows/go.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ jobs:
1313
build:
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/checkout@7884fcad6b5d53d10323aee724dc68d8b9096a2e # pin@v2
16+
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # pin@v3.1.0
1717

1818
- name: Set up Go
19-
uses: actions/setup-go@bfdd3570ce990073878bf10f6b2d79082de49492 # pin@v2
19+
uses: actions/setup-go@268d8c0ca0432bb2cf416faae41297df9d262d7f # pin@v3.3.0
2020
with:
21-
go-version: 1.19
21+
go-version: 1.19.2
2222

2323
- name: Build
2424
run: go build -v ./...

router/log.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package router
2+
3+
import (
4+
"bytes"
5+
6+
"github.com/ecnepsnai/logtic"
7+
)
8+
9+
// mute the following unhelpful events from log lines
10+
11+
var logMutePatterns = [][]byte{
12+
[]byte("http: TLS handshake error from"),
13+
}
14+
15+
type muteLogger struct {
16+
source *logtic.Source
17+
level int
18+
}
19+
20+
func (l muteLogger) Write(p []byte) (n int, err error) {
21+
length := len(p)
22+
23+
mute := false
24+
for _, pattern := range logMutePatterns {
25+
if bytes.Contains(p, pattern) {
26+
mute = true
27+
break
28+
}
29+
}
30+
if !mute {
31+
l.source.Write(l.level, string(p[0:length-1]))
32+
}
33+
34+
return len(p), nil
35+
}

router/log_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package router_test
2+
3+
import (
4+
"bytes"
5+
"crypto"
6+
"crypto/ecdsa"
7+
"crypto/elliptic"
8+
"crypto/rand"
9+
"crypto/tls"
10+
"crypto/x509"
11+
"math/big"
12+
"net"
13+
"strings"
14+
"testing"
15+
"time"
16+
17+
"github.com/ecnepsnai/logtic"
18+
"github.com/ecnepsnai/web/router"
19+
)
20+
21+
func TestMuteLogger(t *testing.T) {
22+
b := &bytes.Buffer{}
23+
logtic.Log.Open()
24+
logtic.Log.Level = logtic.LevelDebug
25+
logtic.Log.Stdout = b
26+
logtic.Log.Stderr = b
27+
28+
listenAddress := getListenAddress()
29+
30+
var pKey crypto.PrivateKey
31+
var err error
32+
pKey, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
pub := pKey.(crypto.Signer).Public()
38+
tpl := &x509.Certificate{
39+
SerialNumber: &big.Int{},
40+
NotBefore: time.Now().UTC().AddDate(-100, 0, 0),
41+
NotAfter: time.Now().UTC().AddDate(100, 0, 0),
42+
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageDataEncipherment,
43+
BasicConstraintsValid: true,
44+
}
45+
46+
certBytes, err := x509.CreateCertificate(rand.Reader, tpl, tpl, pub, pKey)
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
l, err := tls.Listen("tcp", listenAddress, &tls.Config{
52+
Certificates: []tls.Certificate{
53+
{
54+
Certificate: [][]byte{certBytes},
55+
PrivateKey: pKey,
56+
},
57+
},
58+
})
59+
if err != nil {
60+
panic(err)
61+
}
62+
63+
server := router.New()
64+
server.ServeFiles(t.TempDir(), "/")
65+
go func() {
66+
server.Serve(l)
67+
}()
68+
time.Sleep(5 * time.Millisecond)
69+
70+
c, err := net.Dial("tcp", listenAddress)
71+
if err != nil {
72+
panic(err)
73+
}
74+
if _, err := c.Write([]byte("")); err != nil {
75+
panic(err)
76+
}
77+
c.Close()
78+
time.Sleep(5 * time.Millisecond)
79+
80+
output := b.String()
81+
if strings.Contains(output, "http: TLS handshake error from") {
82+
t.Errorf("log output contains forbidden log events")
83+
}
84+
}

router/server.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package router
22

33
import (
4+
golog "log"
45
"net"
56
"net/http"
67
"sync"
@@ -39,7 +40,10 @@ func New() *Server {
3940
httpServer: &http.Server{
4041
ReadTimeout: 5 * time.Minute,
4142
ReadHeaderTimeout: 5 * time.Minute,
42-
ErrorLog: log.GoLogger(logtic.LevelError),
43+
ErrorLog: golog.New(muteLogger{
44+
source: log,
45+
level: logtic.LevelError,
46+
}, "", 0),
4347
},
4448
}
4549
return s
@@ -59,8 +63,6 @@ func (s *Server) ListenAndServe(addr string) error {
5963
})
6064
return err
6165
}
62-
s.listener = &l
63-
s.httpServer.Handler = s.impl
6466
s.impl.log.PDebug("Listen", map[string]interface{}{
6567
"address": addr,
6668
})
@@ -72,7 +74,9 @@ func (s *Server) ListenAndServe(addr string) error {
7274
// An error will only be returned if there was an error listening or the listener was abruptly closed.
7375
func (s *Server) Serve(listener net.Listener) error {
7476
s.impl.log.Debug("Serve on listener")
75-
return http.Serve(listener, s.impl)
77+
s.httpServer.Handler = s.impl
78+
s.listener = &listener
79+
return s.httpServer.Serve(listener)
7680
}
7781

7882
// Stop will stop the server. Server.ListenAndServe or Server.Serve will return net.ErrClosed. Does nothing if the

0 commit comments

Comments
 (0)