mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 21:06:39 +08:00
Added gin.LoggerDisk which logs to screen and a file
This commit is contained in:
parent
3d002e3823
commit
0adc3ea50c
64
logger.go
64
logger.go
@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -43,9 +44,68 @@ func ErrorLoggerT(typ ErrorType) HandlerFunc {
|
||||
func Logger() HandlerFunc {
|
||||
return LoggerWithWriter(DefaultWriter)
|
||||
}
|
||||
func LoggerDisk(LogFile io.Writer) HandlerFunc {
|
||||
return LoggerWithDisk(DefaultWriter,LogFile)
|
||||
}
|
||||
|
||||
// Instance a Logger middleware with the specified writter buffer.
|
||||
// Example: os.Stdout, a file opened in write mode, a socket...
|
||||
func LoggerWithDisk(out io.Writer, accesslog io.Writer, notlogged ...string) HandlerFunc {
|
||||
var skip map[string]struct{}
|
||||
|
||||
if length := len(notlogged); length > 0 {
|
||||
skip = make(map[string]struct{}, length)
|
||||
|
||||
for _, path := range notlogged {
|
||||
skip[path] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
return func(c *Context) {
|
||||
// Start timer
|
||||
start := time.Now()
|
||||
path := c.Request.URL.Path
|
||||
|
||||
// Process request
|
||||
c.Next()
|
||||
|
||||
// Log only when path is not being skipped
|
||||
if _, ok := skip[path]; !ok {
|
||||
// Stop timer
|
||||
end := time.Now()
|
||||
latency := end.Sub(start)
|
||||
|
||||
clientIP := c.ClientIP()
|
||||
clientHost,_ := net.LookupAddr(clientIP)
|
||||
method := c.Request.Method
|
||||
statusCode := c.Writer.Status()
|
||||
statusColor := colorForStatus(statusCode)
|
||||
methodColor := colorForMethod(method)
|
||||
comment := c.Errors.ByType(ErrorTypePrivate).String()
|
||||
|
||||
fmt.Fprintf(accesslog, "[GIN] %v | %3d | %13v | %s | %s | %-6s %s\n%s",
|
||||
end.Format("2006/01/02 - 15:04:05"),
|
||||
statusCode,
|
||||
latency,
|
||||
clientIP,
|
||||
clientHost[0],
|
||||
method,
|
||||
path,
|
||||
comment,
|
||||
)
|
||||
fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %s | %s |%s %s %-6s %s\n%s",
|
||||
end.Format("2006/01/02 - 15:04:05"),
|
||||
statusColor, statusCode, reset,
|
||||
latency,
|
||||
clientIP,
|
||||
clientHost[0],
|
||||
methodColor, reset, method,
|
||||
path,
|
||||
comment,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
||||
var skip map[string]struct{}
|
||||
|
||||
@ -72,17 +132,19 @@ func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
||||
latency := end.Sub(start)
|
||||
|
||||
clientIP := c.ClientIP()
|
||||
clientHost,_ := net.LookupAddr(clientIP)
|
||||
method := c.Request.Method
|
||||
statusCode := c.Writer.Status()
|
||||
statusColor := colorForStatus(statusCode)
|
||||
methodColor := colorForMethod(method)
|
||||
comment := c.Errors.ByType(ErrorTypePrivate).String()
|
||||
|
||||
fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %s |%s %s %-7s %s\n%s",
|
||||
fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %s | %s |%s %s %-6s %s\n%s",
|
||||
end.Format("2006/01/02 - 15:04:05"),
|
||||
statusColor, statusCode, reset,
|
||||
latency,
|
||||
clientIP,
|
||||
clientHost[0],
|
||||
methodColor, reset, method,
|
||||
path,
|
||||
comment,
|
||||
|
Loading…
x
Reference in New Issue
Block a user