log: improve logging

- add a panic recovery handler
        - add logs on unexpected error paths
        - use logger's panic method
This commit is contained in:
Ramkumar Chinchani
2019-11-25 14:33:58 -08:00
parent 3e7ca9c517
commit 9ae9e40b67
13 changed files with 75 additions and 25 deletions
+2
View File
@@ -7,6 +7,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//errors:go_default_library",
"//pkg/log:go_default_library",
"@com_github_gofrs_uuid//:go_default_library",
"@com_github_opencontainers_go_digest//:go_default_library",
"@com_github_opencontainers_image_spec//specs-go/v1:go_default_library",
@@ -21,6 +22,7 @@ go_test(
embed = [":go_default_library"],
race = "on",
deps = [
"//pkg/log:go_default_library",
"@com_github_opencontainers_go_digest//:go_default_library",
"@com_github_opencontainers_image_spec//specs-go/v1:go_default_library",
"@com_github_rs_zerolog//:go_default_library",
+6 -7
View File
@@ -10,6 +10,7 @@ import (
"sync"
"github.com/anuvu/zot/errors"
zlog "github.com/anuvu/zot/pkg/log"
guuid "github.com/gofrs/uuid"
godigest "github.com/opencontainers/go-digest"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -32,7 +33,7 @@ type ImageStore struct {
log zerolog.Logger
}
func NewImageStore(rootDir string, log zerolog.Logger) *ImageStore {
func NewImageStore(rootDir string, log zlog.Logger) *ImageStore {
is := &ImageStore{rootDir: rootDir,
lock: &sync.Mutex{},
blobUploads: make(map[string]BlobUpload),
@@ -69,11 +70,10 @@ func (is *ImageStore) InitRepo(name string) error {
il := ispec.ImageLayout{Version: ispec.ImageLayoutVersion}
buf, err := json.Marshal(il)
if err != nil {
panic(err)
is.log.Panic().Err(err).Msg("unable to marshal JSON")
}
if err := ioutil.WriteFile(ilPath, buf, 0644); err != nil {
is.log.Error().Err(err).Str("file", ilPath).Msg("unable to write file")
panic(err)
is.log.Panic().Err(err).Str("file", ilPath).Msg("unable to write file")
}
}
@@ -84,11 +84,10 @@ func (is *ImageStore) InitRepo(name string) error {
index.SchemaVersion = 2
buf, err := json.Marshal(index)
if err != nil {
panic(err)
is.log.Panic().Err(err).Msg("unable to marshal JSON")
}
if err := ioutil.WriteFile(indexPath, buf, 0644); err != nil {
is.log.Error().Err(err).Str("file", indexPath).Msg("unable to write file")
panic(err)
is.log.Panic().Err(err).Str("file", indexPath).Msg("unable to write file")
}
}
+2 -1
View File
@@ -8,6 +8,7 @@ import (
"os"
"testing"
"github.com/anuvu/zot/pkg/log"
"github.com/anuvu/zot/pkg/storage"
godigest "github.com/opencontainers/go-digest"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -22,7 +23,7 @@ func TestAPIs(t *testing.T) {
}
defer os.RemoveAll(dir)
il := storage.NewImageStore(dir, zerolog.New(os.Stdout))
il := storage.NewImageStore(dir, log.Logger{Logger: zerolog.New(os.Stdout)})
Convey("Repo layout", t, func(c C) {
repoName := "test"