main.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. log "github.com/sirupsen/logrus"
  6. "os"
  7. "pmail/config"
  8. "pmail/dto"
  9. "pmail/dto/parsemail"
  10. "pmail/hooks"
  11. "pmail/http_server"
  12. "pmail/mysql"
  13. "pmail/session"
  14. "pmail/smtp_server"
  15. "time"
  16. )
  17. type logFormatter struct {
  18. }
  19. // Format 定义日志输出格式
  20. func (l *logFormatter) Format(entry *log.Entry) ([]byte, error) {
  21. b := bytes.Buffer{}
  22. b.WriteString(fmt.Sprintf("[%s]", entry.Level.String()))
  23. b.WriteString(fmt.Sprintf("[%s]", entry.Time.Format("2006-01-02 15:04:05")))
  24. if entry.Context != nil {
  25. b.WriteString(fmt.Sprintf("[%s]", entry.Context.(*dto.Context).GetValue(dto.LogID)))
  26. }
  27. b.WriteString(fmt.Sprintf("[%s:%d]", entry.Caller.File, entry.Caller.Line))
  28. b.WriteString(entry.Message)
  29. b.WriteString("\n")
  30. return b.Bytes(), nil
  31. }
  32. var (
  33. gitHash string
  34. buildTime string
  35. goVersion string
  36. )
  37. func main() {
  38. // 设置日志格式为json格式
  39. //log.SetFormatter(&log.JSONFormatter{})
  40. log.SetFormatter(&logFormatter{})
  41. log.SetReportCaller(true)
  42. // 设置将日志输出到标准输出(默认的输出为stderr,标准错误)
  43. // 日志消息输出可以是任意的io.writer类型
  44. log.SetOutput(os.Stdout)
  45. // 设置日志级别为warn以上
  46. log.SetLevel(log.DebugLevel)
  47. var cst, _ = time.LoadLocation("Asia/Shanghai")
  48. time.Local = cst
  49. config.Init()
  50. parsemail.Init()
  51. mysql.Init()
  52. session.Init()
  53. hooks.Init()
  54. // smtp server start
  55. go smtp_server.Start()
  56. // http server start
  57. go http_server.Start()
  58. log.Infoln("***************************************************")
  59. log.Infoln("***\tServer Start Success Version:1.0.0")
  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. s := make(chan bool)
  65. <-s
  66. }