| 1 |
/* |
| 2 |
Copyright 2021 The logr Authors. |
| 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 |
|
| 17 |
package funcr_test |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"github.com/go-logr/logr" |
| 24 |
"github.com/go-logr/logr/funcr" |
| 25 |
) |
| 26 |
|
| 27 |
// NewStdoutLogger returns a logr.Logger that prints to stdout. |
| 28 |
// It demonstrates how to implement a custom With* function which |
| 29 |
// controls whether INFO or ERROR are printed in front of the log |
| 30 |
// message. |
| 31 |
func NewStdoutLogger() logr.Logger { |
| 32 |
l := &stdoutlogger{ |
| 33 |
Formatter: funcr.NewFormatter(funcr.Options{}), |
| 34 |
} |
| 35 |
return logr.New(l) |
| 36 |
} |
| 37 |
|
| 38 |
type stdoutlogger struct { |
| 39 |
funcr.Formatter |
| 40 |
logMsgType bool |
| 41 |
} |
| 42 |
|
| 43 |
func (l stdoutlogger) WithName(name string) logr.LogSink { |
| 44 |
l.AddName(name) |
| 45 |
return &l |
| 46 |
} |
| 47 |
|
| 48 |
func (l stdoutlogger) WithValues(kvList ...any) logr.LogSink { |
| 49 |
l.AddValues(kvList) |
| 50 |
return &l |
| 51 |
} |
| 52 |
|
| 53 |
func (l stdoutlogger) WithCallDepth(depth int) logr.LogSink { |
| 54 |
l.AddCallDepth(depth) |
| 55 |
return &l |
| 56 |
} |
| 57 |
|
| 58 |
func (l stdoutlogger) Info(level int, msg string, kvList ...any) { |
| 59 |
prefix, args := l.FormatInfo(level, msg, kvList) |
| 60 |
l.write("INFO", prefix, args) |
| 61 |
} |
| 62 |
|
| 63 |
func (l stdoutlogger) Error(err error, msg string, kvList ...any) { |
| 64 |
prefix, args := l.FormatError(err, msg, kvList) |
| 65 |
l.write("ERROR", prefix, args) |
| 66 |
} |
| 67 |
|
| 68 |
func (l stdoutlogger) write(msgType, prefix, args string) { |
| 69 |
var parts []string |
| 70 |
if l.logMsgType { |
| 71 |
parts = append(parts, msgType) |
| 72 |
} |
| 73 |
if prefix != "" { |
| 74 |
parts = append(parts, prefix) |
| 75 |
} |
| 76 |
parts = append(parts, args) |
| 77 |
fmt.Println(strings.Join(parts, ": ")) |
| 78 |
} |
| 79 |
|
| 80 |
// WithLogMsgType returns a copy of the logger with new settings for |
| 81 |
// logging the message type. It returns the original logger if the |
| 82 |
// underlying LogSink is not a stdoutlogger. |
| 83 |
func WithLogMsgType(log logr.Logger, logMsgType bool) logr.Logger { |
| 84 |
if l, ok := log.GetSink().(*stdoutlogger); ok { |
| 85 |
clone := *l |
| 86 |
clone.logMsgType = logMsgType |
| 87 |
log = log.WithSink(&clone) |
| 88 |
} |
| 89 |
return log |
| 90 |
} |
| 91 |
|
| 92 |
// Assert conformance to the interfaces. |
| 93 |
var _ logr.LogSink = &stdoutlogger{} |
| 94 |
var _ logr.CallDepthLogSink = &stdoutlogger{} |
| 95 |
|
| 96 |
func ExampleFormatter() { |
| 97 |
l := NewStdoutLogger() |
| 98 |
l.Info("no message type") |
| 99 |
WithLogMsgType(l, true).Info("with message type") |
| 100 |
// Output: |
| 101 |
// "level"=0 "msg"="no message type" |
| 102 |
// INFO: "level"=0 "msg"="with message type" |
| 103 |
} |