main.go 2.2 KB

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