mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-24 07:25:38 +00:00
95 lines
2.1 KiB
Go
95 lines
2.1 KiB
Go
|
package logger
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
"github.com/gofiber/fiber/v2"
|
||
|
)
|
||
|
|
||
|
// 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
|
||
|
|
||
|
// 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
|
||
|
|
||
|
enableColors bool
|
||
|
enableLatency bool
|
||
|
timeZoneLocation *time.Location
|
||
|
}
|
||
|
|
||
|
// ConfigDefault is the default config
|
||
|
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,
|
||
|
enableColors: true,
|
||
|
}
|
||
|
|
||
|
// Helper function to set default values
|
||
|
func configDefault(config ...Config) Config {
|
||
|
// Return default config if nothing provided
|
||
|
if len(config) < 1 {
|
||
|
return ConfigDefault
|
||
|
}
|
||
|
|
||
|
// Override default config
|
||
|
cfg := config[0]
|
||
|
|
||
|
// Enable colors if no custom format or output is given
|
||
|
if cfg.Format == "" && cfg.Output == nil {
|
||
|
cfg.enableColors = true
|
||
|
}
|
||
|
|
||
|
// Set default values
|
||
|
if cfg.Next == nil {
|
||
|
cfg.Next = ConfigDefault.Next
|
||
|
}
|
||
|
if cfg.Format == "" {
|
||
|
cfg.Format = ConfigDefault.Format
|
||
|
}
|
||
|
if cfg.TimeZone == "" {
|
||
|
cfg.TimeZone = ConfigDefault.TimeZone
|
||
|
}
|
||
|
if cfg.TimeFormat == "" {
|
||
|
cfg.TimeFormat = ConfigDefault.TimeFormat
|
||
|
}
|
||
|
if int(cfg.TimeInterval) <= 0 {
|
||
|
cfg.TimeInterval = ConfigDefault.TimeInterval
|
||
|
}
|
||
|
if cfg.Output == nil {
|
||
|
cfg.Output = ConfigDefault.Output
|
||
|
}
|
||
|
return cfg
|
||
|
}
|