lclog.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package logger
  2. import (
  3. "github.com/druidcaesa/gotool"
  4. "github.com/sirupsen/logrus"
  5. "iot_manager_service/config"
  6. "os"
  7. "path"
  8. "time"
  9. )
  10. var Logger *logrus.Logger
  11. func InitLog() {
  12. logFilePath := config.Instance().Logger.Path
  13. logFileName := config.Instance().Logger.Name
  14. err := os.MkdirAll(logFilePath, os.ModeDir)
  15. if err != nil {
  16. gotool.Logs.ErrorLog().Println(err)
  17. panic(err)
  18. }
  19. // 日志文件
  20. fileName := path.Join(logFilePath, logFileName) + time.Now().Format("20060102") + ".log"
  21. if !gotool.FileUtils.Exists(fileName) {
  22. create, err := os.Create(fileName)
  23. if err != nil {
  24. gotool.Logs.ErrorLog().Println(err)
  25. panic(err)
  26. }
  27. defer create.Close()
  28. }
  29. // 写入文件
  30. src, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
  31. if err != nil {
  32. panic(err)
  33. }
  34. // 实例化
  35. logger := logrus.New()
  36. // 设置输出
  37. logger.Out = src
  38. // 设置日志级别
  39. logger.SetLevel(logrus.DebugLevel)
  40. logger.Formatter = &logrus.JSONFormatter{
  41. TimestampFormat: "2006-01-02 15:04:05.000",
  42. }
  43. Logger = logger
  44. }