From bcdc8cecd769a495bca516a0194e847e11a013eb Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Fri, 18 Nov 2022 20:51:05 +0800 Subject: [PATCH] :memo: Update middleware/logger docs Update #2219 --- middleware/logger/README.md | 96 +++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 40 deletions(-) diff --git a/middleware/logger/README.md b/middleware/logger/README.md index f6a70117110..814bc49b673 100644 --- a/middleware/logger/README.md +++ b/middleware/logger/README.md @@ -87,56 +87,72 @@ app.Use(logger.New(logger.Config{ })) ``` +### Callback after log is written + +```go +app.Use(logger.New(logger.Config{ + TimeFormat: time.RFC3339Nano, + TimeZone: "Asia/Shanghai", + Done: func(c *fiber.Ctx, logString []byte) { + if c.Response().StatusCode() != fiber.StatusOK { + reporter.SendToSlack(logString) + } + }, +})) +``` + ## Config ```go // Config defines the config for middleware. type Config struct { - // Next defines a function to skip this middleware when returned true. - // - // Optional. Default: nil - Next func(c *fiber.Ctx) bool - - // CustomTags defines the custom tag action - // - // Optional. Default: map[string]LogFunc{} - CustomTags map[string]LogFunc - - // Format defines the logging tags - // - // Optional. Default: [${time}] ${status} - ${latency} ${method} ${path}\n - Format string - - // TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html - // - // Optional. Default: 15:04:05 - TimeFormat string - - // TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc - // - // Optional. Default: "Local" - TimeZone string - - // TimeInterval is the delay before the timestamp is updated - // - // Optional. Default: 500 * time.Millisecond - TimeInterval time.Duration - - // Output is a writer where logs are written - // - // Default: os.Stderr - Output io.Writer + // Next defines a function to skip this middleware when returned true. + // + // Optional. Default: nil + Next func(c *fiber.Ctx) bool + + // Done is a function that is called after the log string for a request is written to Output, + // and pass the log string as parameter. + // + // Optional. Default: nil + Done func(c *fiber.Ctx, logString []byte) + + // Format defines the logging tags + // + // Optional. Default: [${time}] ${status} - ${latency} ${method} ${path}\n + Format string + + // TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html + // + // Optional. Default: 15:04:05 + TimeFormat string + + // TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc + // + // Optional. Default: "Local" + TimeZone string + + // TimeInterval is the delay before the timestamp is updated + // + // Optional. Default: 500 * time.Millisecond + TimeInterval time.Duration + + // Output is a writter where logs are written + // + // Default: os.Stderr + Output io.Writer } ``` ## Default Config ```go var ConfigDefault = Config{ - Next: nil, - Format: "[${time}] ${status} - ${latency} ${method} ${path}\n", - TimeFormat: "15:04:05", - TimeZone: "Local", - TimeInterval: 500 * time.Millisecond, - Output: os.Stderr, + Next: nil, + Done: nil, + Format: "[${time}] ${status} - ${latency} ${method} ${path}\n", + TimeFormat: "15:04:05", + TimeZone: "Local", + TimeInterval: 500 * time.Millisecond, + Output: os.Stderr, } ```