Skip to content

Commit 81e929b

Browse files
committed
pkg/sdk/log: SDK logging package for generated projects
1 parent a6c21fc commit 81e929b

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

pkg/sdk/log/log.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2018 The Operator-SDK 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 log
16+
17+
import (
18+
"os"
19+
20+
"github.com/go-logr/logr"
21+
crlog "sigs.k8s.io/controller-runtime/pkg/runtime/log"
22+
)
23+
24+
// Set the SDK's logger and the parent logger for controller-runtime's logger.
25+
func SetLogger(l logr.Logger) {
26+
logger = l
27+
crlog.SetLogger(l)
28+
}
29+
30+
// Package level logger. Defaults to a null logger that prints nothing.
31+
// SetLogger() must be called to set an active logger.
32+
var logger logr.Logger = crlog.NullLogger{}
33+
34+
func Info(msg string, kvs ...interface{}) {
35+
logger.Info(msg, kvs...)
36+
}
37+
38+
func Enabled() bool {
39+
return logger.Enabled()
40+
}
41+
42+
func Error(err error, msg string, kvs ...interface{}) {
43+
logger.Error(err, msg, kvs...)
44+
}
45+
46+
func V(level int) logr.InfoLogger {
47+
return logger.V(level)
48+
}
49+
50+
func WithValues(kvs ...interface{}) logr.Logger {
51+
return logger.WithValues(kvs...)
52+
}
53+
54+
func WithName(name string) logr.Logger {
55+
return logger.WithName(name)
56+
}
57+
58+
// Fatal calls Error() then exits with a non-zero error code.
59+
// Fatal is not part of the logr interface but is a convenient wrapper.
60+
func Fatal(err error, msg string, kvs ...interface{}) {
61+
logger.Error(err, msg, kvs...)
62+
os.Exit(1)
63+
}

0 commit comments

Comments
 (0)