Merge pull request #1011 from hi019/master

Add more logger examples
pull/1014/head
Joey 2020-11-11 18:29:25 +01:00 committed by GitHub
commit ea50823730
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 5 deletions

View File

@ -13,8 +13,8 @@ Logger middleware for [Fiber](https://github.com/gofiber/fiber) that logs HTTP r
func New(config ...Config) fiber.Handler
```
### Examples
Import the middleware package that is part of the Fiber web framework
## Examples
First ensure the appropriate packages are imported
```go
import (
"github.com/gofiber/fiber/v2"
@ -22,17 +22,42 @@ import (
)
```
After you initiate your Fiber app, you can use the following possibilities:
#### **Initialization / Default Config**
```go
// Default middleware config
app.Use(logger.New())
```
// Or extend your config for customization
#### **Logging Request ID**
```go
app.Use(requestid.New())
app.Use(logger.New(logger.Config{
// For more options, see the Config section
Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}\n",
}))
```
#### **Changing TimeZone & TimeFormat**
```go
app.Use(logger.New(logger.Config{
Format: "${pid} ${status} - ${method} ${path}\n",
TimeFormat: "02-Jan-2006",
TimeZone: "America/New_York",
Output: os.Stdout,
}))
```
#### **Custom File Writer**
```go
file, err := os.OpenFile("./123.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer file.Close()
app.Use(logger.New(logger.Config{
Output: file,
}))
```