forked from cloudwego/hertz
feat: support silentmode to mute engine log (#745)
This commit is contained in:
parent
6100069721
commit
2e9df0df9e
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
)
|
|
@ -22,10 +22,6 @@ import (
|
|||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
systemLogPrefix = "HERTZ: "
|
||||
)
|
||||
|
||||
var (
|
||||
// Provide default logger for users to use
|
||||
logger FullLogger = &defaultLogger{
|
||||
|
|
|
@ -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...)
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue