diff --git a/pkg/app/server/hertz_test.go b/pkg/app/server/hertz_test.go index aef7824ab..b9a07fbce 100644 --- a/pkg/app/server/hertz_test.go +++ b/pkg/app/server/hertz_test.go @@ -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" @@ -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()) + } +} diff --git a/pkg/common/hlog/consts.go b/pkg/common/hlog/consts.go new file mode 100644 index 000000000..e0d31bb8d --- /dev/null +++ b/pkg/common/hlog/consts.go @@ -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" +) diff --git a/pkg/common/hlog/hlog.go b/pkg/common/hlog/hlog.go index 11a81615e..7f73aaa0d 100644 --- a/pkg/common/hlog/hlog.go +++ b/pkg/common/hlog/hlog.go @@ -22,10 +22,6 @@ import ( "os" ) -const ( - systemLogPrefix = "HERTZ: " -) - var ( // Provide default logger for users to use logger FullLogger = &defaultLogger{ diff --git a/pkg/common/hlog/system.go b/pkg/common/hlog/system.go index cb8a1453c..6d2b86bd4 100644 --- a/pkg/common/hlog/system.go +++ b/pkg/common/hlog/system.go @@ -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 }} @@ -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...) } diff --git a/pkg/route/engine.go b/pkg/route/engine.go index 8a9a00b0c..10cffac7a 100644 --- a/pkg/route/engine.go +++ b/pkg/route/engine.go @@ -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 {