main.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. 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 cst, _ = time.LoadLocation("Asia/Shanghai")
  45. time.Local = cst
  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. log.Infoln("***************************************************")
  66. log.Infof("***\tServer Start Success Version:%s\n", version)
  67. log.Infof("***\tGit Commit Hash: %s ", gitHash)
  68. log.Infof("***\tBuild TimeStamp: %s ", buildTime)
  69. log.Infof("***\tBuild GoLang Version: %s ", goVersion)
  70. log.Infoln("***************************************************")
  71. // 定时任务启动
  72. go cron_server.Start()
  73. // 核心服务启动
  74. res_init.Init()
  75. s := make(chan bool)
  76. <-s
  77. }