Skip to content

Commit a8a5067

Browse files
author
Lars Ekman
committed
Added license and updated alternative backends
1 parent a804eb5 commit a8a5067

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

pkg/log/logger.go

+54-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
package log
218

319
import (
@@ -6,7 +22,24 @@ import (
622
"github.com/go-logr/logr"
723
)
824

25+
// Several different logr backends can be tested by remove/add comment
26+
// below.
27+
928
/*
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
1043
import (
1144
"os"
1245
"github.com/go-logr/zerologr"
@@ -30,6 +63,9 @@ func newLogger(level string) logr.Logger {
3063
zl = zl.With().Timestamp().Logger().Level(lvl)
3164
return zerologr.New(&zl)
3265
}
66+
func init() {
67+
defaultLogger = newLogger("").WithName("Meridio")
68+
}
3369
*/
3470

3571
/*
@@ -59,6 +95,9 @@ func newLogger(level string) logr.Logger {
5995
return zapr.NewLogger(z)
6096
6197
}
98+
func init() {
99+
defaultLogger = newLogger("").WithName("Meridio")
100+
}
62101
*/
63102

64103
/*
@@ -82,6 +121,9 @@ func newLogger(level string) logr.Logger {
82121
logrusLog.SetLevel(lvl)
83122
return logrusr.New(logrusLog)
84123
}
124+
func init() {
125+
defaultLogger = newLogger("").WithName("Meridio")
126+
}
85127
*/
86128

87129
import (
@@ -265,8 +307,6 @@ func (e *adpEncoder) OpenNamespace(key string) {
265307
e.jsonEncoder.OpenNamespace(key)
266308
}
267309

268-
var defaultLogger logr.Logger
269-
270310
func init() {
271311
_ = zap.RegisterEncoder(
272312
"ADP", func(cfg zapcore.EncoderConfig) (zapcore.Encoder, error) {
@@ -278,16 +318,6 @@ func init() {
278318
defaultLogger = newLogger("").WithName("Meridio")
279319
}
280320

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-
291321
// Fatal log the message using the passed logger and terminate
292322
func Fatal(logger logr.Logger, msg string, keysAndValues ...interface{}) {
293323
// Get the zap logger
@@ -300,6 +330,18 @@ func Fatal(logger logr.Logger, msg string, keysAndValues ...interface{}) {
300330
}
301331
}
302332

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+
303345
// New returns a new logger. The level may be "DEBUG" (V(1)) or "TRACE" (V(2)),
304346
// any other string (e.g. "") is interpreted as "INFO" (V(0)).
305347
func New(name, level string) logr.Logger {

0 commit comments

Comments
 (0)