main.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/utils/context"
  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. )
  36. func main() {
  37. // 设置日志格式为json格式
  38. log.SetFormatter(&logFormatter{})
  39. log.SetReportCaller(true)
  40. // 设置将日志输出到标准输出(默认的输出为stderr,标准错误)
  41. // 日志消息输出可以是任意的io.writer类型
  42. log.SetOutput(os.Stdout)
  43. var cst, _ = time.LoadLocation("Asia/Shanghai")
  44. time.Local = cst
  45. config.Init()
  46. if config.Instance != nil {
  47. switch config.Instance.LogLevel {
  48. case "":
  49. log.SetLevel(log.InfoLevel)
  50. case "debug":
  51. log.SetLevel(log.DebugLevel)
  52. case "info":
  53. log.SetLevel(log.InfoLevel)
  54. case "warn":
  55. log.SetLevel(log.WarnLevel)
  56. case "error":
  57. log.SetLevel(log.ErrorLevel)
  58. default:
  59. log.SetLevel(log.InfoLevel)
  60. }
  61. } else {
  62. log.SetLevel(log.InfoLevel)
  63. }
  64. log.Infoln("***************************************************")
  65. log.Infof("***\tServer Start Success Version:%s\n", config.Version)
  66. log.Infof("***\tGit Commit Hash: %s ", gitHash)
  67. log.Infof("***\tBuild TimeStamp: %s ", buildTime)
  68. log.Infof("***\tBuild GoLang Version: %s ", goVersion)
  69. log.Infoln("***************************************************")
  70. // 定时任务启动
  71. go cron_server.Start()
  72. // 核心服务启动
  73. res_init.Init()
  74. s := make(chan bool)
  75. <-s
  76. }