logger_config.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. fileLogFilepath string
  13. // email config
  14. emailLogEnabled bool
  15. emailLogLevel slog.Level // INFO by default
  16. emailLogAddSource bool
  17. }
  18. type ConfigOption func(*Config)
  19. type ConfigOptions []ConfigOption
  20. func (s *ConfigOptions) Add(option ConfigOption) {
  21. *s = append(*s, option)
  22. }
  23. func NewConfig(opts ...ConfigOption) Config {
  24. c := &Config{}
  25. for _, opt := range opts {
  26. opt(c)
  27. }
  28. return *c
  29. }
  30. // stdout log
  31. func WithStdoutLogEnabled(enabled bool) ConfigOption {
  32. return func(s *Config) {
  33. s.stdoutLogEnabled = enabled
  34. }
  35. }
  36. func WithStdoutLogLevel(level slog.Level) ConfigOption {
  37. return func(s *Config) {
  38. s.stdoutLogLevel = level
  39. }
  40. }
  41. func WithStdoutLogAddSource(add bool) ConfigOption {
  42. return func(s *Config) {
  43. s.stdoutLogAddSource = add
  44. }
  45. }
  46. // file log
  47. func WithFileLogEnabled(enabled bool) ConfigOption {
  48. return func(s *Config) {
  49. s.fileLogEnabled = enabled
  50. }
  51. }
  52. func WithFileLogLevel(level slog.Level) ConfigOption {
  53. return func(s *Config) {
  54. s.fileLogLevel = level
  55. }
  56. }
  57. func WithFileLogAddSource(add bool) ConfigOption {
  58. return func(s *Config) {
  59. s.fileLogAddSource = add
  60. }
  61. }
  62. func WithFileLogFilepath(path string) ConfigOption {
  63. return func(s *Config) {
  64. s.fileLogFilepath = path
  65. }
  66. }
  67. // email log
  68. func WithEmailLogEnabled(enabled bool) ConfigOption {
  69. return func(s *Config) {
  70. s.emailLogEnabled = enabled
  71. }
  72. }
  73. func WithEmailLogLevel(level slog.Level) ConfigOption {
  74. return func(s *Config) {
  75. s.emailLogLevel = level
  76. }
  77. }
  78. func WithEmailLogAddSource(add bool) ConfigOption {
  79. return func(s *Config) {
  80. s.emailLogAddSource = add
  81. }
  82. }