skip ANSI color commands

ref: https://github.com/gin-gonic/gin/pull/754

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu
2016-12-14 22:27:22 +08:00
parent c5bba12d76
commit c9873b1644
3 changed files with 40 additions and 12 deletions

View File

@@ -4,10 +4,12 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"os"
"strings"
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/mattn/go-isatty"
)
var (
@@ -54,6 +56,12 @@ type LogPushEntry struct {
Category string `json:"category,omitempty"`
}
var isTerm bool
func init() {
isTerm = isatty.IsTerminal(os.Stdout.Fd())
}
// InitLog use for initial log module
func InitLog() error {
@@ -145,9 +153,15 @@ func LogRequest(uri string, method string, ip string, contentType string, agent
output = string(logJSON)
} else {
var headerColor string
if isTerm {
headerColor = magenta
}
// format is string
output = fmt.Sprintf("|%s header %s| %s %s %s %s %s",
magenta, reset,
headerColor, reset,
log.Method,
log.URI,
log.IP,
@@ -203,8 +217,10 @@ func hideToken(token string, markLen int) string {
func LogPush(status, token string, req PushNotification, errPush error) {
var plat, platColor, output string
platColor = colorForPlatForm(req.Platform)
plat = typeForPlatForm(req.Platform)
if isTerm {
platColor = colorForPlatForm(req.Platform)
plat = typeForPlatForm(req.Platform)
}
errMsg := ""
if errPush != nil {
@@ -228,17 +244,26 @@ func LogPush(status, token string, req PushNotification, errPush error) {
output = string(logJSON)
} else {
var typeColor string
switch status {
case SucceededPush:
if isTerm {
typeColor = green
}
output = fmt.Sprintf("|%s %s %s| %s%s%s [%s] %s",
green, log.Type, reset,
typeColor, log.Type, reset,
platColor, log.Platform, reset,
log.Token,
log.Message,
)
case FailedPush:
if isTerm {
typeColor = red
}
output = fmt.Sprintf("|%s %s %s| %s%s%s [%s] | %s | Error Message: %s",
red, log.Type, reset,
typeColor, log.Type, reset,
platColor, log.Platform, reset,
log.Token,
log.Message,