Skip to content

Commit

Permalink
Logger interface (#56)
Browse files Browse the repository at this point in the history
* Make Logger accept an interface
  • Loading branch information
markphelps authored Apr 5, 2019
1 parent c9ca628 commit b994f1a
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 45 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.gitignore
.github/
LICENSE
bin/

*.txt
*.yml
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 0.5.0 - 2019-04-05

### Changed

- Allow `Logger()` option to accept interface `StdLogger` instead of enforcing a specific logger implementation

## 0.4.0 - 2019-02-01

### Added
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GOTOOLS = \
golang.org/x/tools/cmd/cover \
github.com/golangci/golangci-lint/cmd/golangci-lint \
github.com/golangci/golangci-lint/cmd/golangci-lint@de1d1ad \

GOPACKAGES := $(go list ./... | grep -v /vendor/)
VERSION ?= $(shell git describe --abbrev=0 --tags)
Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,24 @@ auth := codeship.NewBasicAuth("username", "password")
client, err := codeship.New(auth, codeship.Verbose(true))
```

The default logger logs to STDOUT but can be replaced by any instance of `*log.Logger`:
### Bring your own Logger

The default logger logs to STDOUT but can be replaced by any type that fulfills the `StdLogger` interface:

```go
// StdLogger allows you to bring your own log implementation for logging
type StdLogger interface {
Println(...interface{})
}
```

Example:

```go
import "github.com/sirupsen/logrus"

var (
buf bytes.Buffer
logger = log.New(&buf, "INFO: ", log.Lshortfile)
logger = logrus.New()
auth = codeship.NewBasicAuth("username", "password")
)

Expand Down
3 changes: 0 additions & 3 deletions codeship-services.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
integration:
build: ./
encrypted_env_file: integration.env.encrypted
cached: true

test:
build: ./
encrypted_env_file: env.encrypted
cached: true
command: ./scripts/cover

gov:
build:
context: .
dockerfile: ./docker/gov/Dockerfile
cached: true
15 changes: 12 additions & 3 deletions codeship.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,22 @@ func newResponse(r *http.Response) Response {

const apiURL = "https://api.codeship.com/v2"

// StdLogger allows you to bring your own log implementation for logging
type StdLogger interface {
Println(...interface{})
}

// Won't compile if StdLogger can't be realized by a log.Logger
var _ StdLogger = &log.Logger{}

// Client holds information necessary to make a request to the Codeship API
type Client struct {
baseURL string
authenticator Authenticator
authentication Authentication
headers http.Header
httpClient *http.Client
logger *log.Logger
logger StdLogger
verbose bool
}

Expand Down Expand Up @@ -126,8 +134,9 @@ func New(auth Authenticator, opts ...Option) (*Client, error) {
// Fall back to default log.Logger (STDOUT) if the user does not provide
// their own
if client.logger == nil {
client.logger = &log.Logger{}
client.logger.SetOutput(os.Stdout)
logger := &log.Logger{}
logger.SetOutput(os.Stdout)
client.logger = logger
}

return client, nil
Expand Down
50 changes: 48 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
module github.com/codeship/codeship-go

require (
github.com/golangci/golangci-lint v1.13.2 // indirect
github.com/OpenPeeDeeP/depguard v0.0.0-20181229194401-1f388ab2d810 // indirect
github.com/StackExchange/wmi v0.0.0-20181212234831-e0a55b97c705 // indirect
github.com/coreos/etcd v3.3.12+incompatible // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/go-critic/go-critic v0.3.4 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/go-toolsmith/astcast v1.0.0 // indirect
github.com/go-toolsmith/astcopy v1.0.0 // indirect
github.com/go-toolsmith/astfmt v1.0.0 // indirect
github.com/go-toolsmith/astp v1.0.0 // indirect
github.com/go-toolsmith/pkgload v1.0.0 // indirect
github.com/go-toolsmith/typep v1.0.0 // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/golang/mock v1.2.0 // indirect
github.com/golang/protobuf v1.3.1 // indirect
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6 // indirect
github.com/golangci/go-tools v0.0.0-20190124090046-35a9f45a5db0 // indirect
github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d // indirect
github.com/golangci/gofmt v0.0.0-20181222123516-0b8337e80d98 // indirect
github.com/golangci/golangci-lint v1.16.1-0.20190402065613-de1d1ad903cd
github.com/golangci/gosec v0.0.0-20180901114220-8afd9cbb6cfb // indirect
github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219 // indirect
github.com/golangci/revgrep v0.0.0-20180812185044-276a5c0a1039 // indirect
github.com/kisielk/errcheck v1.2.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pty v1.1.4 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.7 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d // indirect
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect
github.com/pelletier/go-toml v1.3.0 // indirect
github.com/pkg/errors v0.8.1
github.com/rogpeppe/go-internal v1.3.0 // indirect
github.com/shirou/gopsutil v2.18.12+incompatible // indirect
github.com/shurcooL/go v0.0.0-20190330031554-6713ea532688 // indirect
github.com/sirupsen/logrus v1.4.1
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cobra v0.0.3 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.3.2 // indirect
github.com/stretchr/testify v1.3.0
golang.org/x/tools v0.0.0-20190131163942-067a2f313b69 // indirect
github.com/ugorji/go/codec v0.0.0-20190320090025-2dc34c0b8780 // indirect
golang.org/x/crypto v0.0.0-20190404164418-38d8ce5564a5 // indirect
golang.org/x/net v0.0.0-20190403144856-b630fd6fe46b // indirect
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 // indirect
golang.org/x/tools v0.0.0-20190404132500-923d25813098
mvdan.cc/unparam v0.0.0-20190310220240-1b9ccfa71afe // indirect
sourcegraph.com/sqs/pbtypes v1.0.0 // indirect
)
Loading

0 comments on commit b994f1a

Please sign in to comment.