main.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // 设置日志级别为warn以上
  41. log.SetLevel(log.DebugLevel)
  42. var cst, _ = time.LoadLocation("Asia/Shanghai")
  43. time.Local = cst
  44. res_init.Init()
  45. log.Infoln("***************************************************")
  46. log.Infof("***\tServer Start Success Version:%s\n", config.Version)
  47. log.Infof("***\tGit Commit Hash: %s ", gitHash)
  48. log.Infof("***\tBuild TimeStamp: %s ", buildTime)
  49. log.Infof("***\tBuild GoLang Version: %s ", goVersion)
  50. log.Infoln("***************************************************")
  51. s := make(chan bool)
  52. <-s
  53. }