1
+ /*
2
+ Copyright (c) 2022 Nordix Foundation
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
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
+
1
17
package log
2
18
3
19
import (
@@ -6,7 +22,24 @@ import (
6
22
"github.com/go-logr/logr"
7
23
)
8
24
25
+ // Several different logr backends can be tested by remove/add comment
26
+ // below.
27
+
9
28
/*
29
+ // This does not work! (doesn't even compile)
30
+ import (
31
+ "k8s.io/klog/klogr"
32
+ )
33
+ func newLogger(level string) logr.Logger {
34
+ return klogr.New()
35
+ }
36
+ func init() {
37
+ defaultLogger = newLogger("").WithName("Meridio")
38
+ }
39
+ */
40
+
41
+ /*
42
+ // Zerologr doesn't implement logger.Enabled() correctly
10
43
import (
11
44
"os"
12
45
"github.com/go-logr/zerologr"
@@ -30,6 +63,9 @@ func newLogger(level string) logr.Logger {
30
63
zl = zl.With().Timestamp().Logger().Level(lvl)
31
64
return zerologr.New(&zl)
32
65
}
66
+ func init() {
67
+ defaultLogger = newLogger("").WithName("Meridio")
68
+ }
33
69
*/
34
70
35
71
/*
@@ -59,6 +95,9 @@ func newLogger(level string) logr.Logger {
59
95
return zapr.NewLogger(z)
60
96
61
97
}
98
+ func init() {
99
+ defaultLogger = newLogger("").WithName("Meridio")
100
+ }
62
101
*/
63
102
64
103
/*
@@ -82,6 +121,9 @@ func newLogger(level string) logr.Logger {
82
121
logrusLog.SetLevel(lvl)
83
122
return logrusr.New(logrusLog)
84
123
}
124
+ func init() {
125
+ defaultLogger = newLogger("").WithName("Meridio")
126
+ }
85
127
*/
86
128
87
129
import (
@@ -265,8 +307,6 @@ func (e *adpEncoder) OpenNamespace(key string) {
265
307
e .jsonEncoder .OpenNamespace (key )
266
308
}
267
309
268
- var defaultLogger logr.Logger
269
-
270
310
func init () {
271
311
_ = zap .RegisterEncoder (
272
312
"ADP" , func (cfg zapcore.EncoderConfig ) (zapcore.Encoder , error ) {
@@ -278,16 +318,6 @@ func init() {
278
318
defaultLogger = newLogger ("" ).WithName ("Meridio" )
279
319
}
280
320
281
- // FromContextOrDefault return a logger from the passed context or a
282
- // default logger. This function is intended as a temprary solution
283
- // during the transition to structured logging.
284
- func FromContextOrDefault (ctx context.Context ) logr.Logger {
285
- if logger , err := logr .FromContext (ctx ); err == nil {
286
- return logger
287
- }
288
- return defaultLogger
289
- }
290
-
291
321
// Fatal log the message using the passed logger and terminate
292
322
func Fatal (logger logr.Logger , msg string , keysAndValues ... interface {}) {
293
323
// Get the zap logger
@@ -300,6 +330,18 @@ func Fatal(logger logr.Logger, msg string, keysAndValues ...interface{}) {
300
330
}
301
331
}
302
332
333
+ var defaultLogger logr.Logger
334
+
335
+ // FromContextOrDefault return a logger from the passed context or a
336
+ // default logger. This function is intended as a temprary solution
337
+ // during the transition to structured logging.
338
+ func FromContextOrDefault (ctx context.Context ) logr.Logger {
339
+ if logger , err := logr .FromContext (ctx ); err == nil {
340
+ return logger
341
+ }
342
+ return defaultLogger
343
+ }
344
+
303
345
// New returns a new logger. The level may be "DEBUG" (V(1)) or "TRACE" (V(2)),
304
346
// any other string (e.g. "") is interpreted as "INFO" (V(0)).
305
347
func New (name , level string ) logr.Logger {
0 commit comments