rotatelogs_unix.go 899 B

1234567891011121314151617181920212223242526272829303132
  1. //go:build !windows
  2. // +build !windows
  3. package utils
  4. import (
  5. "os"
  6. "path"
  7. "time"
  8. "github.com/flipped-aurora/gin-vue-admin/server/global"
  9. zaprotatelogs "github.com/lestrrat-go/file-rotatelogs"
  10. "go.uber.org/zap/zapcore"
  11. )
  12. //@author: [SliverHorn](https://github.com/SliverHorn)
  13. //@function: GetWriteSyncer
  14. //@description: zap logger中加入file-rotatelogs
  15. //@return: zapcore.WriteSyncer, error
  16. func GetWriteSyncer() (zapcore.WriteSyncer, error) {
  17. fileWriter, err := zaprotatelogs.New(
  18. path.Join(global.GVA_CONFIG.Zap.Director, "%Y-%m-%d.log"),
  19. zaprotatelogs.WithLinkName(global.GVA_CONFIG.Zap.LinkName),
  20. zaprotatelogs.WithMaxAge(7*24*time.Hour),
  21. zaprotatelogs.WithRotationTime(24*time.Hour),
  22. )
  23. if global.GVA_CONFIG.Zap.LogInConsole {
  24. return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err
  25. }
  26. return zapcore.AddSync(fileWriter), err
  27. }