logger_config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package logger
  2. import "log/slog"
  3. type SMTPClient interface {
  4. Send(recipient string, subject string, content string, html bool) error
  5. }
  6. type Config struct {
  7. // stdout config
  8. stdoutLogEnabled bool
  9. stdoutLogLevel slog.Level // INFO by default
  10. stdoutLogAddSource bool
  11. // file config
  12. fileLogEnabled bool
  13. fileLogLevel slog.Level // INFO by default
  14. fileLogAddSource bool
  15. filepath string
  16. // email config
  17. emailLogEnabled bool
  18. emailLogLevel slog.Level // INFO by default
  19. emailLogAddSource bool
  20. smtpClient SMTPClient
  21. emailRecipient string
  22. emailSubject string
  23. }
  24. type ConfigOption func(*Config)
  25. type ConfigOptions []ConfigOption
  26. func (s *ConfigOptions) Add(option ConfigOption) {
  27. *s = append(*s, option)
  28. }
  29. func NewConfig(opts ...ConfigOption) Config {
  30. c := &Config{}
  31. for _, opt := range opts {
  32. opt(c)
  33. }
  34. return *c
  35. }
  36. // stdout
  37. func WithStdoutLogEnabled(enabled bool) ConfigOption {
  38. return func(s *Config) {
  39. s.stdoutLogEnabled = enabled
  40. }
  41. }
  42. func WithStdoutLogLevel(level string) ConfigOption {
  43. return func(s *Config) {
  44. var l slog.Level
  45. if err := l.UnmarshalText([]byte(level)); err == nil {
  46. s.stdoutLogLevel = l
  47. }
  48. }
  49. }
  50. func WithStdoutLogAddSource(add bool) ConfigOption {
  51. return func(s *Config) {
  52. s.stdoutLogAddSource = add
  53. }
  54. }
  55. // file
  56. func WithFileLogEnabled(enabled bool) ConfigOption {
  57. return func(s *Config) {
  58. s.fileLogEnabled = enabled
  59. }
  60. }
  61. func WithFileLogLevel(level string) ConfigOption {
  62. return func(s *Config) {
  63. var l slog.Level
  64. if err := l.UnmarshalText([]byte(level)); err == nil {
  65. s.fileLogLevel = l
  66. }
  67. }
  68. }
  69. func WithFileLogAddSource(add bool) ConfigOption {
  70. return func(s *Config) {
  71. s.fileLogAddSource = add
  72. }
  73. }
  74. func WithFilepath(path string) ConfigOption {
  75. return func(s *Config) {
  76. s.filepath = path
  77. }
  78. }
  79. // email
  80. func WithEmailLogEnabled(enabled bool) ConfigOption {
  81. return func(s *Config) {
  82. s.emailLogEnabled = enabled
  83. }
  84. }
  85. func WithEmailLogLevel(level string) ConfigOption {
  86. return func(s *Config) {
  87. var l slog.Level
  88. if err := l.UnmarshalText([]byte(level)); err == nil {
  89. s.emailLogLevel = l
  90. }
  91. }
  92. }
  93. func WithEmailLogAddSource(add bool) ConfigOption {
  94. return func(s *Config) {
  95. s.emailLogAddSource = add
  96. }
  97. }
  98. func WithEmailRecipient(email string) ConfigOption {
  99. return func(s *Config) {
  100. s.emailRecipient = email
  101. }
  102. }
  103. func WithEmailSubject(subject string) ConfigOption {
  104. return func(s *Config) {
  105. s.emailSubject = subject
  106. }
  107. }
  108. func WithSMTPClient(c SMTPClient) ConfigOption {
  109. return func(s *Config) {
  110. s.smtpClient = c
  111. }
  112. }