Skip to content
Myr

Merge pull request #744 from aviddiviner/logger-fix

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Bo-Yi Wu · committed by GitHub · parents 9177f01, 70ada0c · Browse files · 1 file+13 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Logger: skip ANSI color commands if output is not a tty

Diff stats

1 file+13 added−2 removed

  • other:+13 added−2 removed

1 file+13 added−2 removed

modified:logger.go+13 added-2 removed
5 unmodified lines
67import (8 "fmt"9 "io"10 "os"11 "time"1213 "golang.org/x/crypto/ssh/terminal"14)1516var (17 green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
28 unmodified lines
4647// LoggerWithWriter instance a Logger middleware with the specified writter buffer.48// Example: os.Stdout, a file opened in write mode, a socket...49func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {50 isTerm := true51 if outFile, ok := out.(*os.File); ok {52 isTerm = terminal.IsTerminal(int(outFile.Fd()))53 }5455 var skip map[string]struct{}5657 if length := len(notlogged); length > 0 {58 skip = make(map[string]struct{}, length)
19 unmodified lines
7879 clientIP := c.ClientIP()80 method := c.Request.Method81 statusCode := c.Writer.Status()82 var statusColor, methodColor string83 if isTerm {74 statusColor := colorForStatus(statusCode)75 methodColor := colorForMethod(method)84 statusColor = colorForStatus(statusCode)85 methodColor = colorForMethod(method)86 }87 comment := c.Errors.ByType(ErrorTypePrivate).String()8889 fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %s |%s %s %-7s %s\n%s",90 end.Format("2006/01/02 - 15:04:05"),
44 unmodified lines