main.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/dto"
  10. "pmail/res_init"
  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.(*dto.Context).GetValue(dto.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(&log.JSONFormatter{})
  36. log.SetFormatter(&logFormatter{})
  37. log.SetReportCaller(true)
  38. // 设置将日志输出到标准输出(默认的输出为stderr,标准错误)
  39. // 日志消息输出可以是任意的io.writer类型
  40. log.SetOutput(os.Stdout)
  41. var cst, _ = time.LoadLocation("Asia/Shanghai")
  42. time.Local = cst
  43. config.Init()
  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. log.Infoln("***************************************************")
  59. log.Infof("***\tServer Start Success Version:%s\n", config.Version)
  60. log.Infof("***\tGit Commit Hash: %s ", gitHash)
  61. log.Infof("***\tBuild TimeStamp: %s ", buildTime)
  62. log.Infof("***\tBuild GoLang Version: %s ", goVersion)
  63. log.Infoln("***************************************************")
  64. // 定时任务启动
  65. go cron_server.Start()
  66. // 核心服务启动
  67. res_init.Init()
  68. s := make(chan bool)
  69. <-s
  70. }