tcp server 用于给andriod 客户端定时发送消息
fix
liuxiaolong
2021-05-19 ed8c09655a43d9ec44142fb968e5abfbb69416cb
log/log.go
@@ -2,6 +2,9 @@
import (
   "io/ioutil"
   "os"
   "os/exec"
   "path/filepath"
   "time"
   rotatelogs "github.com/lestrrat-go/file-rotatelogs"
@@ -13,26 +16,38 @@
func init() {
   Log = NewLogger()
   Log.SetLevel(logrus.ErrorLevel)
}
func SetLogLevel(level string){
   if level == "ErrorLevel" {
      Log.SetLevel(logrus.ErrorLevel)
   } else if level == "InfoLevel" {
      Log.SetLevel(logrus.InfoLevel)
   } else {
      Log.SetLevel(logrus.InfoLevel)
   }
}
func NewLogger() *logrus.Logger {
   if Log != nil {
      return Log
   }
   infopath := "log/info.log"
   rootPath := GetAppRootPath()
   infopath := rootPath + "/logs/info.log"
   infowriter, _ := rotatelogs.New(
      infopath+".%Y%m%d%H%M",
      rotatelogs.WithLinkName(infopath),
      rotatelogs.WithMaxAge(time.Duration(86400)*time.Second),
      rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
      rotatelogs.WithMaxAge(30*24*time.Hour),
      rotatelogs.WithRotationTime(24*time.Hour),
   )
   errorpath := "log/error.log"
   errorpath := rootPath + "/logs/error.log"
   errorwriter, _ := rotatelogs.New(
      errorpath+".%Y%m%d%H%M",
      rotatelogs.WithLinkName(errorpath),
      rotatelogs.WithMaxAge(time.Duration(86400)*time.Second),
      rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
      rotatelogs.WithMaxAge(30*24*time.Hour),
      rotatelogs.WithRotationTime(24*time.Hour),
   )
   Log = logrus.New()
@@ -46,3 +61,15 @@
   ))
   return Log
}
func GetAppRootPath() string {
   file,err:= exec.LookPath(os.Args[0])
   if err != nil {
      return ""
   }
   p, err := filepath.Abs(file)
   if err != nil {
      return ""
   }
   return filepath.Dir(p)
}