Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 23:30 UTC
log: default logger should respect `{in,ex}clude` (#6995)
* log: default logger should respect `{in,ex}clude` Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com> * add tests Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com> --------- Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
Mohammed Al Sahaf committed
May 7, 2025 at 01:06 UTC
9f7148392adb72a6121bf99070efaa1a90ffe901
2 files changed
+109
-1
logging.go
+3
-1
@@ -162,7 +162,9 @@ func (logging *Logging) setupNewDefault(ctx Context) error {
162
if err != nil {
163
return fmt.Errorf("setting up default log: %v", err)
164
}
165
- newDefault.logger = zap.New(newDefault.CustomLog.core, options...)
165
+
166
+ filteringCore := &filteringCore{newDefault.CustomLog.core, newDefault.CustomLog}
167
+ newDefault.logger = zap.New(filteringCore, options...)
168
169
// redirect the default caddy logs
170
defaultLoggerMu.Lock()
logging_test.go
new
+106
@@ -0,0 +1,106 @@
1
+// Copyright 2015 Matthew Holt and The Caddy Authors
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License");
4
+// you may not use this file except in compliance with the License.
5
+// You may obtain a copy of the License at
6
+//
7
+// http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS,
11
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+// See the License for the specific language governing permissions and
13
+// limitations under the License.
14
+
15
+package caddy
16
+
17
+import "testing"
18
+
19
+func TestCustomLog_loggerAllowed(t *testing.T) {
20
+ type fields struct {
21
+ BaseLog BaseLog
22
+ Include []string
23
+ Exclude []string
24
+ }
25
+ type args struct {
26
+ name string
27
+ isModule bool
28
+ }
29
+ tests := []struct {
30
+ name string
31
+ fields fields
32
+ args args
33
+ want bool
34
+ }{
35
+ {
36
+ name: "include",
37
+ fields: fields{
38
+ Include: []string{"foo"},
39
+ },
40
+ args: args{
41
+ name: "foo",
42
+ isModule: true,
43
+ },
44
+ want: true,
45
+ },
46
+ {
47
+ name: "exclude",
48
+ fields: fields{
49
+ Exclude: []string{"foo"},
50
+ },
51
+ args: args{
52
+ name: "foo",
53
+ isModule: true,
54
+ },
55
+ want: false,
56
+ },
57
+ {
58
+ name: "include and exclude",
59
+ fields: fields{
60
+ Include: []string{"foo"},
61
+ Exclude: []string{"foo"},
62
+ },
63
+ args: args{
64
+ name: "foo",
65
+ isModule: true,
66
+ },
67
+ want: false,
68
+ },
69
+ {
70
+ name: "include and exclude (longer namespace)",
71
+ fields: fields{
72
+ Include: []string{"foo.bar"},
73
+ Exclude: []string{"foo"},
74
+ },
75
+ args: args{
76
+ name: "foo.bar",
77
+ isModule: true,
78
+ },
79
+ want: true,
80
+ },
81
+ {
82
+ name: "excluded module is not printed",
83
+ fields: fields{
84
+ Include: []string{"admin.api.load"},
85
+ Exclude: []string{"admin.api"},
86
+ },
87
+ args: args{
88
+ name: "admin.api",
89
+ isModule: false,
90
+ },
91
+ want: false,
92
+ },
93
+ }
94
+ for _, tt := range tests {
95
+ t.Run(tt.name, func(t *testing.T) {
96
+ cl := &CustomLog{
97
+ BaseLog: tt.fields.BaseLog,
98
+ Include: tt.fields.Include,
99
+ Exclude: tt.fields.Exclude,
100
+ }
101
+ if got := cl.loggerAllowed(tt.args.name, tt.args.isModule); got != tt.want {
102
+ t.Errorf("CustomLog.loggerAllowed() = %v, want %v", got, tt.want)
103
+ }
104
+ })
105
+ }
106
+}