1
0

log.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package logger
  2. import (
  3. rotatelogs "github.com/lestrrat-go/file-rotatelogs"
  4. "io"
  5. "os"
  6. "time"
  7. //"gogs.baozhida.cn/zoie/OAuth-core/debug/writer"
  8. "gogs.baozhida.cn/zoie/OAuth-core/logger"
  9. log "gogs.baozhida.cn/zoie/OAuth-core/logger"
  10. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  11. "gogs.baozhida.cn/zoie/OAuth-core/plugins/logger/zap"
  12. )
  13. // SetupLogger 日志 cap 单位为kb
  14. func SetupLogger(opts ...Option) logger.Logger {
  15. op := setDefault()
  16. for _, o := range opts {
  17. o(&op)
  18. }
  19. if !pkg.PathExist(op.path) {
  20. err := pkg.PathCreate(op.path)
  21. if err != nil {
  22. log.Fatalf("create dir error: %s", err.Error())
  23. }
  24. }
  25. var err error
  26. var output io.Writer
  27. switch op.stdout {
  28. case "file":
  29. //output, err = writer.NewFileWriter(
  30. // writer.WithPath(op.path),
  31. // writer.WithCap(op.cap<<10),
  32. //)
  33. //if err != nil {
  34. // log.Fatal("logger setup error: %s", err.Error())
  35. //}
  36. output, err = rotatelogs.New(
  37. op.path+"/%Y-%m-%d.log",
  38. rotatelogs.WithLinkName(op.path+"/log.log"),
  39. rotatelogs.WithMaxAge(time.Duration(op.maxDays)*24*time.Hour),
  40. rotatelogs.WithRotationTime(24*time.Hour),
  41. )
  42. if err != nil {
  43. log.Fatal("logger setup error: %s", err.Error())
  44. }
  45. default:
  46. output = os.Stdout
  47. }
  48. var level logger.Level
  49. level, err = logger.GetLevel(op.level)
  50. if err != nil {
  51. log.Fatalf("get logger level error, %s", err.Error())
  52. }
  53. switch op.driver {
  54. case "zap":
  55. log.DefaultLogger, err = zap.NewLogger(logger.WithLevel(level), logger.WithOutput(output), zap.WithCallerSkip(2))
  56. if err != nil {
  57. log.Fatalf("new zap logger error, %s", err.Error())
  58. }
  59. //case "logrus":
  60. // setLogger = logrus.NewLogger(logger.WithLevel(level), logger.WithOutput(output), logrus.ReportCaller())
  61. default:
  62. log.DefaultLogger = logger.NewLogger(logger.WithLevel(level), logger.WithOutput(output))
  63. }
  64. return log.DefaultLogger
  65. }