fix(logger): print to stderr if log fails for default format (#2830)

We log to stderr if logging fails when a custom format is used, but not
for the default format. This change addresses this inconsistency.
pull/2835/head
nickajacks1 2024-02-04 23:16:34 -08:00 committed by GitHub
parent 93c726fb76
commit 926c537252
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 18 deletions

View File

@ -2,6 +2,7 @@ package logger
import (
"fmt"
"io"
"os"
"sync"
@ -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())
@ -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 {
@ -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)
}
}
}

View File

@ -143,9 +143,9 @@ 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")
}
@ -153,7 +153,7 @@ func (o *fakeOutput) Write([]byte) (int, error) {
// 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,
@ -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,
@ -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
@ -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()
@ -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)
}