From e857914b3de02ea34a8dc5e55c8fb52f2f183576 Mon Sep 17 00:00:00 2001 From: Cai Cooper Date: Thu, 9 Apr 2020 15:17:40 +0100 Subject: [PATCH] chore: updated README and tests and gomod --- .gitignore | 70 +++++++++++++++++++++++++++++++ README.md | 44 +++++++++++++++---- go.mod | 6 +++ go.sum | 13 ++++++ pkg/stackdriver/example_test.go | 29 ------------- pkg/stackdriver/formatter_test.go | 6 +-- pkg/stackdriver/stackskip_test.go | 6 +-- 7 files changed, 131 insertions(+), 43 deletions(-) create mode 100644 .gitignore create mode 100644 go.sum delete mode 100644 pkg/stackdriver/example_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb64650 --- /dev/null +++ b/.gitignore @@ -0,0 +1,70 @@ +# Binaries for programs and plugins +*.exe +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +debug + +# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 +.glide/ + +.idea/ +*.iml + +**/.settings/ +.project +.classpath + +**/target/ + +.vscode/ + +#Various temporary created while building/running jx. +/build/ +/release/ +dist/ +scanning/ +.updatebot-repos/ +.terraform/ +myvalues.yaml +myvalues.yml +**/extraValues.yaml +changelog.md + +# vim swap files +*.swp + +#OSX system files. +**/.DS_Store + +vendor/** + +# if you run `jx install --gitops` locally +jenkins-x-dev-environment + +# generated apidocs +docs/apidocs/build +docs/apidocs/includes +docs/apidocs/site + +# ignore these files 1 by 1 so we can not ignore the stylesheet override +docs/apidocs/static/FontAwesome.otf +docs/apidocs/static/bootstrap-3.3.7.min.js +docs/apidocs/static/bootstrap.min.css +docs/apidocs/static/font-awesome.min.css +docs/apidocs/static/fontawesome-webfont.eot +docs/apidocs/static/fontawesome-webfont.svg +docs/apidocs/static/fontawesome-webfont.ttf +docs/apidocs/static/fontawesome-webfont.woff +docs/apidocs/static/fontawesome-webfont.woff2 +docs/apidocs/static/jquery-3.2.1.min.js +docs/apidocs/static/jquery.scrollTo.min.js +docs/apidocs/static/scroll.js +pkg/jx/cmd/verify-pod.log diff --git a/README.md b/README.md index 072e578..a466650 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,5 @@ # logrus-stackdriver-formatter -[![Build Status](https://travis-ci.org/TV4/logrus-stackdriver-formatter.svg?branch=master)](https://travis-ci.org/TV4/logrus-stackdriver-formatter) -[![Go Report Card](https://goreportcard.com/badge/github.com/TV4/logrus-stackdriver-formatter)](https://goreportcard.com/report/github.com/TV4/logrus-stackdriver-formatter) -[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/TV4/logrus-stackdriver-formatter) -[![License MIT](https://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat)](https://github.com/TV4/logrus-stackdriver-formatter#license) - [logrus](https://github.com/sirupsen/logrus) formatter for Stackdriver. 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/). @@ -12,7 +7,7 @@ In addition to supporting level-based logging to Stackdriver, for Error, Fatal a ## Installation ```shell -go get -u github.com/TV4/logrus-stackdriver-formatter +go get -u github.com/jenkins-x/logrus-stackdriver-formatter ``` ## Usage @@ -22,7 +17,7 @@ package main import ( "github.com/sirupsen/logrus" - stackdriver "github.com/TV4/logrus-stackdriver-formatter" + stackdriver "github.com/jenkins-x/logrus-stackdriver-formatter" ) var log = logrus.New() @@ -50,7 +45,7 @@ Here's a sample entry (prettified) from the example: "severity": "ERROR", "context": { "reportLocation": { - "filePath": "github.com/TV4/logrus-stackdriver-formatter/example_test.go", + "filePath": "github.com/jenkins-x/logrus-stackdriver-formatter/example_test.go", "lineNumber": 21, "functionName": "ExampleLogError" } @@ -58,6 +53,39 @@ Here's a sample entry (prettified) from the example: } ``` +Here's an example test: +```golang +package stackdriver + +import ( + "os" + "strconv" + + stackdriver "github.com/jenkins-x/logrus-stackdriver-formatter" + "github.com/sirupsen/logrus" +) + +func ExampleLogError() { + logger := logrus.New() + logger.Out = os.Stdout + logger.Formatter = stackdriver.NewFormatter( + stackdriver.WithService("test-service"), + stackdriver.WithVersion("v0.1.0"), + ) + + logger.Info("application up and running") + + _, err := strconv.ParseInt("text", 10, 64) + if err != nil { + logger.WithError(err).Errorln("unable to parse integer") + } + + // Output: + // {"message":"application up and running","severity":"INFO","context":{}} + // {"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/jenkins-x/logrus-stackdriver-formatter/example_test.go","lineNumber":23,"functionName":"ExampleLogError"}}} +} +``` + ## HTTP request context If you'd like to add additional context like the `httpRequest`, here's a convenience function for creating a HTTP logger: diff --git a/go.mod b/go.mod index 34e96f2..8122dac 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module github.com/jenkins-x/logrus-stackdriver-formatter go 1.13 + +require ( + github.com/go-stack/stack v1.8.0 + github.com/kr/pretty v0.2.0 + github.com/sirupsen/logrus v1.5.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2628179 --- /dev/null +++ b/go.sum @@ -0,0 +1,13 @@ +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= +github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q= +github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/pkg/stackdriver/example_test.go b/pkg/stackdriver/example_test.go deleted file mode 100644 index 6650f1c..0000000 --- a/pkg/stackdriver/example_test.go +++ /dev/null @@ -1,29 +0,0 @@ -package stackdriver - -import ( - "os" - "strconv" - - stackdriver "github.com/jenkins-x/logrus-stackdriver-formatter" - "github.com/sirupsen/logrus" -) - -func ExampleLogError() { - logger := logrus.New() - logger.Out = os.Stdout - logger.Formatter = stackdriver.NewFormatter( - stackdriver.WithService("test-service"), - stackdriver.WithVersion("v0.1.0"), - ) - - logger.Info("application up and running") - - _, err := strconv.ParseInt("text", 10, 64) - if err != nil { - logger.WithError(err).Errorln("unable to parse integer") - } - - // Output: - // {"message":"application up and running","severity":"INFO","context":{}} - // {"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"}}} -} diff --git a/pkg/stackdriver/formatter_test.go b/pkg/stackdriver/formatter_test.go index 5a5f1c2..70b7ca9 100644 --- a/pkg/stackdriver/formatter_test.go +++ b/pkg/stackdriver/formatter_test.go @@ -72,7 +72,7 @@ var formatterTests = []struct { "foo": "bar", }, "reportLocation": map[string]interface{}{ - "filePath": "github.com/TV4/logrus-stackdriver-formatter/formatter_test.go", + "filePath": "github.com/jenkins-x/logrus-stackdriver-formatter/formatter_test.go", "lineNumber": 59.0, "functionName": "glob..func2", }, @@ -98,7 +98,7 @@ var formatterTests = []struct { "foo": "bar", }, "reportLocation": map[string]interface{}{ - "filePath": "github.com/TV4/logrus-stackdriver-formatter/formatter_test.go", + "filePath": "github.com/jenkins-x/logrus-stackdriver-formatter/formatter_test.go", "lineNumber": 85.0, "functionName": "glob..func3", }, @@ -131,7 +131,7 @@ var formatterTests = []struct { "method": "GET", }, "reportLocation": map[string]interface{}{ - "filePath": "github.com/TV4/logrus-stackdriver-formatter/formatter_test.go", + "filePath": "github.com/jenkins-x/logrus-stackdriver-formatter/formatter_test.go", "lineNumber": 115.0, "functionName": "glob..func4", }, diff --git a/pkg/stackdriver/stackskip_test.go b/pkg/stackdriver/stackskip_test.go index a5d20a5..08e755b 100644 --- a/pkg/stackdriver/stackskip_test.go +++ b/pkg/stackdriver/stackskip_test.go @@ -8,7 +8,7 @@ import ( "reflect" "testing" - "github.com/jenkins-x/logrus-stackdriver-formatter" + "github.com/jenkins-x/logrus-stackdriver-formatter/test" "github.com/kr/pretty" "github.com/sirupsen/logrus" ) @@ -21,7 +21,7 @@ func TestStackSkip(t *testing.T) { logger.Formatter = NewFormatter( WithService("test"), WithVersion("0.1"), - WithStackSkip("github.com/TV4/logrus-stackdriver-formatter/test"), + WithStackSkip("github.com/jenkins-x/logrus-stackdriver-formatter/test"), ) mylog := test.LogWrapper{ @@ -42,7 +42,7 @@ func TestStackSkip(t *testing.T) { }, "context": map[string]interface{}{ "reportLocation": map[string]interface{}{ - "filePath": "github.com/TV4/logrus-stackdriver-formatter/stackskip_test.go", + "filePath": "github.com/jenkins-x/logrus-stackdriver-formatter/stackskip_test.go", "lineNumber": 29.0, "functionName": "TestStackSkip", },