Repository: rs/zerolog
Pull request #267 (2020-11-02 13:37:47)

Log relative path for caller when using `ConsoleWriter`
This PR improves on #266 by always logging relative file path, rather than logging absolute path when the caller is outside the current directory.  This results in less verbose, yet equally useful, output.

With `master` after #266 merged:

```
8:29PM INF resource_project_test.go:114 > Cleaning up Rollbar projects from acceptance test runs.
8:29PM DBG /home/jmcvetta/projects/terraform-provider-rollbar/client/client.go:43 > Initializing Rollbar client
8:29PM DBG /home/jmcvetta/projects/terraform-provider-rollbar/client/project.go:112 > Successfully listed projects cleaned_projects=1 raw_projects=2351 url=https://api.rollbar.com/api/1/projects
8:29PM INF resource_project_test.go:138 > Projects cleanup complete
2020/11/02 20:29:58 [DEBUG] Running Sweeper (rollbar_team) in region (all)
8:29PM INF resource_team_test.go:160 > Cleaning up Rollbar teams from acceptance test runs.
8:29PM DBG /home/jmcvetta/projects/terraform-provider-rollbar/client/client.go:43 > Initializing Rollbar client
8:29PM INF /home/jmcvetta/projects/terraform-provider-rollbar/client/team.go:84 > Listing all teams
8:29PM DBG /home/jmcvetta/projects/terraform-provider-rollbar/client/team.go:102 > Successfully listed teams
8:29PM INF resource_team_test.go:184 > Teams cleanup complete
```

With this PR:

```
8:30PM INF resource_project_test.go:114 > Cleaning up Rollbar projects from acceptance test runs.
8:30PM DBG ../client/client.go:43 > Initializing Rollbar client
8:30PM DBG ../client/project.go:112 > Successfully listed projects cleaned_projects=1 raw_projects=2351 url=https://api.rollbar.com/api/1/projects
8:30PM INF resource_project_test.go:138 > Projects cleanup complete
2020/11/02 20:30:28 [DEBUG] Running Sweeper (rollbar_team) in region (all)
8:30PM INF resource_team_test.go:160 > Cleaning up Rollbar teams from acceptance test runs.
8:30PM DBG ../client/client.go:43 > Initializing Rollbar client
8:30PM INF ../client/team.go:84 > Listing all teams
8:30PM DBG ../client/team.go:102 > Successfully listed teams
8:30PM INF resource_team_test.go:184 > Teams cleanup complete
```

The relative paths are correctly interpreted and clickable in GoLand IDE:

![image](https://user-images.githubusercontent.com/55682/97873623-7b660100-1d4a-11eb-913a-f2d36ac33a17.png)



Patch:
diff --git a/console.go b/console.go
index 2d80a15..fd0cad4 100644
--- a/console.go
+++ b/console.go
@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"io"
 	"os"
+	"path/filepath"
 	"sort"
 	"strconv"
 	"strings"
@@ -57,6 +58,9 @@ type ConsoleWriter struct {
 	// PartsOrder defines the order of parts in output.
 	PartsOrder []string
 
+	// PartsExclude defines parts to not display in output.
+	PartsExclude []string
+
 	FormatTimestamp     Formatter
 	FormatLevel         Formatter
 	FormatCaller        Formatter
@@ -208,6 +212,14 @@ func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer
 func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{}, p string) {
 	var f Formatter
 
+	if w.PartsExclude != nil && len(w.PartsExclude) > 0 {
+		for _, exclude := range w.PartsExclude {
+			if exclude == p {
+				return
+			}
+		}
+	}
+
 	switch p {
 	case LevelFieldName:
 		if w.FormatLevel == nil {
@@ -356,10 +368,10 @@ func consoleDefaultFormatCaller(noColor bool) Formatter {
 			c = cc
 		}
 		if len(c) > 0 {
-			cwd, err := os.Getwd()
-			if err == nil {
-				prefix := cwd + "/"
-				c = strings.TrimPrefix(c, prefix)
+			if cwd, err := os.Getwd(); err == nil {
+				if rel, err := filepath.Rel(cwd, c); err == nil {
+					c = rel
+				}
 			}
 			c = colorize(c, colorBold, noColor) + colorize(" >", colorCyan, noColor)
 		}


Test patch:
diff --git a/console_test.go b/console_test.go
index 751cdcd..508a544 100644
--- a/console_test.go
+++ b/console_test.go
@@ -336,6 +336,24 @@ func TestConsoleWriterConfiguration(t *testing.T) {
 			t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
 		}
 	})
+
+	t.Run("Sets PartsExclude", func(t *testing.T) {
+		buf := &bytes.Buffer{}
+		w := zerolog.ConsoleWriter{Out: buf, NoColor: true, PartsExclude: []string{"time"}}
+
+		d := time.Unix(0, 0).UTC().Format(time.RFC3339)
+		evt := `{"time": "` + d + `", "level": "info", "message": "Foobar"}`
+		_, err := w.Write([]byte(evt))
+		if err != nil {
+			t.Errorf("Unexpected error when writing output: %s", err)
+		}
+
+		expectedOutput := "INF Foobar\n"
+		actualOutput := buf.String()
+		if actualOutput != expectedOutput {
+			t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
+		}
+	})
 }
 
 func BenchmarkConsoleWriter(b *testing.B) {