component.go (3168B)
1 /* 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 19 package grpclog 20 21 import ( 22 "fmt" 23 24 "google.golang.org/grpc/internal/grpclog" 25 ) 26 27 // componentData records the settings for a component. 28 type componentData struct { 29 name string 30 } 31 32 var cache = map[string]*componentData{} 33 34 func (c *componentData) InfoDepth(depth int, args ...interface{}) { 35 args = append([]interface{}{"[" + string(c.name) + "]"}, args...) 36 grpclog.InfoDepth(depth+1, args...) 37 } 38 39 func (c *componentData) WarningDepth(depth int, args ...interface{}) { 40 args = append([]interface{}{"[" + string(c.name) + "]"}, args...) 41 grpclog.WarningDepth(depth+1, args...) 42 } 43 44 func (c *componentData) ErrorDepth(depth int, args ...interface{}) { 45 args = append([]interface{}{"[" + string(c.name) + "]"}, args...) 46 grpclog.ErrorDepth(depth+1, args...) 47 } 48 49 func (c *componentData) FatalDepth(depth int, args ...interface{}) { 50 args = append([]interface{}{"[" + string(c.name) + "]"}, args...) 51 grpclog.FatalDepth(depth+1, args...) 52 } 53 54 func (c *componentData) Info(args ...interface{}) { 55 c.InfoDepth(1, args...) 56 } 57 58 func (c *componentData) Warning(args ...interface{}) { 59 c.WarningDepth(1, args...) 60 } 61 62 func (c *componentData) Error(args ...interface{}) { 63 c.ErrorDepth(1, args...) 64 } 65 66 func (c *componentData) Fatal(args ...interface{}) { 67 c.FatalDepth(1, args...) 68 } 69 70 func (c *componentData) Infof(format string, args ...interface{}) { 71 c.InfoDepth(1, fmt.Sprintf(format, args...)) 72 } 73 74 func (c *componentData) Warningf(format string, args ...interface{}) { 75 c.WarningDepth(1, fmt.Sprintf(format, args...)) 76 } 77 78 func (c *componentData) Errorf(format string, args ...interface{}) { 79 c.ErrorDepth(1, fmt.Sprintf(format, args...)) 80 } 81 82 func (c *componentData) Fatalf(format string, args ...interface{}) { 83 c.FatalDepth(1, fmt.Sprintf(format, args...)) 84 } 85 86 func (c *componentData) Infoln(args ...interface{}) { 87 c.InfoDepth(1, args...) 88 } 89 90 func (c *componentData) Warningln(args ...interface{}) { 91 c.WarningDepth(1, args...) 92 } 93 94 func (c *componentData) Errorln(args ...interface{}) { 95 c.ErrorDepth(1, args...) 96 } 97 98 func (c *componentData) Fatalln(args ...interface{}) { 99 c.FatalDepth(1, args...) 100 } 101 102 func (c *componentData) V(l int) bool { 103 return V(l) 104 } 105 106 // Component creates a new component and returns it for logging. If a component 107 // with the name already exists, nothing will be created and it will be 108 // returned. SetLoggerV2 will panic if it is called with a logger created by 109 // Component. 110 func Component(componentName string) DepthLoggerV2 { 111 if cData, ok := cache[componentName]; ok { 112 return cData 113 } 114 c := &componentData{componentName} 115 cache[componentName] = c 116 return c 117 }