github.com/gofiber/fiber/v2@v2.47.0/docs/api/middleware/logger.md (about) 1 --- 2 id: logger 3 title: Logger 4 --- 5 6 Logger middleware for [Fiber](https://github.com/gofiber/fiber) that logs HTTP request/response details. 7 8 ## Signatures 9 ```go 10 func New(config ...Config) fiber.Handler 11 ``` 12 ## Examples 13 14 Import the middleware package that is part of the Fiber web framework 15 16 ```go 17 import ( 18 "github.com/gofiber/fiber/v2" 19 "github.com/gofiber/fiber/v2/middleware/logger" 20 ) 21 ``` 22 23 :::tip 24 The order of registration plays a role. Only all routes that are registered after this one will be logged. 25 The middleware should therefore be one of the first to be registered. 26 ::: 27 28 After you initiate your Fiber app, you can use the following possibilities: 29 30 ```go 31 // Initialize default config 32 app.Use(logger.New()) 33 34 // Or extend your config for customization 35 // Logging remote IP and Port 36 app.Use(logger.New(logger.Config{ 37 Format: "[${ip}]:${port} ${status} - ${method} ${path}\n", 38 })) 39 40 // Logging Request ID 41 app.Use(requestid.New()) 42 app.Use(logger.New(logger.Config{ 43 // For more options, see the Config section 44 Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}\n", 45 })) 46 47 // Changing TimeZone & TimeFormat 48 app.Use(logger.New(logger.Config{ 49 Format: "${pid} ${status} - ${method} ${path}\n", 50 TimeFormat: "02-Jan-2006", 51 TimeZone: "America/New_York", 52 })) 53 54 // Custom File Writer 55 file, err := os.OpenFile("./123.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) 56 if err != nil { 57 log.Fatalf("error opening file: %v", err) 58 } 59 defer file.Close() 60 app.Use(logger.New(logger.Config{ 61 Output: file, 62 })) 63 64 // Add Custom Tags 65 app.Use(logger.New(logger.Config{ 66 CustomTags: map[string]logger.LogFunc{ 67 "custom_tag": func(output logger.Buffer, c *fiber.Ctx, data *logger.Data, extraParam string) (int, error) { 68 return output.WriteString("it is a custom tag") 69 }, 70 }, 71 })) 72 73 // Callback after log is written 74 app.Use(logger.New(logger.Config{ 75 TimeFormat: time.RFC3339Nano, 76 TimeZone: "Asia/Shanghai", 77 Done: func(c *fiber.Ctx, logString []byte) { 78 if c.Response().StatusCode() != fiber.StatusOK { 79 reporter.SendToSlack(logString) 80 } 81 }, 82 })) 83 84 // Disable colors when outputting to default format 85 app.Use(logger.New(logger.Config{ 86 DisableColors: true, 87 })) 88 ``` 89 90 ## Config 91 ```go 92 // Config defines the config for middleware. 93 type Config struct { 94 // Next defines a function to skip this middleware when returned true. 95 // 96 // Optional. Default: nil 97 Next func(c *fiber.Ctx) bool 98 99 // Done is a function that is called after the log string for a request is written to Output, 100 // and pass the log string as parameter. 101 // 102 // Optional. Default: nil 103 Done func(c *fiber.Ctx, logString []byte) 104 105 // tagFunctions defines the custom tag action 106 // 107 // Optional. Default: map[string]LogFunc 108 CustomTags map[string]LogFunc 109 110 // Format defines the logging tags 111 // 112 // Optional. Default: [${time}] ${status} - ${latency} ${method} ${path}\n 113 Format string 114 115 // TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html 116 // 117 // Optional. Default: 15:04:05 118 TimeFormat string 119 120 // TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc 121 // 122 // Optional. Default: "Local" 123 TimeZone string 124 125 // TimeInterval is the delay before the timestamp is updated 126 // 127 // Optional. Default: 500 * time.Millisecond 128 TimeInterval time.Duration 129 130 // Output is a writer where logs are written 131 // 132 // Default: os.Stdout 133 Output io.Writer 134 135 // DisableColors defines if the logs output should be colorized 136 // 137 // Default: false 138 DisableColors bool 139 140 enableColors bool 141 enableLatency bool 142 timeZoneLocation *time.Location 143 } 144 type LogFunc func(buf logger.Buffer, c *fiber.Ctx, data *logger.Data, extraParam string) (int, error) 145 ``` 146 ## Default Config 147 ```go 148 var ConfigDefault = Config{ 149 Next: nil, 150 Done: nil, 151 Format: "[${time}] ${status} - ${latency} ${method} ${path}\n", 152 TimeFormat: "15:04:05", 153 TimeZone: "Local", 154 TimeInterval: 500 * time.Millisecond, 155 Output: os.Stdout, 156 DisableColors: true, 157 } 158 ``` 159 160 ## Constants 161 ```go 162 // Logger variables 163 const ( 164 TagPid = "pid" 165 TagTime = "time" 166 TagReferer = "referer" 167 TagProtocol = "protocol" 168 TagPort = "port" 169 TagIP = "ip" 170 TagIPs = "ips" 171 TagHost = "host" 172 TagMethod = "method" 173 TagPath = "path" 174 TagURL = "url" 175 TagUA = "ua" 176 TagLatency = "latency" 177 TagStatus = "status" // response status 178 TagResBody = "resBody" // response body 179 TagReqHeaders = "reqHeaders" 180 TagQueryStringParams = "queryParams" // request query parameters 181 TagBody = "body" // request body 182 TagBytesSent = "bytesSent" 183 TagBytesReceived = "bytesReceived" 184 TagRoute = "route" 185 TagError = "error" 186 // DEPRECATED: Use TagReqHeader instead 187 TagHeader = "header:" // request header 188 TagReqHeader = "reqHeader:" // request header 189 TagRespHeader = "respHeader:" // response header 190 TagQuery = "query:" // request query 191 TagForm = "form:" // request form 192 TagCookie = "cookie:" // request cookie 193 TagLocals = "locals:" 194 // colors 195 TagBlack = "black" 196 TagRed = "red" 197 TagGreen = "green" 198 TagYellow = "yellow" 199 TagBlue = "blue" 200 TagMagenta = "magenta" 201 TagCyan = "cyan" 202 TagWhite = "white" 203 TagReset = "reset" 204 ) 205 ```