|
| 1 | +package conf |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/spf13/viper" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "github.com/tphakala/birdnet-go/internal/logger" |
| 12 | +) |
| 13 | + |
| 14 | +// TestViperUnmarshalLoggingConfig verifies that Viper correctly unmarshals |
| 15 | +// logging configuration including module outputs with underscore-separated |
| 16 | +// keys. This is a regression test for missing mapstructure tags that caused |
| 17 | +// module-level debug settings to be silently ignored. |
| 18 | +func TestViperUnmarshalLoggingConfig(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + configContent := ` |
| 22 | +logging: |
| 23 | + default_level: debug |
| 24 | + debug_webhooks: true |
| 25 | + timezone: UTC |
| 26 | + console: |
| 27 | + enabled: true |
| 28 | + level: warn |
| 29 | + file_output: |
| 30 | + enabled: true |
| 31 | + path: logs/app.log |
| 32 | + level: debug |
| 33 | + max_size: 50 |
| 34 | + max_age: 7 |
| 35 | + max_rotated_files: 5 |
| 36 | + compress: true |
| 37 | + modules: |
| 38 | + imageprovider: |
| 39 | + enabled: true |
| 40 | + file_path: logs/imageprovider.log |
| 41 | + level: debug |
| 42 | + console_also: true |
| 43 | + max_size: 25 |
| 44 | + max_age: 14 |
| 45 | + max_rotated_files: 3 |
| 46 | + audio: |
| 47 | + enabled: true |
| 48 | + file_path: logs/audio.log |
| 49 | + level: trace |
| 50 | + module_levels: |
| 51 | + analysis: warn |
| 52 | + ebird: error |
| 53 | +` |
| 54 | + |
| 55 | + v := viper.New() |
| 56 | + v.SetConfigType("yaml") |
| 57 | + |
| 58 | + err := v.ReadConfig(strings.NewReader(configContent)) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + var settings Settings |
| 62 | + err = v.Unmarshal(&settings, viper.DecodeHook(DurationDecodeHook())) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + cfg := settings.Logging |
| 66 | + |
| 67 | + // Verify top-level fields |
| 68 | + assert.Equal(t, "debug", cfg.DefaultLevel, "DefaultLevel should be unmarshaled from default_level") |
| 69 | + assert.True(t, cfg.DebugWebhooks, "DebugWebhooks should be unmarshaled from debug_webhooks") |
| 70 | + assert.Equal(t, "UTC", cfg.Timezone) |
| 71 | + |
| 72 | + // Verify console config |
| 73 | + require.NotNil(t, cfg.Console, "Console should not be nil") |
| 74 | + assert.True(t, cfg.Console.Enabled) |
| 75 | + assert.Equal(t, "warn", cfg.Console.Level) |
| 76 | + |
| 77 | + // Verify file output config (underscore keys) |
| 78 | + require.NotNil(t, cfg.FileOutput, "FileOutput should not be nil (mapped from file_output)") |
| 79 | + assert.True(t, cfg.FileOutput.Enabled) |
| 80 | + assert.Equal(t, "logs/app.log", cfg.FileOutput.Path) |
| 81 | + assert.Equal(t, "debug", cfg.FileOutput.Level) |
| 82 | + assert.Equal(t, 50, cfg.FileOutput.MaxSize, "MaxSize should be unmarshaled from max_size") |
| 83 | + assert.Equal(t, 7, cfg.FileOutput.MaxAge, "MaxAge should be unmarshaled from max_age") |
| 84 | + assert.Equal(t, 5, cfg.FileOutput.MaxRotatedFiles, "MaxRotatedFiles should be unmarshaled from max_rotated_files") |
| 85 | + assert.True(t, cfg.FileOutput.Compress) |
| 86 | + |
| 87 | + // Verify module outputs (the critical fix — "modules" maps to ModuleOutputs) |
| 88 | + require.NotNil(t, cfg.ModuleOutputs, "ModuleOutputs should not be nil (mapped from modules)") |
| 89 | + require.Contains(t, cfg.ModuleOutputs, "imageprovider") |
| 90 | + require.Contains(t, cfg.ModuleOutputs, "audio") |
| 91 | + |
| 92 | + imgMod := cfg.ModuleOutputs["imageprovider"] |
| 93 | + assert.True(t, imgMod.Enabled) |
| 94 | + assert.Equal(t, "logs/imageprovider.log", imgMod.FilePath, "FilePath should be unmarshaled from file_path") |
| 95 | + assert.Equal(t, "debug", imgMod.Level, "Module level should be 'debug' — this was the root cause of missing debug logs") |
| 96 | + assert.True(t, imgMod.ConsoleAlso, "ConsoleAlso should be unmarshaled from console_also") |
| 97 | + assert.Equal(t, 25, imgMod.MaxSize) |
| 98 | + assert.Equal(t, 14, imgMod.MaxAge) |
| 99 | + assert.Equal(t, 3, imgMod.MaxRotatedFiles) |
| 100 | + |
| 101 | + audioMod := cfg.ModuleOutputs["audio"] |
| 102 | + assert.True(t, audioMod.Enabled) |
| 103 | + assert.Equal(t, "logs/audio.log", audioMod.FilePath) |
| 104 | + assert.Equal(t, "trace", audioMod.Level) |
| 105 | + |
| 106 | + // Verify module levels (separate from module outputs) |
| 107 | + require.NotNil(t, cfg.ModuleLevels, "ModuleLevels should not be nil (mapped from module_levels)") |
| 108 | + assert.Equal(t, "warn", cfg.ModuleLevels["analysis"]) |
| 109 | + assert.Equal(t, "error", cfg.ModuleLevels["ebird"]) |
| 110 | +} |
| 111 | + |
| 112 | +// TestViperUnmarshalLoggingDefaults verifies that Viper SetDefault values |
| 113 | +// for logging modules are correctly unmarshaled with mapstructure tags. |
| 114 | +func TestViperUnmarshalLoggingDefaults(t *testing.T) { |
| 115 | + t.Parallel() |
| 116 | + |
| 117 | + v := viper.New() |
| 118 | + |
| 119 | + // Set defaults the same way setDefaultConfig does |
| 120 | + v.SetDefault("logging.default_level", "info") |
| 121 | + v.SetDefault("logging.console.enabled", true) |
| 122 | + v.SetDefault("logging.console.level", "info") |
| 123 | + v.SetDefault("logging.file_output.enabled", true) |
| 124 | + v.SetDefault("logging.file_output.path", "logs/application.log") |
| 125 | + v.SetDefault("logging.file_output.level", "info") |
| 126 | + v.SetDefault("logging.file_output.max_size", 100) |
| 127 | + v.SetDefault("logging.modules.imageprovider.enabled", true) |
| 128 | + v.SetDefault("logging.modules.imageprovider.file_path", "logs/imageprovider.log") |
| 129 | + v.SetDefault("logging.modules.imageprovider.level", "debug") |
| 130 | + v.SetDefault("logging.module_levels.analysis", "warn") |
| 131 | + |
| 132 | + var settings Settings |
| 133 | + err := v.Unmarshal(&settings, viper.DecodeHook(DurationDecodeHook())) |
| 134 | + require.NoError(t, err) |
| 135 | + |
| 136 | + cfg := settings.Logging |
| 137 | + |
| 138 | + assert.Equal(t, "info", cfg.DefaultLevel) |
| 139 | + |
| 140 | + require.NotNil(t, cfg.FileOutput) |
| 141 | + assert.Equal(t, "logs/application.log", cfg.FileOutput.Path) |
| 142 | + assert.Equal(t, 100, cfg.FileOutput.MaxSize) |
| 143 | + |
| 144 | + require.NotNil(t, cfg.ModuleOutputs, "ModuleOutputs must be populated from logging.modules.* defaults") |
| 145 | + require.Contains(t, cfg.ModuleOutputs, "imageprovider") |
| 146 | + assert.Equal(t, "debug", cfg.ModuleOutputs["imageprovider"].Level, |
| 147 | + "Module level from defaults should be 'debug'") |
| 148 | + assert.Equal(t, "logs/imageprovider.log", cfg.ModuleOutputs["imageprovider"].FilePath) |
| 149 | + |
| 150 | + require.NotNil(t, cfg.ModuleLevels, "ModuleLevels must be populated from logging.module_levels.* defaults") |
| 151 | + assert.Equal(t, "warn", cfg.ModuleLevels["analysis"]) |
| 152 | +} |
| 153 | + |
| 154 | +// TestLoggingModuleLevelAppliedToLogger verifies the end-to-end flow: |
| 155 | +// module level from config reaches the CentralLogger and produces a |
| 156 | +// logger that actually enables debug messages. |
| 157 | +func TestLoggingModuleLevelAppliedToLogger(t *testing.T) { |
| 158 | + t.Parallel() |
| 159 | + |
| 160 | + logPath := t.TempDir() + "/test.log" |
| 161 | + cfg := &logger.LoggingConfig{ |
| 162 | + DefaultLevel: "info", |
| 163 | + Timezone: "UTC", |
| 164 | + Console: &logger.ConsoleOutput{ |
| 165 | + Enabled: false, // disable console to avoid noise |
| 166 | + }, |
| 167 | + ModuleOutputs: map[string]logger.ModuleOutput{ |
| 168 | + "testmod": { |
| 169 | + Enabled: true, |
| 170 | + FilePath: logPath, |
| 171 | + Level: "debug", |
| 172 | + }, |
| 173 | + }, |
| 174 | + } |
| 175 | + |
| 176 | + cl, err := logger.NewCentralLogger(cfg) |
| 177 | + require.NoError(t, err) |
| 178 | + defer func() { require.NoError(t, cl.Close()) }() |
| 179 | + |
| 180 | + log := cl.Module("testmod") |
| 181 | + require.NotNil(t, log) |
| 182 | + |
| 183 | + // Debug should not be filtered — the module is set to debug level |
| 184 | + log.Debug("test debug message", logger.String("key", "value")) |
| 185 | + log.Info("test info message") |
| 186 | + |
| 187 | + // Flush and read the log file to verify debug entries appear |
| 188 | + require.NoError(t, cl.Flush()) |
| 189 | + |
| 190 | + content, err := os.ReadFile(logPath) |
| 191 | + require.NoError(t, err) |
| 192 | + |
| 193 | + assert.Contains(t, string(content), "DEBUG", "Log file should contain DEBUG entries when module level is debug") |
| 194 | + assert.Contains(t, string(content), "test debug message") |
| 195 | + assert.Contains(t, string(content), "INFO") |
| 196 | + assert.Contains(t, string(content), "test info message") |
| 197 | +} |
| 198 | + |
| 199 | +// TestLoggingModuleLevelInfoFiltersDebug verifies that info-level modules |
| 200 | +// correctly filter out debug messages. |
| 201 | +func TestLoggingModuleLevelInfoFiltersDebug(t *testing.T) { |
| 202 | + t.Parallel() |
| 203 | + |
| 204 | + logPath := t.TempDir() + "/test.log" |
| 205 | + cfg := &logger.LoggingConfig{ |
| 206 | + DefaultLevel: "info", |
| 207 | + Timezone: "UTC", |
| 208 | + Console: &logger.ConsoleOutput{ |
| 209 | + Enabled: false, |
| 210 | + }, |
| 211 | + ModuleOutputs: map[string]logger.ModuleOutput{ |
| 212 | + "testmod": { |
| 213 | + Enabled: true, |
| 214 | + FilePath: logPath, |
| 215 | + Level: "info", |
| 216 | + }, |
| 217 | + }, |
| 218 | + } |
| 219 | + |
| 220 | + cl, err := logger.NewCentralLogger(cfg) |
| 221 | + require.NoError(t, err) |
| 222 | + defer func() { require.NoError(t, cl.Close()) }() |
| 223 | + |
| 224 | + log := cl.Module("testmod") |
| 225 | + log.Debug("should not appear") |
| 226 | + log.Info("should appear") |
| 227 | + |
| 228 | + require.NoError(t, cl.Flush()) |
| 229 | + |
| 230 | + content, err := os.ReadFile(logPath) |
| 231 | + require.NoError(t, err) |
| 232 | + |
| 233 | + assert.NotContains(t, string(content), "should not appear", "Debug messages should be filtered at info level") |
| 234 | + assert.Contains(t, string(content), "should appear") |
| 235 | +} |
0 commit comments