mirror of https://github.com/gofiber/fiber.git
parent
b0925dc454
commit
dc2d2ef524
|
@ -97,7 +97,7 @@ app.Use(logger.New(logger.Config{
|
|||
| Next | `func(*fiber.Ctx) bool` | Next defines a function to skip this middleware when returned true. | `nil` |
|
||||
| Done | `func(*fiber.Ctx, []byte)` | 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. | `nil` |
|
||||
| CustomTags | `map[string]LogFunc` | tagFunctions defines the custom tag action. | `map[string]LogFunc` |
|
||||
| Format | `string` | Format defines the logging tags. | `[${time}] ${ip} ${status} - ${latency} ${method} ${path}\n` |
|
||||
| Format | `string` | Format defines the logging tags. | `${time} | ${status} | ${latency} | ${ip} | ${method} | ${path}` |
|
||||
| TimeFormat | `string` | TimeFormat defines the time format for log timestamps. | `15:04:05` |
|
||||
| TimeZone | `string` | TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc | `"Local"` |
|
||||
| TimeInterval | `time.Duration` | TimeInterval is the delay before the timestamp is updated. | `500 * time.Millisecond` |
|
||||
|
@ -112,7 +112,7 @@ app.Use(logger.New(logger.Config{
|
|||
var ConfigDefault = Config{
|
||||
Next: nil,
|
||||
Done: nil,
|
||||
Format: "[${time}] ${ip} ${status} - ${latency} ${method} ${path}\n",
|
||||
Format: "${time} | ${status} | ${latency} | ${ip} | ${method} | ${path}",
|
||||
TimeFormat: "15:04:05",
|
||||
TimeZone: "Local",
|
||||
TimeInterval: 500 * time.Millisecond,
|
||||
|
|
|
@ -28,7 +28,7 @@ type Config struct {
|
|||
|
||||
// Format defines the logging tags
|
||||
//
|
||||
// Optional. Default: [${time}] ${ip} ${status} - ${latency} ${method} ${path}\n
|
||||
// Optional. Default: ${time} | ${status} | ${latency} | ${ip} | ${method} | ${path}
|
||||
Format string
|
||||
|
||||
// TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html
|
||||
|
@ -86,7 +86,7 @@ type LogFunc func(output Buffer, c *fiber.Ctx, data *Data, extraParam string) (i
|
|||
var ConfigDefault = Config{
|
||||
Next: nil,
|
||||
Done: nil,
|
||||
Format: "[${time}] ${ip} ${status} - ${latency} ${method} ${path}\n",
|
||||
Format: "${time} | ${status} | ${latency} | ${ip} | ${method} | ${path}\n",
|
||||
TimeFormat: "15:04:05",
|
||||
TimeZone: "Local",
|
||||
TimeInterval: 500 * time.Millisecond,
|
||||
|
|
|
@ -85,9 +85,6 @@ func New(config ...Config) fiber.Handler {
|
|||
return c.Next()
|
||||
}
|
||||
|
||||
// Alias colors
|
||||
colors := c.App().Config().ColorScheme
|
||||
|
||||
// Set error handler once
|
||||
once.Do(func() {
|
||||
// get longested possible path
|
||||
|
@ -137,56 +134,6 @@ func New(config ...Config) fiber.Handler {
|
|||
// Get new buffer
|
||||
buf := bytebufferpool.Get()
|
||||
|
||||
// Default output when no custom Format or io.Writer is given
|
||||
if cfg.Format == ConfigDefault.Format {
|
||||
// Format error if exist
|
||||
formatErr := ""
|
||||
if cfg.enableColors {
|
||||
if chainErr != nil {
|
||||
formatErr = colors.Red + " | " + chainErr.Error() + colors.Reset
|
||||
}
|
||||
_, _ = buf.WriteString( //nolint:errcheck // This will never fail
|
||||
fmt.Sprintf("%s |%s %3d %s| %13v | %15s |%s %-7s %s| %-"+errPaddingStr+"s %s\n",
|
||||
timestamp.Load().(string),
|
||||
statusColor(c.Response().StatusCode(), colors), c.Response().StatusCode(), colors.Reset,
|
||||
data.Stop.Sub(data.Start),
|
||||
c.IP(),
|
||||
methodColor(c.Method(), colors), c.Method(), colors.Reset,
|
||||
c.Path(),
|
||||
formatErr,
|
||||
),
|
||||
)
|
||||
} else {
|
||||
if chainErr != nil {
|
||||
formatErr = " | " + chainErr.Error()
|
||||
}
|
||||
_, _ = buf.WriteString( //nolint:errcheck // This will never fail
|
||||
fmt.Sprintf("%s | %3d | %13v | %15s | %-7s | %-"+errPaddingStr+"s %s\n",
|
||||
timestamp.Load().(string),
|
||||
c.Response().StatusCode(),
|
||||
data.Stop.Sub(data.Start),
|
||||
c.IP(),
|
||||
c.Method(),
|
||||
c.Path(),
|
||||
formatErr,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// Write buffer to output
|
||||
_, _ = cfg.Output.Write(buf.Bytes()) //nolint:errcheck // This will never fail
|
||||
|
||||
if cfg.Done != nil {
|
||||
cfg.Done(c, buf.Bytes())
|
||||
}
|
||||
|
||||
// Put buffer back to pool
|
||||
bytebufferpool.Put(buf)
|
||||
|
||||
// End chain
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
// Loop over template parts execute dynamic parts and add fixed parts to the buffer
|
||||
for i, logFunc := range logFunChain {
|
||||
|
|
|
@ -147,7 +147,7 @@ type fakeOutput int
|
|||
|
||||
func (o *fakeOutput) Write([]byte) (int, error) {
|
||||
*o++
|
||||
return 0, errors.New("fake output")
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// go test -run Test_Logger_ErrorOutput_WithoutColor
|
||||
|
|
Loading…
Reference in New Issue