You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

global_logger.go 3.9 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package logger
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. nested "github.com/antonfisher/nested-logrus-formatter"
  8. "github.com/sirupsen/logrus"
  9. "gitlink.org.cn/cloudream/common/utils/reflect2"
  10. )
  11. // Std 是一个输出日志到标准输出的Logger,适用于没有设计好日志输出方案时的临时使用。
  12. var Std Logger
  13. // init 初始化包,设置日志格式为不带颜色的Nested格式,日志级别为Debug,输出到标准输出。
  14. func init() {
  15. logger := logrus.New()
  16. logger.SetFormatter(&nested.Formatter{
  17. TimestampFormat: "2006-01-02 15:04:05",
  18. NoColors: true,
  19. NoFieldsColors: true,
  20. })
  21. logrus.SetLevel(logrus.DebugLevel)
  22. logrus.SetOutput(os.Stdout)
  23. Std = &logrusLogger{entry: logger.WithField("TODO", "")}
  24. }
  25. // Init 初始化全局默认的日志器,根据配置设置日志级别和输出位置。
  26. //
  27. // 参数:
  28. //
  29. // cfg *Config: 日志配置项,包括日志级别和输出位置等。
  30. //
  31. // 返回值:
  32. //
  33. // error: 初始化过程中的任何错误。
  34. func Init(cfg *Config) error {
  35. logrus.SetFormatter(&nested.Formatter{
  36. TimestampFormat: "2006-01-02 15:04:05",
  37. NoColors: true,
  38. NoFieldsColors: true,
  39. })
  40. // 设置日志级别
  41. level, ok := loggerLevels[strings.ToUpper(cfg.Level)]
  42. if !ok {
  43. return fmt.Errorf("invalid log level: %s", cfg.Level)
  44. }
  45. logrus.SetLevel(level)
  46. // 设置日志输出位置
  47. output := strings.ToUpper(cfg.Output)
  48. if output == OUTPUT_FILE {
  49. logFilePath := filepath.Join(cfg.OutputDirectory, cfg.OutputFileName+".log")
  50. // 创建日志文件所在的目录
  51. if err := os.MkdirAll(cfg.OutputDirectory, 0755); err != nil {
  52. return err
  53. }
  54. // 打开或创建日志文件
  55. file, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0755)
  56. if err != nil {
  57. return err
  58. }
  59. logrus.SetOutput(file)
  60. } else if output == OUTPUT_STDOUT {
  61. logrus.SetOutput(os.Stdout)
  62. } else {
  63. logrus.SetOutput(os.Stdout)
  64. logrus.Warnf("unsupported output: %s, will output to stdout", output)
  65. }
  66. return nil
  67. }
  68. // 下面是日志记录的方法,它们分别对应不同的日志级别和格式。
  69. // 这些方法最终都会调用logrus对应的方法来记录日志。
  70. func Trace(args ...interface{}) {
  71. logrus.Trace(args...)
  72. }
  73. func Tracef(format string, args ...interface{}) {
  74. logrus.Tracef(format, args...)
  75. }
  76. func Debug(args ...interface{}) {
  77. logrus.Debug(args...)
  78. }
  79. func Debugf(format string, args ...interface{}) {
  80. logrus.Debugf(format, args...)
  81. }
  82. func Info(args ...interface{}) {
  83. logrus.Info(args...)
  84. }
  85. func Infof(format string, args ...interface{}) {
  86. logrus.Infof(format, args...)
  87. }
  88. func Warn(args ...interface{}) {
  89. logrus.Warn(args...)
  90. }
  91. func Warnf(format string, args ...interface{}) {
  92. logrus.Warnf(format, args...)
  93. }
  94. func Error(args ...interface{}) {
  95. logrus.Error(args...)
  96. }
  97. func Errorf(format string, args ...interface{}) {
  98. logrus.Errorf(format, args...)
  99. }
  100. func Fatal(args ...interface{}) {
  101. logrus.Fatal(args...)
  102. }
  103. func Fatalf(format string, args ...interface{}) {
  104. logrus.Fatalf(format, args...)
  105. }
  106. func Panic(args ...interface{}) {
  107. logrus.Panic(args...)
  108. }
  109. func Panicf(format string, args ...interface{}) {
  110. logrus.Panicf(format, args...)
  111. }
  112. // WithField 创建并返回一个新的Logger,该Logger在记录日志时会包含额外的字段。
  113. //
  114. // 参数:
  115. //
  116. // key string: 字段键。
  117. // val any: 字段值。
  118. //
  119. // 返回值:
  120. //
  121. // Logger: 包含指定字段的Logger。
  122. func WithField(key string, val any) Logger {
  123. return &logrusLogger{
  124. entry: logrus.WithField(key, val),
  125. }
  126. }
  127. // WithType 创建并返回一个新的Logger,该Logger在记录日志时会包含类型的字段。
  128. //
  129. // 参数:
  130. //
  131. // key string: 字段键。
  132. //
  133. // 返回值:
  134. //
  135. // Logger: 包含指定类型字段的Logger。
  136. func WithType[T any](key string) Logger {
  137. return &logrusLogger{
  138. entry: logrus.WithField(key, reflect2.TypeOf[T]().Name()),
  139. }
  140. }