2021-07-13 08:32:39 +00:00
|
|
|
package logx
|
2016-04-05 05:15:47 +00:00
|
|
|
|
|
|
|
import (
|
2016-04-07 06:16:59 +00:00
|
|
|
"encoding/json"
|
2016-04-05 07:04:50 +00:00
|
|
|
"errors"
|
2016-04-07 06:16:59 +00:00
|
|
|
"fmt"
|
2016-04-05 05:15:47 +00:00
|
|
|
"os"
|
2016-05-16 12:36:56 +00:00
|
|
|
"strings"
|
2016-12-14 14:27:22 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
"github.com/appleboy/gorush/core"
|
|
|
|
|
2016-12-14 14:27:22 +00:00
|
|
|
"github.com/mattn/go-isatty"
|
2017-06-24 18:23:24 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-04-05 05:15:47 +00:00
|
|
|
)
|
|
|
|
|
2016-04-07 06:16:59 +00:00
|
|
|
var (
|
2021-08-03 06:44:00 +00:00
|
|
|
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
|
|
|
|
yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
|
|
|
|
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
|
|
|
|
blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
|
|
|
|
reset = string([]byte{27, 91, 48, 109})
|
2016-04-07 06:16:59 +00:00
|
|
|
)
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// LogPushEntry is push response log
|
2016-04-08 08:56:32 +00:00
|
|
|
type LogPushEntry struct {
|
2019-09-06 07:48:42 +00:00
|
|
|
ID string `json:"notif_id,omitempty"`
|
2016-04-08 08:56:32 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Platform string `json:"platform"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2016-12-14 14:27:22 +00:00
|
|
|
var isTerm bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
isTerm = isatty.IsTerminal(os.Stdout.Fd())
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
var (
|
|
|
|
// LogAccess is log server request log
|
2021-07-20 13:16:53 +00:00
|
|
|
LogAccess = logrus.New()
|
2021-07-13 08:32:39 +00:00
|
|
|
// LogError is log server error log
|
2021-07-20 13:16:53 +00:00
|
|
|
LogError = logrus.New()
|
2021-07-13 08:32:39 +00:00
|
|
|
)
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// InitLog use for initial log module
|
2021-07-13 08:32:39 +00:00
|
|
|
func InitLog(accessLevel, accessLog, errorLevel, errorLog string) error {
|
2016-04-05 05:15:47 +00:00
|
|
|
var err error
|
|
|
|
|
2020-02-05 08:57:55 +00:00
|
|
|
if !isTerm {
|
|
|
|
LogAccess.SetFormatter(&logrus.JSONFormatter{})
|
|
|
|
LogError.SetFormatter(&logrus.JSONFormatter{})
|
|
|
|
} else {
|
|
|
|
LogAccess.Formatter = &logrus.TextFormatter{
|
|
|
|
TimestampFormat: "2006/01/02 - 15:04:05",
|
|
|
|
FullTimestamp: true,
|
|
|
|
}
|
2016-04-05 05:15:47 +00:00
|
|
|
|
2020-02-05 08:57:55 +00:00
|
|
|
LogError.Formatter = &logrus.TextFormatter{
|
|
|
|
TimestampFormat: "2006/01/02 - 15:04:05",
|
|
|
|
FullTimestamp: true,
|
|
|
|
}
|
2016-04-05 05:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set logger
|
2021-07-13 08:32:39 +00:00
|
|
|
if err = SetLogLevel(LogAccess, accessLevel); err != nil {
|
2016-04-05 07:04:50 +00:00
|
|
|
return errors.New("Set access log level error: " + err.Error())
|
2016-04-05 05:15:47 +00:00
|
|
|
}
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
if err = SetLogLevel(LogError, errorLevel); err != nil {
|
2016-04-05 07:04:50 +00:00
|
|
|
return errors.New("Set error log level error: " + err.Error())
|
2016-04-05 05:15:47 +00:00
|
|
|
}
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
if err = SetLogOut(LogAccess, accessLog); err != nil {
|
2016-04-05 07:04:50 +00:00
|
|
|
return errors.New("Set access log path error: " + err.Error())
|
2016-04-05 05:15:47 +00:00
|
|
|
}
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
if err = SetLogOut(LogError, errorLog); err != nil {
|
2016-04-05 07:04:50 +00:00
|
|
|
return errors.New("Set error log path error: " + err.Error())
|
2016-04-05 05:15:47 +00:00
|
|
|
}
|
2016-04-05 06:50:08 +00:00
|
|
|
|
|
|
|
return nil
|
2016-04-05 05:15:47 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// SetLogOut provide log stdout and stderr output
|
2016-04-05 05:15:47 +00:00
|
|
|
func SetLogOut(log *logrus.Logger, outString string) error {
|
|
|
|
switch outString {
|
|
|
|
case "stdout":
|
|
|
|
log.Out = os.Stdout
|
|
|
|
case "stderr":
|
|
|
|
log.Out = os.Stderr
|
|
|
|
default:
|
2021-01-23 01:39:06 +00:00
|
|
|
f, err := os.OpenFile(outString, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o600)
|
2016-04-05 05:15:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2016-04-05 05:15:47 +00:00
|
|
|
log.Out = f
|
|
|
|
}
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2016-04-05 05:15:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// SetLogLevel is define log level what you want
|
|
|
|
// log level: panic, fatal, error, warn, info and debug
|
2016-04-05 05:15:47 +00:00
|
|
|
func SetLogLevel(log *logrus.Logger, levelString string) error {
|
|
|
|
level, err := logrus.ParseLevel(levelString)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2016-04-05 05:15:47 +00:00
|
|
|
log.Level = level
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2016-04-05 05:15:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-04-07 06:16:59 +00:00
|
|
|
|
2016-04-08 08:56:32 +00:00
|
|
|
func colorForPlatForm(platform int) string {
|
|
|
|
switch platform {
|
2021-07-13 08:32:39 +00:00
|
|
|
case core.PlatFormIos:
|
2016-04-08 08:56:32 +00:00
|
|
|
return blue
|
2021-07-13 08:32:39 +00:00
|
|
|
case core.PlatFormAndroid:
|
2016-04-08 10:11:36 +00:00
|
|
|
return yellow
|
2021-07-13 08:32:39 +00:00
|
|
|
case core.PlatFormHuawei:
|
2020-09-04 03:01:21 +00:00
|
|
|
return green
|
2016-04-08 08:56:32 +00:00
|
|
|
default:
|
|
|
|
return reset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func typeForPlatForm(platform int) string {
|
|
|
|
switch platform {
|
2021-07-13 08:32:39 +00:00
|
|
|
case core.PlatFormIos:
|
2016-04-08 08:56:32 +00:00
|
|
|
return "ios"
|
2021-07-13 08:32:39 +00:00
|
|
|
case core.PlatFormAndroid:
|
2016-04-08 08:56:32 +00:00
|
|
|
return "android"
|
2021-07-13 08:32:39 +00:00
|
|
|
case core.PlatFormHuawei:
|
2020-09-04 03:01:21 +00:00
|
|
|
return "huawei"
|
2016-04-08 08:56:32 +00:00
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 13:04:05 +00:00
|
|
|
func hideToken(token string, markLen int) string {
|
2021-01-23 01:48:58 +00:00
|
|
|
if token == "" {
|
2016-05-16 12:36:56 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-05-16 13:04:05 +00:00
|
|
|
if len(token) < markLen*2 {
|
|
|
|
return strings.Repeat("*", len(token))
|
|
|
|
}
|
|
|
|
|
|
|
|
start := token[len(token)-markLen:]
|
|
|
|
end := token[0:markLen]
|
2016-05-16 12:36:56 +00:00
|
|
|
|
2016-05-16 13:04:05 +00:00
|
|
|
result := strings.Replace(token, start, strings.Repeat("*", markLen), -1)
|
|
|
|
result = strings.Replace(result, end, strings.Repeat("*", markLen), -1)
|
2016-05-16 12:36:56 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
// GetLogPushEntry get push data into log structure
|
|
|
|
func GetLogPushEntry(input *InputLog) LogPushEntry {
|
2017-04-10 03:46:48 +00:00
|
|
|
var errMsg string
|
2016-04-08 08:56:32 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
plat := typeForPlatForm(input.Platform)
|
2016-04-08 08:56:32 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
if input.Error != nil {
|
|
|
|
errMsg = input.Error.Error()
|
2016-04-08 08:56:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 09:57:34 +00:00
|
|
|
token := input.Token
|
2021-07-13 08:32:39 +00:00
|
|
|
if input.HideToken {
|
|
|
|
token = hideToken(input.Token, 10)
|
2016-05-16 13:04:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 03:46:48 +00:00
|
|
|
return LogPushEntry{
|
2021-07-13 08:32:39 +00:00
|
|
|
ID: input.ID,
|
|
|
|
Type: input.Status,
|
2016-04-08 08:56:32 +00:00
|
|
|
Platform: plat,
|
|
|
|
Token: token,
|
2021-07-13 08:32:39 +00:00
|
|
|
Message: input.Message,
|
2016-04-08 08:56:32 +00:00
|
|
|
Error: errMsg,
|
|
|
|
}
|
2017-04-10 03:46:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
// InputLog log request
|
|
|
|
type InputLog struct {
|
|
|
|
ID string
|
|
|
|
Status string
|
|
|
|
Token string
|
|
|
|
Message string
|
|
|
|
Platform int
|
|
|
|
Error error
|
|
|
|
HideToken bool
|
|
|
|
Format string
|
|
|
|
}
|
|
|
|
|
2017-04-10 03:46:48 +00:00
|
|
|
// LogPush record user push request and server response.
|
2021-08-01 09:12:16 +00:00
|
|
|
func LogPush(input *InputLog) LogPushEntry {
|
2017-04-10 03:46:48 +00:00
|
|
|
var platColor, resetColor, output string
|
|
|
|
|
|
|
|
if isTerm {
|
2021-07-13 08:32:39 +00:00
|
|
|
platColor = colorForPlatForm(input.Platform)
|
2017-04-10 03:46:48 +00:00
|
|
|
resetColor = reset
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
log := GetLogPushEntry(input)
|
2016-04-08 08:56:32 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
if input.Format == "json" {
|
2016-04-13 06:59:28 +00:00
|
|
|
logJSON, _ := json.Marshal(log)
|
2016-04-08 08:56:32 +00:00
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
output = string(logJSON)
|
2016-04-08 08:56:32 +00:00
|
|
|
} else {
|
2016-12-14 14:27:22 +00:00
|
|
|
var typeColor string
|
2021-07-13 08:32:39 +00:00
|
|
|
switch input.Status {
|
|
|
|
case core.SucceededPush:
|
2016-12-14 14:27:22 +00:00
|
|
|
if isTerm {
|
|
|
|
typeColor = green
|
|
|
|
}
|
|
|
|
|
2016-04-09 02:26:55 +00:00
|
|
|
output = fmt.Sprintf("|%s %s %s| %s%s%s [%s] %s",
|
2016-12-20 01:47:58 +00:00
|
|
|
typeColor, log.Type, resetColor,
|
|
|
|
platColor, log.Platform, resetColor,
|
2016-04-09 02:26:55 +00:00
|
|
|
log.Token,
|
|
|
|
log.Message,
|
|
|
|
)
|
2021-07-13 08:32:39 +00:00
|
|
|
case core.FailedPush:
|
2016-12-14 14:27:22 +00:00
|
|
|
if isTerm {
|
|
|
|
typeColor = red
|
|
|
|
}
|
|
|
|
|
2016-04-09 02:26:55 +00:00
|
|
|
output = fmt.Sprintf("|%s %s %s| %s%s%s [%s] | %s | Error Message: %s",
|
2016-12-20 01:47:58 +00:00
|
|
|
typeColor, log.Type, resetColor,
|
|
|
|
platColor, log.Platform, resetColor,
|
2016-04-09 02:26:55 +00:00
|
|
|
log.Token,
|
|
|
|
log.Message,
|
|
|
|
log.Error,
|
|
|
|
)
|
2016-04-08 08:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
switch input.Status {
|
|
|
|
case core.SucceededPush:
|
2017-01-27 10:19:39 +00:00
|
|
|
LogAccess.Info(output)
|
2021-07-13 08:32:39 +00:00
|
|
|
case core.FailedPush:
|
2017-01-27 10:19:39 +00:00
|
|
|
LogError.Error(output)
|
2016-04-08 08:56:32 +00:00
|
|
|
}
|
2021-08-01 09:12:16 +00:00
|
|
|
|
|
|
|
return log
|
2016-04-08 08:56:32 +00:00
|
|
|
}
|