Skip to content

Commit

Permalink
Merge pull request #3 from cagiti/master
Browse files Browse the repository at this point in the history
chore: updated README and tests and gomod
  • Loading branch information
cagiti authored Apr 9, 2020
2 parents 9c60d86 + e857914 commit 681e031
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 43 deletions.
70 changes: 70 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
# 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/).

## Installation

```shell
go get -u github.com/TV4/logrus-stackdriver-formatter
go get -u github.com/jenkins-x/logrus-stackdriver-formatter
```

## Usage
Expand All @@ -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()
Expand Down Expand Up @@ -50,14 +45,47 @@ 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"
}
}
}
```

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:
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
29 changes: 0 additions & 29 deletions pkg/stackdriver/example_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/stackdriver/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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",
},
Expand Down Expand Up @@ -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",
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/stackdriver/stackskip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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{
Expand All @@ -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",
},
Expand Down

0 comments on commit 681e031

Please sign in to comment.