log.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package log
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "github.com/rs/zerolog"
  7. "github.com/rs/zerolog/log"
  8. "gopkg.in/natefinch/lumberjack.v2"
  9. "github.com/spf13/viper"
  10. )
  11. func NewLogger(conf *viper.Viper) {
  12. // UNIX Time is faster and smaller than most timestamps
  13. zerolog.TimeFieldFormat = "2006/01/02 15:04:05.000000"
  14. consoleWriter := zerolog.ConsoleWriter{
  15. Out: io.MultiWriter(
  16. os.Stdout,
  17. withLumberjack(conf),
  18. ),
  19. TimeFormat: "2006/01/02 15:04:05.000000",
  20. }
  21. log.Logger = log.Output(consoleWriter).With().Caller().Logger()
  22. }
  23. func withLumberjack(conf *viper.Viper) *lumberjack.Logger {
  24. return &lumberjack.Logger{
  25. Filename: fmt.Sprintf(conf.GetString("log.log_file_name"), conf.GetString("server.name")), // Log file path
  26. MaxSize: conf.GetInt("log.max_size"), // Maximum size unit for each log file: M
  27. MaxBackups: conf.GetInt("log.max_backups"), // The maximum number of backups that can be saved for log files
  28. MaxAge: conf.GetInt("log.max_age"), // Maximum number of days the file can be saved
  29. Compress: conf.GetBool("log.compress"), // Compression or not
  30. }
  31. }