logger_config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package mhayaLogger
  2. import (
  3. "fmt"
  4. cprofile "github.com/mhaya/profile"
  5. "time"
  6. cfacade "github.com/mhaya/facade"
  7. "go.uber.org/zap/zapcore"
  8. )
  9. type (
  10. Config struct {
  11. LogLevel string `json:"level"` // 输出日志等级
  12. StackLevel string `json:"stack_level"` // 堆栈输出日志等级
  13. EnableConsole bool `json:"enable_console"` // 是否控制台输出
  14. EnableWriteFile bool `json:"enable_write_file"` // 是否输出文件(必需配置FilePath)
  15. MaxAge int `json:"max_age"` // 最大保留天数(达到限制,则会被清理)
  16. TimeFormat string `json:"time_format"` // 打印时间输出格式
  17. PrintCaller bool `json:"print_caller"` // 是否打印调用函数
  18. RotationTime int `json:"rotation_time"` // 日期分割时间(秒)
  19. FileLinkPath string `json:"file_link_path"` // 日志文件连接路径
  20. FilePathFormat string `json:"file_path_format"` // 日志文件路径格式
  21. IncludeStdout bool `json:"include_stdout"` // 是否包含os.stdout输出
  22. IncludeStderr bool `json:"include_stderr"` // 是否包含os.stderr输出
  23. }
  24. )
  25. func defaultConsoleConfig() *Config {
  26. config := &Config{
  27. LogLevel: "debug",
  28. StackLevel: "error",
  29. EnableConsole: true,
  30. EnableWriteFile: false,
  31. MaxAge: 7,
  32. TimeFormat: "15:04:05.000", //2006-01-02 15:04:05.000
  33. PrintCaller: true,
  34. RotationTime: 86400,
  35. FileLinkPath: "logs/debug.log",
  36. FilePathFormat: "logs/debug_%Y%m%d%H%M.log",
  37. IncludeStdout: false,
  38. IncludeStderr: false,
  39. }
  40. return config
  41. }
  42. func NewConfig(jsonConfig cfacade.ProfileJSON) (*Config, error) {
  43. config := &Config{
  44. LogLevel: jsonConfig.GetString("level", "debug"),
  45. StackLevel: jsonConfig.GetString("stack_level", "error"),
  46. EnableConsole: jsonConfig.GetBool("enable_console", true),
  47. EnableWriteFile: jsonConfig.GetBool("enable_write_file", false),
  48. MaxAge: jsonConfig.GetInt("max_age", 7),
  49. TimeFormat: jsonConfig.GetString("time_format", "15:04:05.000"),
  50. PrintCaller: jsonConfig.GetBool("print_caller", true),
  51. RotationTime: jsonConfig.GetInt("rotation_time", 86400),
  52. FileLinkPath: jsonConfig.GetString("file_link_path", ""),
  53. FilePathFormat: jsonConfig.GetString("file_path_format", ""),
  54. IncludeStdout: jsonConfig.GetBool("include_stdout", false),
  55. IncludeStderr: jsonConfig.GetBool("include_stderr", false),
  56. }
  57. if config.EnableWriteFile {
  58. if config.FileLinkPath == "" {
  59. defaultValue := fmt.Sprintf("logs/%s.log", config.LogLevel)
  60. config.FileLinkPath = jsonConfig.GetString("file_link_path", defaultValue)
  61. }
  62. if config.FilePathFormat == "" {
  63. defaultValue := fmt.Sprintf("logs/%s_%s.log", config.LogLevel, "%Y%m%d%H%M")
  64. config.FilePathFormat = jsonConfig.GetString("file_path_format", defaultValue)
  65. }
  66. }
  67. return config, nil
  68. }
  69. func NewConfigWithName(refLoggerName string) (*Config, error) {
  70. loggerConfig := cprofile.GetConfig("logger")
  71. if loggerConfig.LastError() != nil {
  72. return nil, loggerConfig.LastError()
  73. }
  74. jsonConfig := loggerConfig.GetConfig(refLoggerName)
  75. if jsonConfig.LastError() != nil {
  76. return nil, jsonConfig.LastError()
  77. }
  78. return NewConfig(jsonConfig)
  79. }
  80. func (c *Config) TimeEncoder() zapcore.TimeEncoder {
  81. return func(time time.Time, encoder zapcore.PrimitiveArrayEncoder) {
  82. encoder.AppendString(time.Format(c.TimeFormat))
  83. }
  84. }