| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// Some functions in this file (see comments) is based on the Go source code, |
| 3 |
// copyright The Go Authors and governed by a BSD-style license. |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
// you may not use this file except in compliance with the License. |
| 7 |
// You may obtain a copy of the License at |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
|
| 16 |
package loggers_test |
| 17 |
|
| 18 |
import ( |
| 19 |
"io" |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/bep/logg" |
| 24 |
qt "github.com/frankban/quicktest" |
| 25 |
"github.com/gohugoio/hugo/common/loggers" |
| 26 |
) |
| 27 |
|
| 28 |
func TestLogDistinct(t *testing.T) { |
| 29 |
c := qt.New(t) |
| 30 |
|
| 31 |
opts := loggers.Options{ |
| 32 |
DistinctLevel: logg.LevelWarn, |
| 33 |
StoreErrors: true, |
| 34 |
StdOut: io.Discard, |
| 35 |
StdErr: io.Discard, |
| 36 |
} |
| 37 |
|
| 38 |
l := loggers.New(opts) |
| 39 |
|
| 40 |
for range 10 { |
| 41 |
l.Errorln("error 1") |
| 42 |
l.Errorln("error 2") |
| 43 |
l.Warnln("warn 1") |
| 44 |
} |
| 45 |
c.Assert(strings.Count(l.Errors(), "error 1"), qt.Equals, 1) |
| 46 |
c.Assert(l.LoggCount(logg.LevelError), qt.Equals, 2) |
| 47 |
c.Assert(l.LoggCount(logg.LevelWarn), qt.Equals, 1) |
| 48 |
} |
| 49 |
|
| 50 |
func TestHookLast(t *testing.T) { |
| 51 |
c := qt.New(t) |
| 52 |
|
| 53 |
opts := loggers.Options{ |
| 54 |
HandlerPost: func(e *logg.Entry) error { |
| 55 |
panic(e.Message) |
| 56 |
}, |
| 57 |
StdOut: io.Discard, |
| 58 |
StdErr: io.Discard, |
| 59 |
} |
| 60 |
|
| 61 |
l := loggers.New(opts) |
| 62 |
|
| 63 |
c.Assert(func() { l.Warnln("warn 1") }, qt.PanicMatches, "warn 1") |
| 64 |
} |
| 65 |
|
| 66 |
func TestOptionStoreErrors(t *testing.T) { |
| 67 |
c := qt.New(t) |
| 68 |
|
| 69 |
var sb strings.Builder |
| 70 |
|
| 71 |
opts := loggers.Options{ |
| 72 |
StoreErrors: true, |
| 73 |
StdErr: &sb, |
| 74 |
StdOut: &sb, |
| 75 |
} |
| 76 |
|
| 77 |
l := loggers.New(opts) |
| 78 |
l.Errorln("error 1") |
| 79 |
l.Errorln("error 2") |
| 80 |
|
| 81 |
errorsStr := l.Errors() |
| 82 |
|
| 83 |
c.Assert(errorsStr, qt.Contains, "error 1") |
| 84 |
c.Assert(errorsStr, qt.Not(qt.Contains), "ERROR") |
| 85 |
|
| 86 |
c.Assert(sb.String(), qt.Contains, "error 1") |
| 87 |
c.Assert(sb.String(), qt.Contains, "ERROR") |
| 88 |
} |
| 89 |
|
| 90 |
func TestLogCount(t *testing.T) { |
| 91 |
c := qt.New(t) |
| 92 |
|
| 93 |
opts := loggers.Options{ |
| 94 |
StoreErrors: true, |
| 95 |
} |
| 96 |
|
| 97 |
l := loggers.New(opts) |
| 98 |
l.Errorln("error 1") |
| 99 |
l.Errorln("error 2") |
| 100 |
l.Warnln("warn 1") |
| 101 |
|
| 102 |
c.Assert(l.LoggCount(logg.LevelError), qt.Equals, 2) |
| 103 |
c.Assert(l.LoggCount(logg.LevelWarn), qt.Equals, 1) |
| 104 |
c.Assert(l.LoggCount(logg.LevelInfo), qt.Equals, 0) |
| 105 |
} |
| 106 |
|
| 107 |
func TestSuppressStatements(t *testing.T) { |
| 108 |
c := qt.New(t) |
| 109 |
|
| 110 |
opts := loggers.Options{ |
| 111 |
StoreErrors: true, |
| 112 |
SuppressStatements: map[string]bool{ |
| 113 |
"error-1": true, |
| 114 |
}, |
| 115 |
} |
| 116 |
|
| 117 |
l := loggers.New(opts) |
| 118 |
l.Error().WithField(loggers.FieldNameStatementID, "error-1").Logf("error 1") |
| 119 |
l.Errorln("error 2") |
| 120 |
|
| 121 |
errorsStr := l.Errors() |
| 122 |
|
| 123 |
c.Assert(errorsStr, qt.Not(qt.Contains), "error 1") |
| 124 |
c.Assert(errorsStr, qt.Contains, "error 2") |
| 125 |
c.Assert(l.LoggCount(logg.LevelError), qt.Equals, 1) |
| 126 |
} |
| 127 |
|
| 128 |
func TestReset(t *testing.T) { |
| 129 |
c := qt.New(t) |
| 130 |
|
| 131 |
opts := loggers.Options{ |
| 132 |
StoreErrors: true, |
| 133 |
DistinctLevel: logg.LevelWarn, |
| 134 |
StdOut: io.Discard, |
| 135 |
StdErr: io.Discard, |
| 136 |
} |
| 137 |
|
| 138 |
l := loggers.New(opts) |
| 139 |
|
| 140 |
for range 3 { |
| 141 |
l.Errorln("error 1") |
| 142 |
l.Errorln("error 2") |
| 143 |
l.Errorln("error 1") |
| 144 |
c.Assert(l.LoggCount(logg.LevelError), qt.Equals, 2) |
| 145 |
|
| 146 |
l.Reset() |
| 147 |
|
| 148 |
errorsStr := l.Errors() |
| 149 |
|
| 150 |
c.Assert(errorsStr, qt.Equals, "") |
| 151 |
c.Assert(l.LoggCount(logg.LevelError), qt.Equals, 0) |
| 152 |
|
| 153 |
} |
| 154 |
} |