fix 4911: make the argument work

This commit is contained in:
Deng Ming 2022-04-17 18:23:40 +08:00
parent 047e4a6750
commit 174291a3c8
4 changed files with 38 additions and 14 deletions

View File

@ -2,6 +2,7 @@
- [upgrade redisgo to v1.8.8](https://github.com/beego/beego/pull/4872) - [upgrade redisgo to v1.8.8](https://github.com/beego/beego/pull/4872)
- [fix prometheus CVE-2022-21698](https://github.com/beego/beego/pull/4878) - [fix prometheus CVE-2022-21698](https://github.com/beego/beego/pull/4878)
- [upgrade to Go 1.18](https://github.com/beego/beego/pull/4896) - [upgrade to Go 1.18](https://github.com/beego/beego/pull/4896)
- [make `PatternLogFormatter` handling the arguments](https://github.com/beego/beego/pull/4914/files)
- [Add httplib OpenTelemetry Filter](https://github.com/beego/beego/pull/4888) - [Add httplib OpenTelemetry Filter](https://github.com/beego/beego/pull/4888)
- [Support NewBeegoRequestWithCtx in httplib](https://github.com/beego/beego/pull/4895) - [Support NewBeegoRequestWithCtx in httplib](https://github.com/beego/beego/pull/4895)

View File

@ -51,7 +51,7 @@ type HTTPStatusCarrier interface {
SetStatusCode(status int) SetStatusCode(status int)
} }
// HttpHeaderCarrier If value implement HttpHeaderCarrier. http.Response.Header will pass to SetHeader // HTTPHeadersCarrier If value implement HttpHeaderCarrier. http.Response.Header will pass to SetHeader
type HTTPHeadersCarrier interface { type HTTPHeadersCarrier interface {
SetHeader(header map[string][]string) SetHeader(header map[string][]string)
} }

View File

@ -15,6 +15,7 @@
package logs package logs
import ( import (
"fmt"
"path" "path"
"strconv" "strconv"
) )
@ -64,9 +65,10 @@ func GetFormatter(name string) (LogFormatter, bool) {
// 'l' level number, 't' prefix of level type, 'T' full name of level type // 'l' level number, 't' prefix of level type, 'T' full name of level type
func (p *PatternLogFormatter) ToString(lm *LogMsg) string { func (p *PatternLogFormatter) ToString(lm *LogMsg) string {
s := []rune(p.Pattern) s := []rune(p.Pattern)
msg := fmt.Sprintf(lm.Msg, lm.Args...)
m := map[rune]string{ m := map[rune]string{
'w': lm.When.Format(p.getWhenFormatter()), 'w': lm.When.Format(p.getWhenFormatter()),
'm': lm.Msg, 'm': msg,
'n': strconv.Itoa(lm.LineNumber), 'n': strconv.Itoa(lm.LineNumber),
'l': strconv.Itoa(lm.Level), 'l': strconv.Itoa(lm.Level),
't': levelPrefix[lm.Level], 't': levelPrefix[lm.Level],

View File

@ -17,7 +17,6 @@ package logs
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"strconv"
"testing" "testing"
"time" "time"
@ -79,17 +78,39 @@ func TestPatternLogFormatter(t *testing.T) {
WhenFormat: "2006-01-02", WhenFormat: "2006-01-02",
} }
when := time.Now() when := time.Now()
lm := &LogMsg{ testCases := []struct {
Msg: "message", msg *LogMsg
FilePath: "/User/go/beego/main.go", want string
Level: LevelWarn, }{
LineNumber: 10, {
When: when, msg: &LogMsg{
Msg: "hello %s",
FilePath: "/User/go/beego/main.go",
Level: LevelWarn,
LineNumber: 10,
When: when,
Args: []interface{}{"world"},
},
want: "/User/go/beego/main.go:10|2022-04-17[W]>> hello world",
},
{
msg: &LogMsg{
Msg: "hello",
FilePath: "/User/go/beego/main.go",
Level: LevelWarn,
LineNumber: 10,
When: when,
},
want: "/User/go/beego/main.go:10|2022-04-17[W]>> hello",
},
{
msg: &LogMsg{},
want: ":0|0001-01-01[M]>> ",
},
} }
got := tes.ToString(lm)
want := lm.FilePath + ":" + strconv.Itoa(lm.LineNumber) + "|" + for _, tc := range testCases {
when.Format(tes.WhenFormat) + levelPrefix[lm.Level] + ">> " + lm.Msg got := tes.ToString(tc.msg)
if got != want { assert.Equal(t, tc.want, got)
t.Errorf("want %s, got %s", want, got)
} }
} }