main.go 2.0 KB

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