update marshal source

This commit is contained in:
2026-03-02 17:23:58 +03:00
parent eb1c583207
commit c528d9fc22
2 changed files with 24 additions and 1 deletions

View File

@@ -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
View 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))
}
}