logger_config.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 slog.Level) ConfigOption {
  43. return func(s *Config) {
  44. s.stdoutLogLevel = level
  45. }
  46. }
  47. func WithStdoutLogAddSource(add bool) ConfigOption {
  48. return func(s *Config) {
  49. s.stdoutLogAddSource = add
  50. }
  51. }
  52. // file
  53. func WithFileLogEnabled(enabled bool) ConfigOption {
  54. return func(s *Config) {
  55. s.fileLogEnabled = enabled
  56. }
  57. }
  58. func WithFileLogLevel(level slog.Level) ConfigOption {
  59. return func(s *Config) {
  60. s.fileLogLevel = level
  61. }
  62. }
  63. func WithFileLogAddSource(add bool) ConfigOption {
  64. return func(s *Config) {
  65. s.fileLogAddSource = add
  66. }
  67. }
  68. func WithFilepath(path string) ConfigOption {
  69. return func(s *Config) {
  70. s.filepath = path
  71. }
  72. }
  73. // email
  74. func WithEmailLogEnabled(enabled bool) ConfigOption {
  75. return func(s *Config) {
  76. s.emailLogEnabled = enabled
  77. }
  78. }
  79. func WithEmailLogLevel(level slog.Level) ConfigOption {
  80. return func(s *Config) {
  81. s.emailLogLevel = level
  82. }
  83. }
  84. func WithEmailLogAddSource(add bool) ConfigOption {
  85. return func(s *Config) {
  86. s.emailLogAddSource = add
  87. }
  88. }
  89. func WithEmailRecipient(email string) ConfigOption {
  90. return func(s *Config) {
  91. s.emailRecipient = email
  92. }
  93. }
  94. func WithEmailSubject(subject string) ConfigOption {
  95. return func(s *Config) {
  96. s.emailSubject = subject
  97. }
  98. }
  99. func WithSMTPClient(c SMTPClient) ConfigOption {
  100. return func(s *Config) {
  101. s.smtpClient = c
  102. }
  103. }