v1.1.4
# Conflicts: # logger.go
This commit is contained in:
commit
e2212d40c6
@ -2,9 +2,9 @@ language: go
|
|||||||
sudo: false
|
sudo: false
|
||||||
go:
|
go:
|
||||||
- 1.4
|
- 1.4
|
||||||
- 1.5
|
- 1.5.4
|
||||||
- 1.6
|
- 1.6.4
|
||||||
- 1.7
|
- 1.7.4
|
||||||
- tip
|
- tip
|
||||||
|
|
||||||
script:
|
script:
|
||||||
|
16
logger.go
16
logger.go
@ -7,7 +7,10 @@ package gin
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/mattn/go-isatty"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -44,6 +47,12 @@ func Logger() HandlerFunc {
|
|||||||
// LoggerWithWriter instance a Logger middleware with the specified writter buffer.
|
// LoggerWithWriter instance a Logger middleware with the specified writter buffer.
|
||||||
// Example: os.Stdout, a file opened in write mode, a socket...
|
// Example: os.Stdout, a file opened in write mode, a socket...
|
||||||
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
||||||
|
isTerm := true
|
||||||
|
|
||||||
|
if w, ok := out.(*os.File); !ok || !isatty.IsTerminal(w.Fd()) {
|
||||||
|
isTerm = false
|
||||||
|
}
|
||||||
|
|
||||||
var skip map[string]struct{}
|
var skip map[string]struct{}
|
||||||
|
|
||||||
if length := len(notlogged); length > 0 {
|
if length := len(notlogged); length > 0 {
|
||||||
@ -71,8 +80,11 @@ func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
|||||||
clientIP := c.ClientIP()
|
clientIP := c.ClientIP()
|
||||||
method := c.Request.Method
|
method := c.Request.Method
|
||||||
statusCode := c.Writer.Status()
|
statusCode := c.Writer.Status()
|
||||||
statusColor := colorForStatus(statusCode)
|
var statusColor, methodColor string
|
||||||
methodColor := colorForMethod(method)
|
if isTerm {
|
||||||
|
statusColor = colorForStatus(statusCode)
|
||||||
|
methodColor = colorForMethod(method)
|
||||||
|
}
|
||||||
comment := c.Errors.ByType(ErrorTypePrivate).String()
|
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 %-7s %s\n%s",
|
||||||
|
Loading…
Reference in New Issue
Block a user