rs / zerolog

Zero Allocation JSON Logger
MIT License
10.41k stars 567 forks source link

Matching caller path in both json and flat line logs #507

Closed bentcoder closed 1 year ago

bentcoder commented 1 year ago

Hi,

Just a suggestion. Would it be better to replace example code here as the one below so that the callers matches in json and flat line logs? e.g. some/path/to/file.go:123 (no forward slash at the beginning which matches json version)

Thanks

From

zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
    short := file
    for i := len(file) - 1; i > 0; i-- {
        if file[i] == '/' {
            short = file[i+1:]
            break
        }
    }
    file = short
    return file + ":" + strconv.Itoa(line)
}

To

zerolog.CallerMarshalFunc = func(_ uintptr, file string, line int) string {
    p, _ := os.Getwd()

    return fmt.Sprintf("%s:%d", strings.TrimPrefix(file, p+"/"), line)
}