log.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package logging
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. "kpt.xdmy/pkg/file"
  12. "kpt.xdmy/pkg/setting"
  13. )
  14. type Level int
  15. var (
  16. F *os.File
  17. DefaultPrefix = ""
  18. DefaultCallerDepth = 2
  19. DefaultPath string
  20. logger *log.Logger
  21. logPrefix = ""
  22. levelFlags = []string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL"}
  23. )
  24. const (
  25. DEBUG Level = iota
  26. INFO
  27. WARNING
  28. ERROR
  29. FATAL
  30. )
  31. func Setup() {
  32. var err error
  33. //tchan :=time.Tick(24*time.Hour)
  34. DefaultPath, _ = setting.GetCurrentPath()
  35. filePath := DefaultPath + getLogFilePath()
  36. fileName := getLogFileName()
  37. F, err = file.MustOpen(fileName, filePath)
  38. if err != nil {
  39. log.Fatalf("logging.Setup err: %v", err)
  40. }
  41. println(filePath)
  42. logger = log.New(F, DefaultPrefix, log.LstdFlags)
  43. //gin.DefaultWriter = io.MultiWriter(F, os.Stdout)
  44. go func() {
  45. defer func() {
  46. if err := recover(); err != nil {
  47. log.Fatalf("logging.recover err: %v", err)
  48. }
  49. }()
  50. tchan := time.Tick(24 * time.Hour)
  51. for {
  52. select {
  53. case <-tchan:
  54. F.Close()
  55. //fmt.Println("t1定时器")
  56. filePath := DefaultPath + getLogFilePath()
  57. fileName := getLogFileName()
  58. F, err = file.MustOpen(fileName, filePath)
  59. if err != nil {
  60. log.Fatalf("logging.Setup err: %v", err)
  61. }
  62. logger = log.New(F, DefaultPrefix, log.LstdFlags)
  63. gin.DefaultWriter = io.MultiWriter(F, os.Stdout)
  64. }
  65. }
  66. }()
  67. //// gin log设置
  68. //gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
  69. //
  70. // // 你的自定义格式
  71. // return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"%s\n",
  72. // param.ClientIP,
  73. // param.TimeStamp.Format(time.RFC1123),
  74. // param.Method,
  75. // param.Path,
  76. // param.Request.Proto,
  77. // param.StatusCode,
  78. // param.Latency,
  79. // param.Request.UserAgent(),
  80. // param.ErrorMessage,
  81. // param.Request.Form,
  82. // )
  83. //})
  84. }
  85. func Debug(v ...interface{}) {
  86. setPrefix(DEBUG)
  87. logger.Println(v)
  88. }
  89. func Info(v ...interface{}) {
  90. setPrefix(INFO)
  91. logger.Println(v)
  92. }
  93. func Warn(v ...interface{}) {
  94. setPrefix(WARNING)
  95. logger.Println(v)
  96. }
  97. func Error(v ...interface{}) {
  98. setPrefix(ERROR)
  99. logger.Println(v)
  100. }
  101. func Fatal(v ...interface{}) {
  102. setPrefix(FATAL)
  103. logger.Fatalln(v)
  104. }
  105. func setPrefix(level Level) {
  106. _, file, line, ok := runtime.Caller(DefaultCallerDepth)
  107. if ok {
  108. logPrefix = fmt.Sprintf("[%s][%s:%d]", levelFlags[level], filepath.Base(file), line)
  109. } else {
  110. logPrefix = fmt.Sprintf("[%s]", levelFlags[level])
  111. }
  112. logger.SetPrefix(logPrefix)
  113. }