update marshal source
This commit is contained in:
@@ -86,7 +86,7 @@ func (l Source) MarshalText() ([]byte, error) {
|
||||
}
|
||||
|
||||
func (l Source) MarshalJSON() ([]byte, error) {
|
||||
return fmt.Appendf([]byte{}, `{"file":"%s","line":%d,"func":"%s"}`, l.File, l.Line, l.Func), nil
|
||||
return fmt.Appendf([]byte{}, `{"file":%q,"line":%d,"func":"%s"}`, l.File, l.Line, l.Func), nil
|
||||
}
|
||||
|
||||
func errSourceField(skip, mframe int) field.Field {
|
||||
|
||||
23
source_test.go
Normal file
23
source_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package log_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/log"
|
||||
)
|
||||
|
||||
func TestSource_MarshalJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
src := log.Source{
|
||||
Func: "fn name",
|
||||
File: `file " \n name`,
|
||||
Line: 42,
|
||||
}
|
||||
|
||||
data, err := json.Marshal(src)
|
||||
if err != nil || len(data) == 0 || string(data) != `{"file":"file \" \\n name","line":42,"func":"fn name"}` {
|
||||
t.Fatalf("failed marshal: err=%v, data=%v", err, string(data))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user