Skip to content

Commit

Permalink
fix(logger): print to stderr if log fails for default format (#2830)
Browse files Browse the repository at this point in the history
We log to stderr if logging fails when a custom format is used, but not
for the default format. This change addresses this inconsistency.
  • Loading branch information
nickajacks1 authored Feb 5, 2024
1 parent 93c726f commit 926c537
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
24 changes: 15 additions & 9 deletions middleware/logger/default_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logger

import (
"fmt"
"io"
"os"
"sync"

Expand Down Expand Up @@ -60,7 +61,7 @@ func defaultLoggerInstance(c fiber.Ctx, data *Data, cfg Config) error {
}

// Write buffer to output
_, _ = cfg.Output.Write(buf.Bytes()) //nolint:errcheck // This will never fail
writeLog(cfg.Output, buf.Bytes())

if cfg.Done != nil {
cfg.Done(c, buf.Bytes())
Expand Down Expand Up @@ -92,15 +93,9 @@ func defaultLoggerInstance(c fiber.Ctx, data *Data, cfg Config) error {
if err != nil {
_, _ = buf.WriteString(err.Error()) //nolint:errcheck // This will never fail
}

mu.Lock()
// Write buffer to output
if _, err := cfg.Output.Write(buf.Bytes()); err != nil {
// Write error to output
if _, err := cfg.Output.Write([]byte(err.Error())); err != nil {
// There is something wrong with the given io.Writer
_, _ = fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
}
}
writeLog(cfg.Output, buf.Bytes())
mu.Unlock()

if cfg.Done != nil {
Expand Down Expand Up @@ -129,3 +124,14 @@ func appendInt(output Buffer, v int) (int, error) {
output.Set(fasthttp.AppendUint(output.Bytes(), v))
return output.Len() - old, nil
}

// writeLog writes a msg to w, printing a warning to stderr if the log fails.
func writeLog(w io.Writer, msg []byte) {
if _, err := w.Write(msg); err != nil {
// Write error to output
if _, err := w.Write([]byte(err.Error())); err != nil {
// There is something wrong with the given io.Writer
_, _ = fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
}
}
}
23 changes: 14 additions & 9 deletions middleware/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,17 @@ func Test_Logger_ErrorTimeZone(t *testing.T) {
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
}

type fakeOutput int
type fakeErrorOutput int

func (o *fakeOutput) Write([]byte) (int, error) {
func (o *fakeErrorOutput) Write([]byte) (int, error) {
*o++
return 0, errors.New("fake output")
}

// go test -run Test_Logger_ErrorOutput_WithoutColor
func Test_Logger_ErrorOutput_WithoutColor(t *testing.T) {
t.Parallel()
o := new(fakeOutput)
o := new(fakeErrorOutput)
app := fiber.New()
app.Use(New(Config{
Output: o,
Expand All @@ -163,14 +163,13 @@ func Test_Logger_ErrorOutput_WithoutColor(t *testing.T) {
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
require.NoError(t, err)
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)

require.Equal(t, 1, int(*o))
require.EqualValues(t, 2, *o)
}

// go test -run Test_Logger_ErrorOutput
func Test_Logger_ErrorOutput(t *testing.T) {
t.Parallel()
o := new(fakeOutput)
o := new(fakeErrorOutput)
app := fiber.New()
app.Use(New(Config{
Output: o,
Expand All @@ -179,7 +178,7 @@ func Test_Logger_ErrorOutput(t *testing.T) {
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
require.NoError(t, err)
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
require.Equal(t, 1, int(*o))
require.EqualValues(t, 2, *o)
}

// go test -run Test_Logger_All
Expand Down Expand Up @@ -634,6 +633,13 @@ func Test_Logger_ByteSent_Streaming(t *testing.T) {
require.Equal(t, "0 0 200", buf.String())
}

type fakeOutput int

func (o *fakeOutput) Write(b []byte) (int, error) {
*o++
return len(b), nil
}

// go test -run Test_Logger_EnableColors
func Test_Logger_EnableColors(t *testing.T) {
t.Parallel()
Expand All @@ -646,6 +652,5 @@ func Test_Logger_EnableColors(t *testing.T) {
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
require.NoError(t, err)
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)

require.Equal(t, 1, int(*o))
require.EqualValues(t, 1, *o)
}

0 comments on commit 926c537

Please sign in to comment.