|
| 1 | +// Copyright 2024 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package promslog |
| 15 | + |
| 16 | +import ( |
| 17 | + "bytes" |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "log/slog" |
| 21 | + "regexp" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + "gopkg.in/yaml.v2" |
| 27 | +) |
| 28 | + |
| 29 | +var logLevelRegexp = regexp.MustCompile(`.*level=(?P<Level>WARN|INFO|ERROR|DEBUG).*`) |
| 30 | + |
| 31 | +// Make sure creating and using a logger with an empty configuration doesn't |
| 32 | +// result in a panic. |
| 33 | +func TestDefaultConfig(t *testing.T) { |
| 34 | + require.NotPanics(t, func() { |
| 35 | + logger := New(&Config{}) |
| 36 | + logger.Info("empty config `Info()` test", "hello", "world") |
| 37 | + logger.Log(context.Background(), slog.LevelInfo, "empty config `Log()` test", "hello", "world") |
| 38 | + logger.LogAttrs(context.Background(), slog.LevelInfo, "empty config `LogAttrs()` test", slog.String("hello", "world")) |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +func TestUnmarshallLevel(t *testing.T) { |
| 43 | + l := &AllowedLevel{} |
| 44 | + err := yaml.Unmarshal([]byte(`debug`), l) |
| 45 | + if err != nil { |
| 46 | + t.Error(err) |
| 47 | + } |
| 48 | + if l.s != "debug" { |
| 49 | + t.Errorf("expected %s, got %s", "debug", l.s) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestUnmarshallEmptyLevel(t *testing.T) { |
| 54 | + l := &AllowedLevel{} |
| 55 | + err := yaml.Unmarshal([]byte(``), l) |
| 56 | + if err != nil { |
| 57 | + t.Error(err) |
| 58 | + } |
| 59 | + if l.s != "" { |
| 60 | + t.Errorf("expected empty level, got %s", l.s) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestUnmarshallBadLevel(t *testing.T) { |
| 65 | + l := &AllowedLevel{} |
| 66 | + err := yaml.Unmarshal([]byte(`debugg`), l) |
| 67 | + if err == nil { |
| 68 | + t.Error("expected error") |
| 69 | + } |
| 70 | + expErr := `unrecognized log level "debugg"` |
| 71 | + if err.Error() != expErr { |
| 72 | + t.Errorf("expected error %s, got %s", expErr, err.Error()) |
| 73 | + } |
| 74 | + if l.s != "" { |
| 75 | + t.Errorf("expected empty level, got %s", l.s) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func getLogEntryLevelCounts(s string) map[string]int { |
| 80 | + counters := make(map[string]int) |
| 81 | + lines := strings.Split(s, "\n") |
| 82 | + for _, line := range lines { |
| 83 | + matches := logLevelRegexp.FindStringSubmatch(line) |
| 84 | + if len(matches) > 1 { |
| 85 | + levelIndex := logLevelRegexp.SubexpIndex("Level") |
| 86 | + |
| 87 | + counters[matches[levelIndex]]++ |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return counters |
| 92 | +} |
| 93 | + |
| 94 | +func TestDynamicLevels(t *testing.T) { |
| 95 | + var buf bytes.Buffer |
| 96 | + config := &Config{ioWriter: &buf} |
| 97 | + logger := New(config) |
| 98 | + |
| 99 | + // Test that log level can be adjusted on-the-fly to debug and that a |
| 100 | + // log entry can be written to the file. |
| 101 | + if err := config.Level.Set("debug"); err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + logger.Info("info", "hello", "world") |
| 105 | + logger.Debug("debug", "hello", "world") |
| 106 | + |
| 107 | + counts := getLogEntryLevelCounts(buf.String()) |
| 108 | + require.Equal(t, 1, counts["INFO"], "info log successfully detected") |
| 109 | + require.Equal(t, 1, counts["DEBUG"], "debug log successfully detected") |
| 110 | + // Print logs for humans to see, if needed. |
| 111 | + fmt.Println(buf.String()) |
| 112 | + buf.Reset() |
| 113 | + |
| 114 | + // Test that log level can be adjusted on-the-fly to info and that a |
| 115 | + // subsequent call to write a debug level log is _not_ written to the |
| 116 | + // file. |
| 117 | + if err := config.Level.Set("info"); err != nil { |
| 118 | + t.Fatal(err) |
| 119 | + } |
| 120 | + logger.Info("info", "hello", "world") |
| 121 | + logger.Debug("debug", "hello", "world") |
| 122 | + |
| 123 | + counts = getLogEntryLevelCounts(buf.String()) |
| 124 | + require.Equal(t, 1, counts["INFO"], "info log successfully detected") |
| 125 | + require.NotEqual(t, 1, counts["DEBUG"], "extra debug log detected") |
| 126 | + // Print logs for humans to see, if needed. |
| 127 | + fmt.Println(buf.String()) |
| 128 | + buf.Reset() |
| 129 | +} |
0 commit comments