| 1155 | } |
| 1156 | |
| 1157 | func TestCallerMarshalFunc(t *testing.T) { |
| 1158 | out := &bytes.Buffer{} |
| 1159 | log := New(out) |
| 1160 | |
| 1161 | var pc uintptr |
| 1162 | var file string |
| 1163 | var line int |
| 1164 | // test default behaviour this is really brittle due to the line numbers |
| 1165 | // actually mattering for validation |
| 1166 | pc, file, line, _ = runtime.Caller(0) |
| 1167 | caller := fmt.Sprintf("%s:%d", file, line+2) |
| 1168 | log.Log().Caller().Msg("msg") |
| 1169 | if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","message":"msg"}`+"\n"; got != want { |
| 1170 | t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want) |
| 1171 | } |
| 1172 | out.Reset() |
| 1173 | |
| 1174 | // test custom behavior. In this case we'll take just the last directory |
| 1175 | origCallerMarshalFunc := CallerMarshalFunc |
| 1176 | defer func() { CallerMarshalFunc = origCallerMarshalFunc }() |
| 1177 | CallerMarshalFunc = func(pc uintptr, file string, line int) string { |
| 1178 | parts := strings.Split(file, "/") |
| 1179 | if len(parts) > 1 { |
| 1180 | return strings.Join(parts[len(parts)-2:], "/") + ":" + strconv.Itoa(line) |
| 1181 | } |
| 1182 | |
| 1183 | return runtime.FuncForPC(pc).Name() + ":" + file + ":" + strconv.Itoa(line) |
| 1184 | } |
| 1185 | pc, file, line, _ = runtime.Caller(0) |
| 1186 | caller = CallerMarshalFunc(pc, file, line+2) |
| 1187 | log.Log().Caller().Msg("msg") |
| 1188 | if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","message":"msg"}`+"\n"; got != want { |
| 1189 | t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want) |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | func TestLevelFieldMarshalFunc(t *testing.T) { |
| 1194 | origLevelFieldMarshalFunc := LevelFieldMarshalFunc |