iris/middleware/logger/logger.go
Gerasimos (Makis) Maropoulos 244a59e055 20 days of unstoppable work. Waiting fo go 1.8, I didn't finish yet, some touches remains.
Former-commit-id: ed84f99c89f43fe5e980a8e6d0ee22c186f0e1b9
2017-02-14 05:54:11 +02:00

63 lines
1.1 KiB
Go

package logger
import (
"fmt"
"strconv"
"time"
"gopkg.in/kataras/iris.v6"
)
type loggerMiddleware struct {
config Config
}
// Serve serves the middleware
func (l *loggerMiddleware) Serve(ctx *iris.Context) {
//all except latency to string
var date, status, ip, method, path string
var latency time.Duration
var startTime, endTime time.Time
path = ctx.Path()
method = ctx.Method()
startTime = time.Now()
ctx.Next()
//no time.Since in order to format it well after
endTime = time.Now()
date = endTime.Format("01/02 - 15:04:05")
latency = endTime.Sub(startTime)
if l.config.Status {
status = strconv.Itoa(ctx.ResponseWriter.StatusCode())
}
if l.config.IP {
ip = ctx.RemoteAddr()
}
if !l.config.Method {
method = ""
}
if !l.config.Path {
path = ""
}
//finally print the logs
ctx.Log(iris.DevMode, fmt.Sprintf("%s %v %4v %s %s %s \n", date, status, latency, ip, method, path))
}
// New returns the logger middleware
// receives optional configs(logger.Config)
func New(cfg ...Config) iris.HandlerFunc {
c := DefaultConfig()
if len(cfg) > 0 {
c = cfg[0]
}
l := &loggerMiddleware{config: c}
return l.Serve
}