2020-06-07 14:26:06 +02:00
|
|
|
// Package logger provides request logging via middleware. See _examples/logging/request-logger
|
2017-02-14 04:54:11 +01:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2017-07-16 12:58:10 +02:00
|
|
|
"fmt"
|
2017-02-14 04:54:11 +01:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/context"
|
2017-02-14 04:54:11 +01:00
|
|
|
)
|
|
|
|
|
2020-04-27 14:48:09 +02:00
|
|
|
func init() {
|
2020-04-28 21:34:36 +02:00
|
|
|
context.SetHandlerName("iris/middleware/logger.*", "iris.logger")
|
2020-04-27 14:48:09 +02:00
|
|
|
}
|
|
|
|
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
type requestLoggerMiddleware struct {
|
2017-02-14 04:54:11 +01:00
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
2017-07-16 12:58:10 +02:00
|
|
|
// New creates and returns a new request logger middleware.
|
|
|
|
// Do not confuse it with the framework's Logger.
|
|
|
|
// This is for the http requests.
|
|
|
|
//
|
|
|
|
// Receives an optional configuation.
|
2020-08-18 04:42:48 +02:00
|
|
|
// Usage: app.UseRouter(logger.New()).
|
2017-07-16 12:58:10 +02:00
|
|
|
func New(cfg ...Config) context.Handler {
|
2017-07-17 16:42:51 +02:00
|
|
|
c := DefaultConfig()
|
2017-07-16 12:58:10 +02:00
|
|
|
if len(cfg) > 0 {
|
|
|
|
c = cfg[0]
|
|
|
|
}
|
|
|
|
c.buildSkipper()
|
|
|
|
l := &requestLoggerMiddleware{config: c}
|
|
|
|
|
|
|
|
return l.ServeHTTP
|
|
|
|
}
|
|
|
|
|
2020-08-18 04:42:48 +02:00
|
|
|
func (l *requestLoggerMiddleware) getPath(ctx *context.Context) string {
|
|
|
|
if l.config.Query {
|
|
|
|
return ctx.Request().URL.RequestURI()
|
|
|
|
}
|
|
|
|
return ctx.Path()
|
|
|
|
}
|
|
|
|
|
2017-02-14 04:54:11 +01:00
|
|
|
// Serve serves the middleware
|
2020-07-10 22:21:09 +02:00
|
|
|
func (l *requestLoggerMiddleware) ServeHTTP(ctx *context.Context) {
|
2017-07-16 12:58:10 +02:00
|
|
|
// skip logs and serve the main request immediately
|
|
|
|
if l.config.skip != nil {
|
|
|
|
if l.config.skip(ctx) {
|
|
|
|
ctx.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 09:06:20 +02:00
|
|
|
// all except latency to string
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
var status, ip, method, path string
|
2017-02-14 04:54:11 +01:00
|
|
|
var latency time.Duration
|
|
|
|
var startTime, endTime time.Time
|
|
|
|
startTime = time.Now()
|
|
|
|
|
2020-08-18 04:42:48 +02:00
|
|
|
// Before Next.
|
2017-02-14 04:54:11 +01:00
|
|
|
if l.config.IP {
|
|
|
|
ip = ctx.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
2017-07-16 12:58:10 +02:00
|
|
|
if l.config.Method {
|
|
|
|
method = ctx.Method()
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|
|
|
|
|
2017-07-16 12:58:10 +02:00
|
|
|
if l.config.Path {
|
2020-08-18 04:42:48 +02:00
|
|
|
path = l.getPath(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Next()
|
|
|
|
|
|
|
|
// no time.Since in order to format it well after
|
|
|
|
endTime = time.Now()
|
|
|
|
latency = endTime.Sub(startTime)
|
|
|
|
|
|
|
|
if l.config.PathAfterHandler /* we don't care if Path is disabled */ {
|
|
|
|
path = l.getPath(ctx)
|
2020-08-22 02:21:44 +02:00
|
|
|
// note: we could just use the r.RequestURI which is the original one,
|
|
|
|
// but some users may need the stripped one (on HandleDir).
|
2020-08-18 04:42:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if l.config.Status {
|
|
|
|
status = strconv.Itoa(ctx.GetStatusCode())
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|
|
|
|
|
2017-07-17 16:42:51 +02:00
|
|
|
var message interface{}
|
2018-03-26 09:50:33 +02:00
|
|
|
if ctxKeys := l.config.MessageContextKeys; len(ctxKeys) > 0 {
|
|
|
|
for _, key := range ctxKeys {
|
|
|
|
msg := ctx.Values().Get(key)
|
|
|
|
if message == nil {
|
|
|
|
message = msg
|
|
|
|
} else {
|
|
|
|
message = fmt.Sprintf(" %v %v", message, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var headerMessage interface{}
|
|
|
|
if headerKeys := l.config.MessageHeaderKeys; len(headerKeys) > 0 {
|
|
|
|
for _, key := range headerKeys {
|
|
|
|
msg := ctx.GetHeader(key)
|
|
|
|
if headerMessage == nil {
|
|
|
|
headerMessage = msg
|
|
|
|
} else {
|
|
|
|
headerMessage = fmt.Sprintf(" %v %v", headerMessage, msg)
|
|
|
|
}
|
|
|
|
}
|
2017-07-17 16:42:51 +02:00
|
|
|
}
|
|
|
|
|
2017-07-16 12:58:10 +02:00
|
|
|
// print the logs
|
|
|
|
if logFunc := l.config.LogFunc; logFunc != nil {
|
2018-03-26 09:50:33 +02:00
|
|
|
logFunc(endTime, latency, status, ip, method, path, message, headerMessage)
|
2017-07-16 12:58:10 +02:00
|
|
|
return
|
2019-06-21 18:43:25 +02:00
|
|
|
} else if logFuncCtx := l.config.LogFuncCtx; logFuncCtx != nil {
|
|
|
|
logFuncCtx(ctx, latency)
|
|
|
|
return
|
2017-07-16 12:58:10 +02:00
|
|
|
}
|
2017-07-26 14:30:20 +02:00
|
|
|
|
2017-07-16 12:58:10 +02:00
|
|
|
// no new line, the framework's logger is responsible how to render each log.
|
2017-07-26 14:30:20 +02:00
|
|
|
line := fmt.Sprintf("%v %4v %s %s %s", status, latency, ip, method, path)
|
2017-07-17 16:42:51 +02:00
|
|
|
if message != nil {
|
|
|
|
line += fmt.Sprintf(" %v", message)
|
|
|
|
}
|
2018-03-26 09:50:33 +02:00
|
|
|
|
|
|
|
if headerMessage != nil {
|
|
|
|
line += fmt.Sprintf(" %v", headerMessage)
|
|
|
|
}
|
2019-12-29 18:14:41 +01:00
|
|
|
|
2020-08-18 04:42:48 +02:00
|
|
|
if context.StatusCodeNotSuccessful(ctx.GetStatusCode()) {
|
|
|
|
ctx.Application().Logger().Warn(line)
|
|
|
|
} else {
|
|
|
|
ctx.Application().Logger().Info(line)
|
|
|
|
}
|
2020-08-18 18:33:07 +02:00
|
|
|
|
|
|
|
if l.config.TraceRoute && ctx.GetCurrentRoute() != nil /* it is nil on unhandled error codes */ {
|
2020-08-19 00:24:36 +02:00
|
|
|
// Get the total length of handlers and see if all are executed.
|
|
|
|
// Note(@kataras): we get those after handler executed, because
|
|
|
|
// filters (and overlap) feature will set the handlers on router build
|
|
|
|
// state to fullfil their needs. And we need to respect
|
|
|
|
// any dev's custom SetHandlers&Do actions too so we don't give false info.
|
|
|
|
// if n, idx := len(ctx.Handlers()), ctx.HandlerIndex(-1); idx < n-1 {
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// Let's pass it into the Trace function itself which will "mark"
|
|
|
|
// every handler that is eventually executed.
|
|
|
|
// Note that if StopExecution is called, the index is always -1,
|
|
|
|
// so no "mark" signs will be printed at all <- this can be fixed by introducing a new ctx field.
|
|
|
|
ctx.GetCurrentRoute().Trace(ctx.Application().Logger().Printer, ctx.HandlerIndex(-1))
|
2020-08-18 18:33:07 +02:00
|
|
|
}
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|