logger_config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package logger
  2. import "log/slog"
  3. type LoggerConfig struct {
  4. // stdout config
  5. stdoutLogEnabled bool
  6. stdoutLogLevel slog.Level // INFO by default
  7. stdoutLogAddSource bool
  8. // file config
  9. fileLogEnabled bool
  10. fileLogLevel slog.Level // INFO by default
  11. fileLogAddSource bool
  12. filepath string
  13. // email config
  14. emailLogEnabled bool
  15. emailLogLevel slog.Level // INFO by default
  16. emailLogAddSource bool
  17. smtpHost string
  18. smtpPort uint
  19. smtpUser string
  20. smtpPassword string
  21. email string
  22. subject string
  23. }
  24. type LoggerConfigOption func(*LoggerConfig)
  25. type LoggerConfigOptions []LoggerConfigOption
  26. func (s *LoggerConfigOptions) Add(option LoggerConfigOption) {
  27. *s = append(*s, option)
  28. }
  29. func NewConfig(opts ...LoggerConfigOption) LoggerConfig {
  30. c := &LoggerConfig{}
  31. for _, opt := range opts {
  32. opt(c)
  33. }
  34. return *c
  35. }
  36. // stdout log
  37. func WithStdoutLogEnabled(enabled bool) LoggerConfigOption {
  38. return func(s *LoggerConfig) {
  39. s.stdoutLogEnabled = enabled
  40. }
  41. }
  42. func WithStdoutLogLevel(level slog.Level) LoggerConfigOption {
  43. return func(s *LoggerConfig) {
  44. s.stdoutLogLevel = level
  45. }
  46. }
  47. func WithStdoutLogAddSource(add bool) LoggerConfigOption {
  48. return func(s *LoggerConfig) {
  49. s.stdoutLogAddSource = add
  50. }
  51. }
  52. // file log
  53. func WithFileLogEnabled(enabled bool) LoggerConfigOption {
  54. return func(s *LoggerConfig) {
  55. s.fileLogEnabled = enabled
  56. }
  57. }
  58. func WithFileLogLevel(level slog.Level) LoggerConfigOption {
  59. return func(s *LoggerConfig) {
  60. s.fileLogLevel = level
  61. }
  62. }
  63. func WithFileLogAddSource(add bool) LoggerConfigOption {
  64. return func(s *LoggerConfig) {
  65. s.fileLogAddSource = add
  66. }
  67. }
  68. func WithFilepath(path string) LoggerConfigOption {
  69. return func(s *LoggerConfig) {
  70. s.filepath = path
  71. }
  72. }
  73. // email log
  74. func WithEmailLogEnabled(enabled bool) LoggerConfigOption {
  75. return func(s *LoggerConfig) {
  76. s.emailLogEnabled = enabled
  77. }
  78. }
  79. func WithEmailLogLevel(level slog.Level) LoggerConfigOption {
  80. return func(s *LoggerConfig) {
  81. s.emailLogLevel = level
  82. }
  83. }
  84. func WithEmailLogAddSource(add bool) LoggerConfigOption {
  85. return func(s *LoggerConfig) {
  86. s.emailLogAddSource = add
  87. }
  88. }
  89. func WithEmailRecipient(email string) LoggerConfigOption {
  90. return func(s *LoggerConfig) {
  91. s.email = email
  92. }
  93. }
  94. func WithEmailSubject(subject string) LoggerConfigOption {
  95. return func(s *LoggerConfig) {
  96. s.subject = subject
  97. }
  98. }
  99. func WithSMTPHost(host string) LoggerConfigOption {
  100. return func(s *LoggerConfig) {
  101. s.smtpHost = host
  102. }
  103. }
  104. func WithSMTPPort(port uint) LoggerConfigOption {
  105. return func(s *LoggerConfig) {
  106. s.smtpPort = port
  107. }
  108. }
  109. func WithSMTPUser(user string) LoggerConfigOption {
  110. return func(s *LoggerConfig) {
  111. s.smtpUser = user
  112. }
  113. }
  114. func WithSMTPPassword(password string) LoggerConfigOption {
  115. return func(s *LoggerConfig) {
  116. s.smtpPassword = password
  117. }
  118. }