3
* Copyright 2020 gRPC authors.
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
25
// PrefixLogger does logging with a prefix.
27
// Logging method on a nil logs without any prefix.
28
type PrefixLogger struct {
33
// Infof does info logging.
34
func (pl *PrefixLogger) Infof(format string, args ...interface{}) {
36
// Handle nil, so the tests can pass in a nil logger.
37
format = pl.prefix + format
38
pl.logger.InfoDepth(1, fmt.Sprintf(format, args...))
41
InfoDepth(1, fmt.Sprintf(format, args...))
44
// Warningf does warning logging.
45
func (pl *PrefixLogger) Warningf(format string, args ...interface{}) {
47
format = pl.prefix + format
48
pl.logger.WarningDepth(1, fmt.Sprintf(format, args...))
51
WarningDepth(1, fmt.Sprintf(format, args...))
54
// Errorf does error logging.
55
func (pl *PrefixLogger) Errorf(format string, args ...interface{}) {
57
format = pl.prefix + format
58
pl.logger.ErrorDepth(1, fmt.Sprintf(format, args...))
61
ErrorDepth(1, fmt.Sprintf(format, args...))
64
// Debugf does info logging at verbose level 2.
65
func (pl *PrefixLogger) Debugf(format string, args ...interface{}) {
70
// Handle nil, so the tests can pass in a nil logger.
71
format = pl.prefix + format
72
pl.logger.InfoDepth(1, fmt.Sprintf(format, args...))
75
InfoDepth(1, fmt.Sprintf(format, args...))
78
// NewPrefixLogger creates a prefix logger with the given prefix.
79
func NewPrefixLogger(logger DepthLoggerV2, prefix string) *PrefixLogger {
80
return &PrefixLogger{logger: logger, prefix: prefix}