Skip to content

Commit 9a36510

Browse files
committed
improve log_helper package, more unit tests
Signed-off-by: Slach <bloodjazman@gmail.com>
1 parent feb5fd3 commit 9a36510

File tree

3 files changed

+264
-47
lines changed

3 files changed

+264
-47
lines changed

pkg/log_helper/log_helper.go

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7-
"os"
8-
"path/filepath"
97
"strconv"
108
"strings"
119

@@ -33,7 +31,6 @@ func CustomStackMarshaler(err error) interface{} {
3331
}
3432

3533
var lines []string
36-
cwd, _ := os.Getwd()
3734

3835
for _, frame := range st {
3936
// Format: package.function() on one line, file.go:line on next with tab
@@ -50,23 +47,8 @@ func CustomStackMarshaler(err error) interface{} {
5047
funcName := parts[0]
5148
fileLine := parts[1]
5249

53-
// Extract file path from file:line
54-
colonIdx := strings.LastIndex(fileLine, ":")
55-
if colonIdx != -1 {
56-
filePath := fileLine[:colonIdx]
57-
lineNum := fileLine[colonIdx:]
58-
59-
// Make path relative to cwd if it's a local file
60-
if strings.HasPrefix(filePath, cwd) {
61-
relPath, err := filepath.Rel(cwd, filePath)
62-
if err == nil {
63-
filePath = "./" + relPath
64-
}
65-
}
66-
67-
lines = append(lines, funcName+"()")
68-
lines = append(lines, "\t"+filePath+lineNum)
69-
}
50+
lines = append(lines, funcName+"()")
51+
lines = append(lines, "\t"+fileLine)
7052
}
7153
}
7254

@@ -76,16 +58,13 @@ func CustomStackMarshaler(err error) interface{} {
7658
// CustomWriter is a custom writer for zerolog that formats output
7759
type CustomWriter struct {
7860
out io.Writer
79-
cwd string
8061
buf bytes.Buffer // Reusable buffer to reduce allocations
8162
}
8263

8364
// NewCustomWriter creates a new custom writer
8465
func NewCustomWriter(out io.Writer) *CustomWriter {
85-
cwd, _ := os.Getwd()
8666
return &CustomWriter{
8767
out: out,
88-
cwd: cwd,
8968
}
9069
}
9170

@@ -123,12 +102,6 @@ func (w *CustomWriter) Write(p []byte) (n int, err error) {
123102

124103
// Caller
125104
if caller, err := jsonparser.GetString(p, "caller"); err == nil {
126-
// Make path relative if it's in cwd
127-
if strings.HasPrefix(caller, w.cwd) {
128-
if relPath, err := filepath.Rel(w.cwd, caller); err == nil {
129-
caller = relPath
130-
}
131-
}
132105
w.buf.WriteString(caller)
133106
w.buf.WriteString(" > ")
134107
}

pkg/log_helper/log_helper_test.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package log_helper
33
import (
44
"bytes"
55
stderrors "errors"
6-
"os"
76
"strings"
87
"testing"
98
"time"
@@ -391,36 +390,32 @@ func TestCustomWriterNoStack(t *testing.T) {
391390
}
392391
}
393392

394-
// TestCustomWriterRelativePathInCwd tests relative path handling for files in cwd
393+
// TestCustomWriterRelativePathInCwd tests that paths are displayed as-is without manipulation
395394
func TestCustomWriterRelativePathInCwd(t *testing.T) {
396395
var buf bytes.Buffer
397396
writer := NewCustomWriter(&buf)
398397

399-
// Get current directory for caller path
400-
cwd, _ := os.Getwd()
401-
callerPath := cwd + "/test.go:42"
398+
// Test with a relative path
399+
callerPath := "test.go:42"
402400

403401
jsonLog := []byte(`{"level":"info","caller":"` + callerPath + `","message":"test"}`)
404402
n, err := writer.Write(jsonLog)
405403
require.NoError(t, err)
406404
require.Greater(t, n, 0)
407405

408406
output := buf.String()
409-
// Should show relative path without full cwd
410-
if strings.Contains(output, cwd) {
411-
t.Errorf("Expected relative path, got full path in: %s", output)
412-
}
407+
// Should show path as-is
413408
if !strings.Contains(output, "test.go:42") {
414-
t.Errorf("Expected relative file path, got: %s", output)
409+
t.Errorf("Expected caller path as-is, got: %s", output)
415410
}
416411
}
417412

418-
// TestCustomWriterAbsolutePathOutsideCwd tests handling of paths outside cwd
413+
// TestCustomWriterAbsolutePathOutsideCwd tests that absolute paths are displayed as-is
419414
func TestCustomWriterAbsolutePathOutsideCwd(t *testing.T) {
420415
var buf bytes.Buffer
421416
writer := NewCustomWriter(&buf)
422417

423-
// Use system path that's definitely outside cwd
418+
// Test with an absolute path
424419
callerPath := "/usr/lib/go/src/runtime/proc.go:285"
425420

426421
jsonLog := []byte(`{"level":"info","caller":"` + callerPath + `","message":"test"}`)
@@ -429,9 +424,9 @@ func TestCustomWriterAbsolutePathOutsideCwd(t *testing.T) {
429424
require.Greater(t, n, 0)
430425

431426
output := buf.String()
432-
// Should keep absolute path for files outside cwd
427+
// Should show absolute path as-is
433428
if !strings.Contains(output, callerPath) {
434-
t.Errorf("Expected absolute path for file outside cwd, got: %s", output)
429+
t.Errorf("Expected absolute path as-is, got: %s", output)
435430
}
436431
}
437432

@@ -550,9 +545,6 @@ func TestNewCustomWriter(t *testing.T) {
550545
if writer.out != &buf {
551546
t.Error("Expected output writer to be set")
552547
}
553-
if writer.cwd == "" {
554-
t.Error("Expected cwd to be set")
555-
}
556548
}
557549

558550
// TestCustomWriterConcurrent tests concurrent writes (no race conditions)
@@ -597,5 +589,5 @@ func TestCustomWriterQuotedStringValues(t *testing.T) {
597589
output := buf.String()
598590
// Should handle quoted values properly
599591
require.Contains(t, output, "field=", "Expected field in output, got: %s", output)
600-
require.Contains(t, output, `"quoted value"`, "Expected \"quoted value\" in output, got: %s", output)
592+
require.Contains(t, output, `\"quoted value\"`, "Expected \\\"quoted value\\\" in output, got: %s", output)
601593
}

0 commit comments

Comments
 (0)