iris/middleware/logger/logger.go

158 lines
3.7 KiB
Go
Raw Normal View History

// Package logger provides request logging via middleware. See _examples/logging/request-logger
package logger
import (
"fmt"
"strconv"
"time"
"github.com/kataras/iris/v12/context"
"github.com/ryanuber/columnize"
)
func init() {
context.SetHandlerName("iris/middleware/logger.*", "iris.logger")
}
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 {
config Config
}
// 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.
func New(cfg ...Config) context.Handler {
c := DefaultConfig()
if len(cfg) > 0 {
c = cfg[0]
}
c.buildSkipper()
l := &requestLoggerMiddleware{config: c}
return l.ServeHTTP
}
// Serve serves the middleware
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
func (l *requestLoggerMiddleware) ServeHTTP(ctx context.Context) {
// skip logs and serve the main request immediately
if l.config.skip != nil {
if l.config.skip(ctx) {
ctx.Next()
return
}
}
// 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
var latency time.Duration
var startTime, endTime time.Time
startTime = time.Now()
ctx.Next()
// no time.Since in order to format it well after
endTime = time.Now()
latency = endTime.Sub(startTime)
if l.config.Status {
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
status = strconv.Itoa(ctx.GetStatusCode())
}
if l.config.IP {
ip = ctx.RemoteAddr()
}
if l.config.Method {
method = ctx.Method()
}
if l.config.Path {
if l.config.Query {
path = ctx.Request().URL.RequestURI()
} else {
path = ctx.Path()
}
}
var message interface{}
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)
}
}
}
// print the logs
if logFunc := l.config.LogFunc; logFunc != nil {
logFunc(endTime, latency, status, ip, method, path, message, headerMessage)
return
} else if logFuncCtx := l.config.LogFuncCtx; logFuncCtx != nil {
logFuncCtx(ctx, latency)
return
}
if l.config.Columns {
endTimeFormatted := endTime.Format("2006/01/02 - 15:04:05")
output := Columnize(endTimeFormatted, latency, status, ip, method, path, message, headerMessage)
_, _ = ctx.Application().Logger().Printer.Write([]byte(output))
return
}
// no new line, the framework's logger is responsible how to render each log.
line := fmt.Sprintf("%v %4v %s %s %s", status, latency, ip, method, path)
if message != nil {
line += fmt.Sprintf(" %v", message)
}
if headerMessage != nil {
line += fmt.Sprintf(" %v", headerMessage)
}
// if context.StatusCodeNotSuccessful(ctx.GetStatusCode()) {
// ctx.Application().Logger().Warn(line)
// } else {
ctx.Application().Logger().Info(line)
// }
}
// Columnize formats the given arguments as columns and returns the formatted output,
// note that it appends a new line to the end.
func Columnize(nowFormatted string, latency time.Duration, status, ip, method, path string, message interface{}, headerMessage interface{}) string {
titles := "Time | Status | Latency | IP | Method | Path"
line := fmt.Sprintf("%s | %v | %4v | %s | %s | %s", nowFormatted, status, latency, ip, method, path)
if message != nil {
titles += " | Message"
line += fmt.Sprintf(" | %v", message)
}
if headerMessage != nil {
titles += " | HeaderMessage"
line += fmt.Sprintf(" | %v", headerMessage)
}
outputC := []string{
titles,
line,
}
output := columnize.SimpleFormat(outputC) + "\n"
return output
}