mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
20 days of unstoppable work. Waiting fo go 1.8, I didn't finish yet, some touches remains.
Former-commit-id: ed84f99c89f43fe5e980a8e6d0ee22c186f0e1b9
This commit is contained in:
62
middleware/logger/logger.go
Normal file
62
middleware/logger/logger.go
Normal file
@@ -0,0 +1,62 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user