main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. log "github.com/sirupsen/logrus"
  6. "os"
  7. "pmail/config"
  8. "pmail/cron_server"
  9. "pmail/res_init"
  10. "pmail/signal"
  11. "pmail/utils/context"
  12. "time"
  13. )
  14. type logFormatter struct {
  15. }
  16. // Format 定义日志输出格式
  17. func (l *logFormatter) Format(entry *log.Entry) ([]byte, error) {
  18. b := bytes.Buffer{}
  19. b.WriteString(fmt.Sprintf("[%s]", entry.Level.String()))
  20. b.WriteString(fmt.Sprintf("[%s]", entry.Time.Format("2006-01-02 15:04:05")))
  21. if entry.Context != nil {
  22. ctx := entry.Context.(*context.Context)
  23. if ctx != nil {
  24. b.WriteString(fmt.Sprintf("[%s]", ctx.GetValue(context.LogID)))
  25. }
  26. }
  27. b.WriteString(fmt.Sprintf("[%s:%d]", entry.Caller.File, entry.Caller.Line))
  28. b.WriteString(entry.Message)
  29. b.WriteString("\n")
  30. return b.Bytes(), nil
  31. }
  32. var (
  33. gitHash string
  34. buildTime string
  35. goVersion string
  36. version string
  37. )
  38. func main() {
  39. // 设置日志格式为json格式
  40. log.SetFormatter(&logFormatter{})
  41. log.SetReportCaller(true)
  42. // 设置将日志输出到标准输出(默认的输出为stderr,标准错误)
  43. // 日志消息输出可以是任意的io.writer类型
  44. log.SetOutput(os.Stdout)
  45. var cstZone = time.FixedZone("CST", 8*3600)
  46. time.Local = cstZone
  47. config.Init()
  48. if config.Instance != nil {
  49. switch config.Instance.LogLevel {
  50. case "":
  51. log.SetLevel(log.InfoLevel)
  52. case "debug":
  53. log.SetLevel(log.DebugLevel)
  54. case "info":
  55. log.SetLevel(log.InfoLevel)
  56. case "warn":
  57. log.SetLevel(log.WarnLevel)
  58. case "error":
  59. log.SetLevel(log.ErrorLevel)
  60. default:
  61. log.SetLevel(log.InfoLevel)
  62. }
  63. } else {
  64. log.SetLevel(log.InfoLevel)
  65. }
  66. if version == "" {
  67. version = "TestVersion"
  68. }
  69. log.Infoln("*******************************************************************")
  70. log.Infof("***\tServer Start Success \n")
  71. log.Infof("***\tServer Version: %s \n", version)
  72. log.Infof("***\tGit Commit Hash: %s ", gitHash)
  73. log.Infof("***\tBuild Date: %s ", buildTime)
  74. log.Infof("***\tBuild GoLang Version: %s ", goVersion)
  75. log.Infoln("*******************************************************************")
  76. // 定时任务启动
  77. go cron_server.Start()
  78. // 核心服务启动
  79. res_init.Init(version)
  80. log.Warnf("Server Stoped \n")
  81. }
  82. func stop() {
  83. log.Warnf("Server Stop \n")
  84. signal.RestartChan <- true
  85. log.Warnf("Server Stop2 \n")
  86. }