mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 18:37:05 +00:00
Read https://github.com/kataras/iris/tree/master/HISTORY.md
This commit is contained in:
@@ -11,11 +11,9 @@ import (
|
||||
|
||||
// Options are the options of the logger middlweare
|
||||
// contains 5 bools
|
||||
// Latency, Status, IP, Method, Path
|
||||
// Status, IP, Method, Path, EnableColors
|
||||
// if set to true then these will print
|
||||
type Options struct {
|
||||
// Latency displays latency (bool)
|
||||
Latency bool
|
||||
// Status displays status code (bool)
|
||||
Status bool
|
||||
// IP displays request's remote address (bool)
|
||||
@@ -24,11 +22,13 @@ type Options struct {
|
||||
Method bool
|
||||
// Path displays the request path (bool)
|
||||
Path bool
|
||||
// EnableColors defaults to false
|
||||
EnableColors bool
|
||||
}
|
||||
|
||||
// DefaultOptions returns an options which all properties are true
|
||||
func DefaultOptions() Options {
|
||||
return Options{true, true, true, true, true}
|
||||
return Options{true, true, true, true, false}
|
||||
}
|
||||
|
||||
type loggerMiddleware struct {
|
||||
@@ -45,17 +45,13 @@ func (l *loggerMiddleware) Serve(ctx *iris.Context) {
|
||||
path = ctx.PathString()
|
||||
method = ctx.MethodString()
|
||||
|
||||
if l.options.Latency {
|
||||
startTime = time.Now()
|
||||
}
|
||||
startTime = time.Now()
|
||||
|
||||
ctx.Next()
|
||||
if l.options.Latency {
|
||||
//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)
|
||||
}
|
||||
//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.options.Status {
|
||||
status = strconv.Itoa(ctx.Response.StatusCode())
|
||||
@@ -74,14 +70,18 @@ func (l *loggerMiddleware) Serve(ctx *iris.Context) {
|
||||
}
|
||||
|
||||
//finally print the logs
|
||||
if l.options.Latency {
|
||||
l.Otherf("%s %v %4v %s %s %s \n", date, status, latency, ip, method, path)
|
||||
} else {
|
||||
l.Otherf("%s %v %s %s %s \n", date, status, ip, method, path)
|
||||
}
|
||||
l.printf("%s %v %4v %s %s %s \n", date, status, latency, ip, method, path)
|
||||
|
||||
}
|
||||
|
||||
func (l *loggerMiddleware) printf(format string, a ...interface{}) {
|
||||
if l.options.EnableColors {
|
||||
l.Logger.Otherf(format, a...)
|
||||
} else {
|
||||
l.Logger.Printf(format, a...)
|
||||
}
|
||||
}
|
||||
|
||||
// Default returns the logger middleware as Handler with the default settings
|
||||
func New(theLogger *logger.Logger, options ...Options) iris.HandlerFunc {
|
||||
if theLogger == nil {
|
||||
|
||||
Reference in New Issue
Block a user