Skip to content

Commit 2405b58

Browse files
authored
ZING-22931: Add go module and fix for errors (#1)
1 parent aa17bfa commit 2405b58

7 files changed

+67
-24
lines changed

README.md

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
# logrus-stackdriver-formatter
22

3-
[![Build Status](https://travis-ci.org/TV4/logrus-stackdriver-formatter.svg?branch=master)](https://travis-ci.org/TV4/logrus-stackdriver-formatter)
4-
[![Go Report Card](https://goreportcard.com/badge/github.com/TV4/logrus-stackdriver-formatter)](https://goreportcard.com/report/github.com/TV4/logrus-stackdriver-formatter)
5-
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/TV4/logrus-stackdriver-formatter)
6-
[![License MIT](https://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat)](https://github.com/TV4/logrus-stackdriver-formatter#license)
7-
83
[logrus](https://github.com/sirupsen/logrus) formatter for Stackdriver.
94

105
In addition to supporting level-based logging to Stackdriver, for Error, Fatal and Panic levels it will append error context for [Error Reporting](https://cloud.google.com/error-reporting/).
116

127
## Installation
138

149
```shell
15-
go get -u github.com/TV4/logrus-stackdriver-formatter
10+
go get github.com/zenoss/logrus-stackdriver-formatter
1611
```
1712

1813
## Usage
@@ -22,7 +17,7 @@ package main
2217

2318
import (
2419
"github.com/sirupsen/logrus"
25-
stackdriver "github.com/TV4/logrus-stackdriver-formatter"
20+
stackdriver "github.com/zenoss/logrus-stackdriver-formatter"
2621
)
2722

2823
var log = logrus.New()
@@ -50,7 +45,7 @@ Here's a sample entry (prettified) from the example:
5045
"severity": "ERROR",
5146
"context": {
5247
"reportLocation": {
53-
"filePath": "github.com/TV4/logrus-stackdriver-formatter/example_test.go",
48+
"filePath": "github.com/zenoss/logrus-stackdriver-formatter/example_test.go",
5449
"lineNumber": 21,
5550
"functionName": "ExampleLogError"
5651
}

example_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"os"
55
"strconv"
66

7-
stackdriver "github.com/TV4/logrus-stackdriver-formatter"
87
"github.com/sirupsen/logrus"
8+
stackdriver "github.com/zenoss/logrus-stackdriver-formatter"
99
)
1010

1111
func ExampleLogError() {
@@ -25,5 +25,5 @@ func ExampleLogError() {
2525

2626
// Output:
2727
// {"message":"application up and running","severity":"INFO","context":{}}
28-
// {"serviceContext":{"service":"test-service","version":"v0.1.0"},"message":"unable to parse integer: strconv.ParseInt: parsing \"text\": invalid syntax","severity":"ERROR","context":{"reportLocation":{"filePath":"github.com/TV4/logrus-stackdriver-formatter/example_test.go","lineNumber":23,"functionName":"ExampleLogError"}}}
28+
// {"serviceContext":{"service":"test-service","version":"v0.1.0"},"message":"unable to parse integer: strconv.ParseInt: parsing \"text\": invalid syntax","severity":"ERROR","context":{"reportLocation":{"filePath":"github.com/zenoss/logrus-stackdriver-formatter/example_test.go","lineNumber":23,"functionName":"ExampleLogError"}}}
2929
}

formatter.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,21 @@ func (f *Formatter) errorOrigin() (stack.Call, error) {
133133
func (f *Formatter) Format(e *logrus.Entry) ([]byte, error) {
134134
severity := levelsToSeverity[e.Level]
135135

136-
ee := entry{
136+
data := make(map[string]interface{}, len(e.Data))
137+
for k, v := range e.Data {
138+
switch t := v.(type) {
139+
case error:
140+
data[k] = t.Error()
141+
default:
142+
data[k] = v
143+
}
144+
}
137145

146+
ee := entry{
138147
Message: e.Message,
139148
Severity: severity,
140149
Context: &context{
141-
Data: e.Data,
150+
Data: data,
142151
},
143152
}
144153

@@ -156,19 +165,19 @@ func (f *Formatter) Format(e *logrus.Entry) ([]byte, error) {
156165
// When using WithError(), the error is sent separately, but Error
157166
// Reporting expects it to be a part of the message so we append it
158167
// instead.
159-
if err, ok := ee.Context.Data["error"]; ok {
168+
if err, ok := data["error"]; ok {
160169
ee.Message = fmt.Sprintf("%s: %s", e.Message, err)
161-
delete(ee.Context.Data, "error")
170+
delete(data, "error")
162171
} else {
163172
ee.Message = e.Message
164173
}
165174

166175
// As a convenience, when using supplying the httpRequest field, it
167176
// gets special care.
168-
if reqData, ok := ee.Context.Data["httpRequest"]; ok {
177+
if reqData, ok := data["httpRequest"]; ok {
169178
if req, ok := reqData.(map[string]interface{}); ok {
170179
ee.Context.HTTPRequest = req
171-
delete(ee.Context.Data, "httpRequest")
180+
delete(data, "httpRequest")
172181
}
173182
}
174183

formatter_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestFormatter(t *testing.T) {
2828
tt.run(logger)
2929

3030
var got map[string]interface{}
31-
json.Unmarshal(out.Bytes(), &got)
31+
_ = json.Unmarshal(out.Bytes(), &got)
3232

3333
if !reflect.DeepEqual(got, tt.out) {
3434
t.Errorf("unexpected output = %# v; want = %# v", pretty.Formatter(got), pretty.Formatter(tt.out))
@@ -70,7 +70,7 @@ var formatterTests = []struct {
7070
"foo": "bar",
7171
},
7272
"reportLocation": map[string]interface{}{
73-
"filePath": "github.com/TV4/logrus-stackdriver-formatter/formatter_test.go",
73+
"filePath": "github.com/zenoss/logrus-stackdriver-formatter/formatter_test.go",
7474
"lineNumber": 59.0,
7575
"functionName": "glob..func2",
7676
},
@@ -96,7 +96,7 @@ var formatterTests = []struct {
9696
"foo": "bar",
9797
},
9898
"reportLocation": map[string]interface{}{
99-
"filePath": "github.com/TV4/logrus-stackdriver-formatter/formatter_test.go",
99+
"filePath": "github.com/zenoss/logrus-stackdriver-formatter/formatter_test.go",
100100
"lineNumber": 85.0,
101101
"functionName": "glob..func3",
102102
},
@@ -129,7 +129,7 @@ var formatterTests = []struct {
129129
"method": "GET",
130130
},
131131
"reportLocation": map[string]interface{}{
132-
"filePath": "github.com/TV4/logrus-stackdriver-formatter/formatter_test.go",
132+
"filePath": "github.com/zenoss/logrus-stackdriver-formatter/formatter_test.go",
133133
"lineNumber": 115.0,
134134
"functionName": "glob..func4",
135135
},

go.mod

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module github.com/zenoss/logrus-stackdriver-formatter
2+
3+
go 1.17
4+
5+
require (
6+
github.com/go-stack/stack v1.8.1
7+
github.com/kr/pretty v0.3.0
8+
github.com/sirupsen/logrus v1.8.1
9+
)
10+
11+
require (
12+
github.com/kr/text v0.2.0 // indirect
13+
github.com/rogpeppe/go-internal v1.6.1 // indirect
14+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
15+
)

go.sum

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
5+
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
6+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
7+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
8+
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
9+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
10+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
11+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
12+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
13+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
14+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15+
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
16+
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
17+
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
18+
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
19+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
20+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
21+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
22+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
23+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
24+
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=

stackskip_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"reflect"
77
"testing"
88

9-
"github.com/TV4/logrus-stackdriver-formatter/test"
109
"github.com/kr/pretty"
1110
"github.com/sirupsen/logrus"
11+
"github.com/zenoss/logrus-stackdriver-formatter/test"
1212
)
1313

1414
func TestStackSkip(t *testing.T) {
@@ -19,7 +19,7 @@ func TestStackSkip(t *testing.T) {
1919
logger.Formatter = NewFormatter(
2020
WithService("test"),
2121
WithVersion("0.1"),
22-
WithStackSkip("github.com/TV4/logrus-stackdriver-formatter/test"),
22+
WithStackSkip("github.com/zenoss/logrus-stackdriver-formatter/test"),
2323
)
2424

2525
mylog := test.LogWrapper{
@@ -29,7 +29,7 @@ func TestStackSkip(t *testing.T) {
2929
mylog.Error("my log entry")
3030

3131
var got map[string]interface{}
32-
json.Unmarshal(out.Bytes(), &got)
32+
_ = json.Unmarshal(out.Bytes(), &got)
3333

3434
want := map[string]interface{}{
3535
"severity": "ERROR",
@@ -40,7 +40,7 @@ func TestStackSkip(t *testing.T) {
4040
},
4141
"context": map[string]interface{}{
4242
"reportLocation": map[string]interface{}{
43-
"filePath": "github.com/TV4/logrus-stackdriver-formatter/stackskip_test.go",
43+
"filePath": "github.com/zenoss/logrus-stackdriver-formatter/stackskip_test.go",
4444
"lineNumber": 29.0,
4545
"functionName": "TestStackSkip",
4646
},

0 commit comments

Comments
 (0)