github.com/System-Glitch/goyave/v2@v2.10.3-0.20200819142921-51011e75d504/docs_src/src/guide/advanced/logging.md (about) 1 --- 2 meta: 3 - name: "og:title" 4 content: "Logging - Goyave" 5 - name: "twitter:title" 6 content: "Logging - Goyave" 7 - name: "title" 8 content: "Logging - Goyave" 9 --- 10 11 # Logging <Badge text="Since v2.8.0"/> 12 13 [[toc]] 14 15 ## Introduction 16 17 Logging is an important part of all applications. The framework provides a standard and flexible way to log accesses, errors and regular information. This standard logging feature should be preferred in all applications and modules developed using Goyave for consistency across the framework's environment. 18 19 ## Custom loggers 20 21 The framework provides three [Go standard loggers](https://golang.org/pkg/log/): 22 - `goyave.Logger`: the logger for regular and miscellaneous information. Outputs to `os.Stdout` with `log.LstdFlags` by default. This logger is not used by any of the framework's internal features. 23 - `goyave.AccessLogger`: the logger used by the logging middleware (see below). Outputs to `os.Stdout` with no flags by default. 24 - `goyave.ErrLogger`: the logger for errors and stacktraces. Outputs to `os.Stderr` with `log.LstdFlags` by default. 25 26 All these loggers can be modified or replaced entirely. Modifications to standard loggers should be done **before** calling `goyave.Start()`. 27 28 **Example:** 29 ```go 30 func main() { 31 // Replace de default logger 32 goyave.Logger = log.New(os.Stdout, "myapp", log.Ldate | log.Ltime | log.Lshortfile) 33 34 goyave.Logger.Println("Starting...") 35 goyave.RegisterStartupHook(func() { 36 goyave.Logger.Println("Started.") 37 }) 38 goyave.Start(routes.Register) 39 } 40 ``` 41 42 ## Common and Combined access logs 43 44 To enable logging of accesses using the [Common Log Format](https://en.wikipedia.org/wiki/Common_Log_Format), simply register the `CommonLogMiddleware`. Alternatively, you can use `CombinedLogMiddleware` for Combined Log Format. 45 46 ``` go 47 import ( 48 "github.com/System-Glitch/goyave/v2/log" 49 "github.com/System-Glitch/goyave/v2" 50 ) 51 52 func registerRoutes(router *goyave.Router) { 53 // Common log format 54 router.Middleware(log.CommonLogMiddleware()) 55 56 // Combined log format 57 router.Middleware(log.CombinedLogMiddleware()) 58 } 59 ``` 60 61 Each request the server receives will be logged using the `goyave.AccessLogger` logger. 62 63 #### log.CommonLogMiddleware 64 65 CommonLogMiddleware captures response data and outputs it to the default logger using the common log format. 66 67 | Parameters | Return | 68 |------------|---------------------| 69 | | `goyave.Middleware` | 70 71 #### log.CombinedLogMiddleware 72 73 CombinedLogMiddleware captures response data and outputs it to the default logger using the combined log format. 74 75 | Parameters | Return | 76 |------------|---------------------| 77 | | `goyave.Middleware` | 78 79 ### Custom format 80 81 It is possible to implement custom formatters for access logs. A `Formatter` is a function with the following signature: 82 83 ``` go 84 func(now time.Time, response *goyave.Response, request *goyave.Request, length int) string 85 ``` 86 87 - `now` is the time at which the server received the request 88 - `length` the length of the response body 89 90 **Example:** 91 ``` go 92 func CustomFormatter(now time.Time, response *goyave.Response, request *goyave.Request, length int) string { 93 return fmt.Sprintf("%s %s %s %s %d %d", 94 now.Format(TimestampFormat), 95 host, 96 req.Method, 97 strconv.QuoteToASCII(uri), 98 response.GetStatus(), 99 length, 100 ) 101 } 102 ``` 103 104 #### log.Middleware 105 106 Middleware captures response data and outputs it to the default logger using the given formatter. 107 108 | Parameters | Return | 109 |-----------------------|---------------------| 110 | `formatter Formatter` | `goyave.Middleware` | 111 112 **Example:** 113 ``` go 114 router.Middleware(log.Middleware(CustomFormatter)) 115 ```