1
0

logger_config.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package logger
  2. import "log/slog"
  3. type Config 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 uint16
  19. smtpUsername string
  20. smtpPassword string
  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 WithSMTPHost(host string) ConfigOption {
  100. return func(s *Config) {
  101. s.smtpHost = host
  102. }
  103. }
  104. func WithSMTPPort(port uint16) ConfigOption {
  105. return func(s *Config) {
  106. s.smtpPort = port
  107. }
  108. }
  109. func WithSMTPUsername(user string) ConfigOption {
  110. return func(s *Config) {
  111. s.smtpUsername = user
  112. }
  113. }
  114. func WithSMTPPassword(password string) ConfigOption {
  115. return func(s *Config) {
  116. s.smtpPassword = password
  117. }
  118. }