Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add customTags in logger middleware Config
- Loading branch information
1 parent
24a6170
commit 78e1d4c
Showing
5 changed files
with
190 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package logger | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/fiber/v2/internal/bytebufferpool" | ||
) | ||
|
||
type LogFunc func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) | ||
|
||
// tagMap use to storage how to log content when parse Tag. | ||
var tagMap = map[string]LogFunc{ | ||
TagReferer: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.Get(fiber.HeaderReferer)) | ||
}, | ||
TagProtocol: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.Protocol()) | ||
}, | ||
TagPort: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.Port()) | ||
|
||
}, | ||
TagIP: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.IP()) | ||
}, | ||
TagIPs: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.Get(fiber.HeaderXForwardedFor)) | ||
}, | ||
TagHost: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.Hostname()) | ||
}, | ||
TagPath: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.Path()) | ||
}, | ||
TagURL: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.OriginalURL()) | ||
}, | ||
TagUA: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.Get(fiber.HeaderUserAgent)) | ||
}, | ||
TagBody: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.Write(c.Body()) | ||
}, | ||
TagBytesReceived: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return appendInt(buf, len(c.Request().Body())) | ||
}, | ||
TagBytesSent: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return appendInt(buf, len(c.Response().Body())) | ||
}, | ||
TagRoute: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.Route().Path) | ||
}, | ||
TagResBody: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.Write(c.Response().Body()) | ||
}, | ||
TagReqHeaders: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
reqHeaders := make([]string, 0) | ||
for k, v := range c.GetReqHeaders() { | ||
reqHeaders = append(reqHeaders, k+"="+v) | ||
} | ||
return buf.Write([]byte(strings.Join(reqHeaders, "&"))) | ||
}, | ||
TagQueryStringParams: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.Request().URI().QueryArgs().String()) | ||
}, | ||
TagBlack: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.App().Config().ColorScheme.Black) | ||
}, | ||
TagRed: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.App().Config().ColorScheme.Red) | ||
}, | ||
TagGreen: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.App().Config().ColorScheme.Green) | ||
}, | ||
TagYellow: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.App().Config().ColorScheme.Yellow) | ||
}, | ||
TagBlue: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.App().Config().ColorScheme.Blue) | ||
}, | ||
TagMagenta: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.App().Config().ColorScheme.Magenta) | ||
}, | ||
TagCyan: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.App().Config().ColorScheme.Cyan) | ||
}, | ||
TagWhite: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.App().Config().ColorScheme.White) | ||
}, | ||
TagReset: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
return buf.WriteString(c.App().Config().ColorScheme.Reset) | ||
}, | ||
TagError: func(buf *bytebufferpool.ByteBuffer, c *fiber.Ctx, w io.Writer, tag string) (int, error) { | ||
if err := c.Next(); err != nil { | ||
return buf.WriteString(err.Error()) | ||
} | ||
return buf.WriteString("-") | ||
}, | ||
} |