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

10
glide.lock generated
View File

@ -1,5 +1,5 @@
hash: cccd36b5dd331f04f363476ebc21becfa3650945e3fd0b41821871702bc04743 hash: 54ead355ba6aeef8f2ea2ea1b5a473e2ce21371831e06355001dbc9b4dc18e22
updated: 2016-12-14T00:05:23.060124146+08:00 updated: 2016-12-14T22:43:06.161105893+08:00
imports: imports:
- name: github.com/asdine/storm - name: github.com/asdine/storm
version: 00b2f2df7ab7af9db746b826649395628cb5374e version: 00b2f2df7ab7af9db746b826649395628cb5374e
@ -12,7 +12,7 @@ imports:
- name: github.com/fvbock/endless - name: github.com/fvbock/endless
version: 2cdc20a776897c0f406853c048c33e2c1a9f0ebf version: 2cdc20a776897c0f406853c048c33e2c1a9f0ebf
- name: github.com/gin-gonic/gin - name: github.com/gin-gonic/gin
version: f931d1ea80ae95a6fc739213cdd9399bd2967fb6 version: e2212d40c62a98b388a5eb48ecbdcf88534688ba
subpackages: subpackages:
- binding - binding
- render - render
@ -28,6 +28,8 @@ imports:
version: 0496a6c14df020789376f4d4a261273d5ddb36ec version: 0496a6c14df020789376f4d4a261273d5ddb36ec
- name: github.com/manucorporat/sse - name: github.com/manucorporat/sse
version: ee05b128a739a0fb76c7ebd3ae4810c1de808d6d version: ee05b128a739a0fb76c7ebd3ae4810c1de808d6d
- name: github.com/mattn/go-isatty
version: 3a115632dcd687f9c8cd01679c83a06a0e21c1f3
- name: github.com/mattn/go-xmpp - name: github.com/mattn/go-xmpp
version: e44d1877bb457f5c3991903e9934a31e55c3a2ad version: e44d1877bb457f5c3991903e9934a31e55c3a2ad
- name: github.com/pborman/uuid - name: github.com/pborman/uuid
@ -137,8 +139,6 @@ testImports:
- log - log
- name: github.com/mattn/go-colorable - name: github.com/mattn/go-colorable
version: 9056b7a9f2d1f2d96498d6d146acd1f9d5ed3d59 version: 9056b7a9f2d1f2d96498d6d146acd1f9d5ed3d59
- name: github.com/mattn/go-isatty
version: 56b76bdf51f7708750eac80fa38b952bb9f32639
- name: github.com/pmezard/go-difflib - name: github.com/pmezard/go-difflib
version: 792786c7400a136282c1664665ae0a8db921c6c2 version: 792786c7400a136282c1664665ae0a8db921c6c2
subpackages: subpackages:

View File

@ -4,6 +4,7 @@ import:
- package: github.com/asdine/storm - package: github.com/asdine/storm
- package: github.com/fvbock/endless - package: github.com/fvbock/endless
- package: github.com/gin-gonic/gin - package: github.com/gin-gonic/gin
version: ^1.1.4
- package: github.com/google/go-gcm - package: github.com/google/go-gcm
- package: github.com/sideshow/apns2 - package: github.com/sideshow/apns2
subpackages: subpackages:
@ -17,6 +18,8 @@ import:
- package: gopkg.in/appleboy/gin-status-api.v1 - package: gopkg.in/appleboy/gin-status-api.v1
- package: gopkg.in/redis.v4 - package: gopkg.in/redis.v4
- package: gopkg.in/yaml.v2 - package: gopkg.in/yaml.v2
- package: github.com/mattn/go-isatty
version: ^0.0.1
testImport: testImport:
- package: github.com/buger/jsonparser - package: github.com/buger/jsonparser
- package: github.com/stretchr/testify - package: github.com/stretchr/testify

View File

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