blob: 82af70e96f157f1043fcfa1dd238c6f34b312374 [file] [log] [blame]
kesavand2cde6582020-06-22 04:56:23 -04001/*
2 *
3 * Copyright 2020 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19package grpclog
20
Andrea Campanella764f1ed2022-03-24 11:46:38 +010021import (
22 "fmt"
23)
24
kesavand2cde6582020-06-22 04:56:23 -040025// PrefixLogger does logging with a prefix.
26//
27// Logging method on a nil logs without any prefix.
28type PrefixLogger struct {
Andrea Campanella764f1ed2022-03-24 11:46:38 +010029 logger DepthLoggerV2
kesavand2cde6582020-06-22 04:56:23 -040030 prefix string
31}
32
33// Infof does info logging.
34func (pl *PrefixLogger) Infof(format string, args ...interface{}) {
35 if pl != nil {
36 // Handle nil, so the tests can pass in a nil logger.
37 format = pl.prefix + format
Andrea Campanella764f1ed2022-03-24 11:46:38 +010038 pl.logger.InfoDepth(1, fmt.Sprintf(format, args...))
39 return
kesavand2cde6582020-06-22 04:56:23 -040040 }
Andrea Campanella764f1ed2022-03-24 11:46:38 +010041 InfoDepth(1, fmt.Sprintf(format, args...))
kesavand2cde6582020-06-22 04:56:23 -040042}
43
44// Warningf does warning logging.
45func (pl *PrefixLogger) Warningf(format string, args ...interface{}) {
46 if pl != nil {
47 format = pl.prefix + format
Andrea Campanella764f1ed2022-03-24 11:46:38 +010048 pl.logger.WarningDepth(1, fmt.Sprintf(format, args...))
49 return
kesavand2cde6582020-06-22 04:56:23 -040050 }
Andrea Campanella764f1ed2022-03-24 11:46:38 +010051 WarningDepth(1, fmt.Sprintf(format, args...))
kesavand2cde6582020-06-22 04:56:23 -040052}
53
54// Errorf does error logging.
55func (pl *PrefixLogger) Errorf(format string, args ...interface{}) {
56 if pl != nil {
57 format = pl.prefix + format
Andrea Campanella764f1ed2022-03-24 11:46:38 +010058 pl.logger.ErrorDepth(1, fmt.Sprintf(format, args...))
59 return
kesavand2cde6582020-06-22 04:56:23 -040060 }
Andrea Campanella764f1ed2022-03-24 11:46:38 +010061 ErrorDepth(1, fmt.Sprintf(format, args...))
kesavand2cde6582020-06-22 04:56:23 -040062}
63
64// Debugf does info logging at verbose level 2.
65func (pl *PrefixLogger) Debugf(format string, args ...interface{}) {
Andrea Campanella764f1ed2022-03-24 11:46:38 +010066 if !Logger.V(2) {
67 return
kesavand2cde6582020-06-22 04:56:23 -040068 }
Andrea Campanella764f1ed2022-03-24 11:46:38 +010069 if pl != nil {
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...))
73 return
74 }
75 InfoDepth(1, fmt.Sprintf(format, args...))
kesavand2cde6582020-06-22 04:56:23 -040076}
77
78// NewPrefixLogger creates a prefix logger with the given prefix.
Andrea Campanella764f1ed2022-03-24 11:46:38 +010079func NewPrefixLogger(logger DepthLoggerV2, prefix string) *PrefixLogger {
80 return &PrefixLogger{logger: logger, prefix: prefix}
kesavand2cde6582020-06-22 04:56:23 -040081}