Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit 18a2437

Browse files
author
Marcus Olsson
committed
Add test for stack skipping
1 parent 1e11d08 commit 18a2437

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

stackskip_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package stackdriver
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"reflect"
7+
"testing"
8+
9+
"github.com/TV4/logrus-stackdriver-formatter/test"
10+
"github.com/kr/pretty"
11+
"github.com/sirupsen/logrus"
12+
)
13+
14+
func TestStackSkip(t *testing.T) {
15+
var out bytes.Buffer
16+
17+
logger := logrus.New()
18+
logger.Out = &out
19+
logger.Formatter = NewFormatter(
20+
WithService("test"),
21+
WithVersion("0.1"),
22+
WithStackSkip("github.com/TV4/logrus-stackdriver-formatter/test"),
23+
)
24+
25+
mylog := test.LogWrapper{
26+
Logger: logger,
27+
}
28+
29+
mylog.Error("my log entry")
30+
31+
var got map[string]interface{}
32+
json.Unmarshal(out.Bytes(), &got)
33+
34+
want := map[string]interface{}{
35+
"severity": "ERROR",
36+
"message": "my log entry",
37+
"serviceContext": map[string]interface{}{
38+
"service": "test",
39+
"version": "0.1",
40+
},
41+
"context": map[string]interface{}{
42+
"reportLocation": map[string]interface{}{
43+
"filePath": "github.com/TV4/logrus-stackdriver-formatter/stackskip_test.go",
44+
"lineNumber": 29.0,
45+
"functionName": "TestStackSkip",
46+
},
47+
},
48+
}
49+
50+
if !reflect.DeepEqual(got, want) {
51+
t.Errorf("unexpected output = %# v; want = %# v", pretty.Formatter(got), pretty.Formatter(want))
52+
}
53+
}

test/logger.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package test
2+
3+
import "github.com/sirupsen/logrus"
4+
5+
type LogWrapper struct {
6+
Logger *logrus.Logger
7+
}
8+
9+
func (l *LogWrapper) Error(msg string) {
10+
l.Logger.Error(msg)
11+
}

0 commit comments

Comments
 (0)