Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 4911: make the argument work #4914

Merged
merged 1 commit into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- [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)
- [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)
- [Support NewBeegoRequestWithCtx in httplib](https://github.com/beego/beego/pull/4895)

Expand Down
2 changes: 1 addition & 1 deletion client/httplib/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type HTTPStatusCarrier interface {
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 {
SetHeader(header map[string][]string)
}
Expand Down
4 changes: 3 additions & 1 deletion core/logs/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package logs

import (
"fmt"
"path"
"strconv"
)
Expand Down Expand Up @@ -64,9 +65,10 @@ func GetFormatter(name string) (LogFormatter, bool) {
// 'l' level number, 't' prefix of level type, 'T' full name of level type
func (p *PatternLogFormatter) ToString(lm *LogMsg) string {
s := []rune(p.Pattern)
msg := fmt.Sprintf(lm.Msg, lm.Args...)
m := map[rune]string{
'w': lm.When.Format(p.getWhenFormatter()),
'm': lm.Msg,
'm': msg,
'n': strconv.Itoa(lm.LineNumber),
'l': strconv.Itoa(lm.Level),
't': levelPrefix[lm.Level],
Expand Down
45 changes: 33 additions & 12 deletions core/logs/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package logs
import (
"encoding/json"
"errors"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -79,17 +78,39 @@ func TestPatternLogFormatter(t *testing.T) {
WhenFormat: "2006-01-02",
}
when := time.Now()
lm := &LogMsg{
Msg: "message",
FilePath: "/User/go/beego/main.go",
Level: LevelWarn,
LineNumber: 10,
When: when,
testCases := []struct {
msg *LogMsg
want string
}{
{
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) + "|" +
when.Format(tes.WhenFormat) + levelPrefix[lm.Level] + ">> " + lm.Msg
if got != want {
t.Errorf("want %s, got %s", want, got)

for _, tc := range testCases {
got := tes.ToString(tc.msg)
assert.Equal(t, tc.want, got)
}
}