log.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package logging
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "time"
  10. "../../pkg/file"
  11. "../setting"
  12. "github.com/gin-gonic/gin"
  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. tchan := time.Tick(24 * time.Hour)
  46. for {
  47. select {
  48. case <-tchan:
  49. F.Close()
  50. //fmt.Println("t1定时器")
  51. filePath := DefaultPath + getLogFilePath()
  52. fileName := getLogFileName()
  53. F, err = file.MustOpen(fileName, filePath)
  54. if err != nil {
  55. log.Fatalf("logging.Setup err: %v", err)
  56. }
  57. logger = log.New(F, DefaultPrefix, log.LstdFlags)
  58. gin.DefaultWriter = io.MultiWriter(F, os.Stdout)
  59. }
  60. }
  61. }()
  62. //// gin log设置
  63. //gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
  64. //
  65. // // 你的自定义格式
  66. // return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"%s\n",
  67. // param.ClientIP,
  68. // param.TimeStamp.Format(time.RFC1123),
  69. // param.Method,
  70. // param.Path,
  71. // param.Request.Proto,
  72. // param.StatusCode,
  73. // param.Latency,
  74. // param.Request.UserAgent(),
  75. // param.ErrorMessage,
  76. // param.Request.Form,
  77. // )
  78. //})
  79. }
  80. func Debug(v ...interface{}) {
  81. setPrefix(DEBUG)
  82. logger.Println(v)
  83. }
  84. func Info(v ...interface{}) {
  85. setPrefix(INFO)
  86. logger.Println(v)
  87. }
  88. func Warn(v ...interface{}) {
  89. setPrefix(WARNING)
  90. logger.Println(v)
  91. }
  92. func Error(v ...interface{}) {
  93. setPrefix(ERROR)
  94. logger.Println(v)
  95. }
  96. func Fatal(v ...interface{}) {
  97. setPrefix(FATAL)
  98. logger.Fatalln(v)
  99. }
  100. func setPrefix(level Level) {
  101. _, file, line, ok := runtime.Caller(DefaultCallerDepth)
  102. if ok {
  103. logPrefix = fmt.Sprintf("[%s][%s:%d]", levelFlags[level], filepath.Base(file), line)
  104. } else {
  105. logPrefix = fmt.Sprintf("[%s]", levelFlags[level])
  106. }
  107. logger.SetPrefix(logPrefix)
  108. }