Skip to content

Commit

Permalink
Migrate logrus to zap and implement proper logging
Browse files Browse the repository at this point in the history
  • Loading branch information
albertus.raharja authored and ajatprabha committed Jul 1, 2019
1 parent 0d95358 commit 7cdd95f
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 186 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
out/

# config files
config.yaml
config.yaml

coverage.json
config.env
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Create a file containing the environment variables mentioned in [`config.yaml.ex
```bash
DEBUG=true
LOG_LEVEL=debug
LOG_FORMAT=json

APP_NAME=darkroom
APP_VERSION=0.0.1
Expand Down
1 change: 1 addition & 0 deletions cmd/darkroom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func main() {
}

if err := a.Run(os.Args); err != nil {
logger.Errorf("got an error while running main with error: %s", err)
panic(err)
}
}
1 change: 0 additions & 1 deletion config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ debug: "true"

log:
level: "debug"
format: "text"

app:
name: "darkroom"
Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
github.com/anthonynsimon/bild v0.10.0
github.com/aws/aws-sdk-go v1.20.0
github.com/axw/gocov v0.0.0-20170322000131-3a69a0d2a4ef // indirect
github.com/axw/gocov v1.0.0 // indirect
github.com/cactus/go-statsd-client/statsd v0.0.0-20190501063751-9a7692639588
github.com/gojektech/heimdall v5.0.2+incompatible
github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c // indirect
Expand All @@ -19,10 +19,11 @@ require (
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.3.0
github.com/urfave/cli v1.20.0
golang.org/x/crypto v0.0.0-20190617133340-57b3e21c3d56 // indirect
go.uber.org/zap v1.10.0
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443 // indirect
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 // indirect
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 // indirect
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f // indirect
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b // indirect
golang.org/x/sys v0.0.0-20190620070143-6f217b454f45 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20190617190820-da514acc4774 // indirect
golang.org/x/tools v0.0.0-20190619215442-4adf7a708c2d // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/aws/aws-sdk-go v1.19.41/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpi
github.com/aws/aws-sdk-go v1.20.0 h1:t74VM7opfCwwbe+wg6eys4a2wLqky6Znitr7BsqYPUg=
github.com/aws/aws-sdk-go v1.20.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/axw/gocov v0.0.0-20170322000131-3a69a0d2a4ef/go.mod h1:pc6XrbIn8RLeVSNzXCZKXNst+RTE5Ju/nySYl1Wc0B4=
github.com/axw/gocov v1.0.0 h1:YsqYR66hUmilVr23tu8USgnJIJvnwh3n7j5zRn7x4LU=
github.com/axw/gocov v1.0.0/go.mod h1:LvQpEYiwwIb2nYkXY2fDWhg9/AsYqkhmrCshjlUJECE=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/cactus/go-statsd-client/statsd v0.0.0-20190501063751-9a7692639588 h1:6yVhh6P5OsW6HutPt7z2ggDgZczgUtSl2kGRe+DslPU=
Expand Down Expand Up @@ -134,14 +136,18 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
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 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190617133340-57b3e21c3d56/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 h1:QzoH/1pFpZguR8NrRHLcO6jKqfv2zpuSqZLgdm7ZmjI=
Expand All @@ -156,6 +162,7 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65 h1:+rhAzEzT3f4JtomfC371qB+0O
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 h1:dfGZHvZk057jK2MCeWus/TowKpJ8y4AmooUzdBSR9GU=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -175,6 +182,8 @@ golang.org/x/sys v0.0.0-20190614084037-d442b75600c5 h1:tQrtnaPeNyfkuD2UMixVD6lAa
golang.org/x/sys v0.0.0-20190614084037-d442b75600c5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f h1:25KHgbfyiSm6vwQLbM3zZIe1v9p/3ea4Rz+nnM5K/i4=
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190620070143-6f217b454f45 h1:Dl2hc890lrizvUppGbRWhnIh2f8jOTCQpY5IKWRS0oM=
golang.org/x/sys v0.0.0-20190620070143-6f217b454f45/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
Expand All @@ -190,6 +199,8 @@ golang.org/x/tools v0.0.0-20190613204242-ed0dc450797f h1:+zypR5600WBcnJgA2nzZAsB
golang.org/x/tools v0.0.0-20190613204242-ed0dc450797f/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190617190820-da514acc4774 h1:CQVOmarCBFzTx0kbOU0ru54Cvot8SdSrNYjZPhQl+gk=
golang.org/x/tools v0.0.0-20190617190820-da514acc4774/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190619215442-4adf7a708c2d h1:LQ06Vbju+Kwbcd94hb+6CgDsWoj/e7GOLPcYzHrG+iI=
golang.org/x/tools v0.0.0-20190619215442-4adf7a708c2d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
Expand Down
5 changes: 3 additions & 2 deletions internal/handler/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ const (

func ImageHandler(deps *service.Dependencies) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := logger.SugaredWithRequest(r)
res := deps.Storage.Get(r.Context(), r.URL.Path)
if res.Error() != nil {
logger.Errorf("error: %s", res.Error())
l.Errorf("error from Storage.Get: %s", res.Error())
metrics.Update(metrics.UpdateOption{Name: StorageGetErrorKey, Type: metrics.Count})
w.WriteHeader(res.Status())
return
Expand All @@ -40,7 +41,7 @@ func ImageHandler(deps *service.Dependencies) http.HandlerFunc {
}
data, err = deps.Manipulator.Process(r.Context(), data, params)
if err != nil {
logger.Errorf("error: %s", res.Error())
l.Errorf("error from Manipulator.Process: %s", err)
metrics.Update(metrics.UpdateOption{Name: ProcessorErrorKey, Type: metrics.Count})
w.WriteHeader(http.StatusUnprocessableEntity)
return
Expand Down
5 changes: 1 addition & 4 deletions internal/handler/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ type mockManipulator struct {

func (m *mockManipulator) Process(ctx context.Context, data []byte, params map[string]string) ([]byte, error) {
args := m.Called(ctx, data, params)
if args.Get(1) == nil {
return args.Get(0).([]byte), nil
}
return args.Get(0).([]byte), args.Get(1).(error)
return args.Get(0).([]byte), args.Error(1)
}

type mockStorage struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func Ping() http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("pong"))
return
Expand Down
13 changes: 3 additions & 10 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type config struct {
logger loggerConfig
logLevel string
app app
debugMode bool
port int
Expand Down Expand Up @@ -48,10 +48,7 @@ func newConfig() *config {
s.readValue()

return &config{
logger: loggerConfig{
level: v.GetString("log.level"),
format: v.GetString("log.format"),
},
logLevel: v.GetString("log.level"),
app: app{
name: v.GetString("app.Name"),
version: v.GetString("app.version"),
Expand All @@ -69,11 +66,7 @@ func Update() {
}

func LogLevel() string {
return getConfig().logger.level
}

func LogFormat() string {
return getConfig().logger.format
return getConfig().logLevel
}

func AppName() string {
Expand Down
4 changes: 0 additions & 4 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ func TestConfigCases(t *testing.T) {
key: "log.level",
callFunc: LogLevel,
},
{
key: "log.format",
callFunc: LogFormat,
},
}

for _, c := range cases {
Expand Down
5 changes: 0 additions & 5 deletions pkg/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import (
"***REMOVED***/darkroom/core/pkg/storage"
)

type loggerConfig struct {
level string
format string
}

type app struct {
name string
version string
Expand Down
Loading

0 comments on commit 7cdd95f

Please sign in to comment.