Skip to content

Commit

Permalink
feat(log): print traceback when panics occur
Browse files Browse the repository at this point in the history
Signed-off-by: Petu Eusebiu <[email protected]>
  • Loading branch information
eusebiu-constantin-petu-dbk committed Dec 21, 2023
1 parent 59f41ac commit 02b74ac
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
5 changes: 3 additions & 2 deletions pkg/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ func (c *Controller) Run() error {

engine.Use(
SessionLogger(c),
handlers.RecoveryHandler(handlers.RecoveryLogger(c.Log),
handlers.PrintRecoveryStack(false)))
handlers.RecoveryHandler(
handlers.PrintRecoveryStack(true),
))

if c.Audit != nil {
engine.Use(SessionAuditLogger(c.Audit))
Expand Down
25 changes: 25 additions & 0 deletions pkg/api/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import (
"zotregistry.io/zot/pkg/storage"
storageConstants "zotregistry.io/zot/pkg/storage/constants"
"zotregistry.io/zot/pkg/storage/gc"
storageTypes "zotregistry.io/zot/pkg/storage/types"
authutils "zotregistry.io/zot/pkg/test/auth"
test "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/deprecated"
Expand Down Expand Up @@ -308,6 +309,30 @@ func TestRunAlreadyRunningServer(t *testing.T) {
})
}

func TestPrintTracebackOnPanic(t *testing.T) {
Convey("Run server on unavailable port", t, func() {
port := test.GetFreePort()
baseURL := test.GetBaseURL(port)

conf := config.New()
conf.HTTP.Port = port

ctlr := makeController(conf, t.TempDir())
cm := test.NewControllerManager(ctlr)

cm.StartAndWait(port)
defer cm.StopServer()

ctlr.StoreController.SubStore = make(map[string]storageTypes.ImageStore)
ctlr.StoreController.SubStore["/a"] = nil

resp, err := resty.R().Get(baseURL + "/v2/_catalog")
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusInternalServerError)
})
}

func TestAutoPortSelection(t *testing.T) {
Convey("Run server with specifying a port", t, func() {
conf := config.New()
Expand Down
10 changes: 9 additions & 1 deletion pkg/log/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package log

import (
"fmt"
"os"
"runtime"
"strconv"
Expand All @@ -22,7 +23,14 @@ type Logger struct {
}

func (l Logger) Println(v ...interface{}) {
l.Logger.Error().Msg("panic recovered") //nolint: check-logs
for _, i := range v {
switch t := i.(type) {
case error:
l.Logger.Error().Err(t).Msg("panic recovered") //nolint: check-logs
default:
fmt.Println(t)
}
}
}

func NewLogger(level, output string) Logger {
Expand Down

0 comments on commit 02b74ac

Please sign in to comment.