main.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/res_init"
  10. "time"
  11. )
  12. type logFormatter struct {
  13. }
  14. // Format 定义日志输出格式
  15. func (l *logFormatter) Format(entry *log.Entry) ([]byte, error) {
  16. b := bytes.Buffer{}
  17. b.WriteString(fmt.Sprintf("[%s]", entry.Level.String()))
  18. b.WriteString(fmt.Sprintf("[%s]", entry.Time.Format("2006-01-02 15:04:05")))
  19. if entry.Context != nil {
  20. b.WriteString(fmt.Sprintf("[%s]", entry.Context.(*dto.Context).GetValue(dto.LogID)))
  21. }
  22. b.WriteString(fmt.Sprintf("[%s:%d]", entry.Caller.File, entry.Caller.Line))
  23. b.WriteString(entry.Message)
  24. b.WriteString("\n")
  25. return b.Bytes(), nil
  26. }
  27. var (
  28. gitHash string
  29. buildTime string
  30. goVersion string
  31. )
  32. func main() {
  33. // 设置日志格式为json格式
  34. //log.SetFormatter(&log.JSONFormatter{})
  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. }