Skip to content

Commit

Permalink
feat: support silentmode to mute engine log (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
Duslia committed Apr 27, 2023
1 parent 6100069 commit 2e9df0d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
41 changes: 41 additions & 0 deletions pkg/app/server/hertz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/cloudwego/hertz/pkg/app/server/registry"
"github.com/cloudwego/hertz/pkg/common/config"
errs "github.com/cloudwego/hertz/pkg/common/errors"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/common/test/assert"
"github.com/cloudwego/hertz/pkg/common/test/mock"
"github.com/cloudwego/hertz/pkg/common/utils"
Expand Down Expand Up @@ -740,3 +741,43 @@ func TestOnprepare(t *testing.T) {
time.Sleep(time.Second)
c.Get(context.Background(), nil, "http://127.0.0.1:9231/ping")
}

type lockBuffer struct {
sync.Mutex
b bytes.Buffer
}

func (l *lockBuffer) Write(p []byte) (int, error) {
l.Lock()
defer l.Unlock()
return l.b.Write(p)
}

func (l *lockBuffer) String() string {
l.Lock()
defer l.Unlock()
return l.b.String()
}

func TestSilentMode(t *testing.T) {
hlog.SetSilentMode(true)
b := &lockBuffer{b: bytes.Buffer{}}

hlog.SetOutput(b)

h := New(WithHostPorts("localhost:9232"), WithTransport(standard.NewTransporter))
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
ctx.Write([]byte("hello, world"))
})
go h.Spin()
time.Sleep(time.Second)

d := standard.NewDialer()
conn, _ := d.DialConnection("tcp", "127.0.0.1:9232", 0, nil)
conn.Write([]byte("aaa"))
conn.Close()

if strings.Contains(b.String(), "Error") {
t.Fatalf("unexpected error in log: %s", b.String())
}
}
23 changes: 23 additions & 0 deletions pkg/common/hlog/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2023 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package hlog

const (
systemLogPrefix = "HERTZ: "

EngineErrorFormat = "Error=%s, remoteAddr=%s"
)
4 changes: 0 additions & 4 deletions pkg/common/hlog/hlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ import (
"os"
)

const (
systemLogPrefix = "HERTZ: "
)

var (
// Provide default logger for users to use
logger FullLogger = &defaultLogger{
Expand Down
12 changes: 12 additions & 0 deletions pkg/common/hlog/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ import (
"sync"
)

var silentMode = false

// SetSilentMode is used to mute engine error log,
// for example: error when reading request headers.
// If true, hertz engine will mute it.
func SetSilentMode(s bool) {
silentMode = s
}

var builderPool = sync.Pool{New: func() interface{} {
return &strings.Builder{} // nolint:SA6002
}}
Expand Down Expand Up @@ -80,6 +89,9 @@ func (ll *systemLogger) Fatalf(format string, v ...interface{}) {
}

func (ll *systemLogger) Errorf(format string, v ...interface{}) {
if silentMode && format == EngineErrorFormat {
return
}
ll.logger.Errorf(ll.addPrefix(format), v...)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/route/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func errProcess(conn io.Closer, err error) {
}
}
// other errors
hlog.SystemLogger().Errorf("Error=%s, remoteAddr=%s", err.Error(), rip)
hlog.SystemLogger().Errorf(hlog.EngineErrorFormat, err.Error(), rip)
}

func getRemoteAddrFromCloser(conn io.Closer) string {
Expand Down

0 comments on commit 2e9df0d

Please sign in to comment.