Skip to content

Commit

Permalink
added logging configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronan Barrett committed Feb 10, 2020
1 parent f998a02 commit 23c048c
Show file tree
Hide file tree
Showing 21 changed files with 1,333 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ For more details, see the examples.

- [minimal](./examples/minimal/main.go): `go run ./examples/minimal`

## Configuration

### Logging
The default format is generic JSON. The log format can be configured by setting the `LOG_FORMAT` environment variable. The supported formats are:
- "stackdriver"
- "console"

## Contributions

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/voi-go/svc

require (
github.com/blendle/zapdriver v1.3.1
github.com/caarlos0/env v3.5.0+incompatible
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
Expand All @@ -9,7 +10,6 @@ require (
github.com/leodido/go-urn v1.2.0 // indirect
github.com/prometheus/client_golang v1.2.1
github.com/stretchr/testify v1.4.0
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.2.0 // indirect
go.uber.org/zap v1.11.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/blendle/zapdriver v1.3.1 h1:C3dydBOWYRiOk+B8X9IVZ5IOe+7cl+tGOexN4QqHfpE=
github.com/blendle/zapdriver v1.3.1/go.mod h1:mdXfREi6u5MArG4j9fewC+FGnXaBR+T4Ox4J2u4eHCc=
github.com/caarlos0/env v3.5.0+incompatible h1:Yy0UN8o9Wtr/jGHZDpCBLpNrzcFLLM2yixi/rBrKyJs=
github.com/caarlos0/env v3.5.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y=
github.com/cespare/xxhash/v2 v2.1.0 h1:yTUvW7Vhb89inJ+8irsUqiWjh8iT6sQPZiQzI6ReGkA=
Expand Down Expand Up @@ -83,8 +85,10 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.2.0 h1:6I+W7f5VwC5SV9dNrZ3qXrDB9mD0dyGOi/ZJmYw03T4=
go.uber.org/multierr v1.2.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.11.0 h1:gSmpCfs+R47a4yQPAI4xJ0IPDLTRGXskm6UelqNXpqE=
go.uber.org/zap v1.11.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
30 changes: 28 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,51 @@ package svc

import (
"os"
"strings"
"time"

"github.com/blendle/zapdriver"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func newLogger(name, version string, level zapcore.Level) (*zap.Logger, zap.AtomicLevel) {
logFormat := strings.ToLower(os.Getenv("LOG_FORMAT"))

atom := zap.NewAtomicLevel()
atom.SetLevel(level)

// stackdriver
if logFormat == "stackdriver" {
logger := zap.New(zapcore.NewSampler(zapcore.NewCore(
zapcore.NewJSONEncoder(zapdriver.NewProductionEncoderConfig()),
zapcore.Lock(os.Stdout),
atom,
), time.Second, 100, 10),
zap.ErrorOutput(zapcore.Lock(os.Stderr)),
zap.AddCaller(),
)
logger = logger.With(zapdriver.ServiceContext(name), zapdriver.Label("version", version))
return logger, atom
}

// console text logger or fallback to default generic JSON
var enc zapcore.Encoder
if logFormat == "console" {
config := zap.NewProductionEncoderConfig()
config.EncodeTime = zapcore.RFC3339TimeEncoder
enc = zapcore.NewConsoleEncoder(config)
} else {
enc = zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
}
logger := zap.New(zapcore.NewSampler(zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
enc,
zapcore.Lock(os.Stdout),
atom,
), time.Second, 100, 10),
zap.ErrorOutput(zapcore.Lock(os.Stderr)),
zap.AddCaller(),
zap.Fields(zap.String("app", name), zap.String("version", version)),
)

return logger, atom
}
1 change: 1 addition & 0 deletions vendor/github.com/blendle/zapdriver/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/blendle/zapdriver/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 23c048c

Please sign in to comment.