github.com/cloudwego/hertz@v0.9.3/pkg/common/hlog/system.go (about) 1 /* 2 * Copyright 2022 CloudWeGo 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 hlog 18 19 import ( 20 "context" 21 "io" 22 "strings" 23 "sync" 24 ) 25 26 var silentMode = false 27 28 // SetSilentMode is used to mute engine error log, 29 // for example: error when reading request headers. 30 // If true, hertz engine will mute it. 31 func SetSilentMode(s bool) { 32 silentMode = s 33 } 34 35 var builderPool = sync.Pool{New: func() interface{} { 36 return &strings.Builder{} // nolint:SA6002 37 }} 38 39 type systemLogger struct { 40 logger FullLogger 41 prefix string 42 } 43 44 func (ll *systemLogger) SetOutput(w io.Writer) { 45 ll.logger.SetOutput(w) 46 } 47 48 func (ll *systemLogger) SetLevel(lv Level) { 49 ll.logger.SetLevel(lv) 50 } 51 52 func (ll *systemLogger) Fatal(v ...interface{}) { 53 v = append([]interface{}{ll.prefix}, v...) 54 ll.logger.Fatal(v...) 55 } 56 57 func (ll *systemLogger) Error(v ...interface{}) { 58 v = append([]interface{}{ll.prefix}, v...) 59 ll.logger.Error(v...) 60 } 61 62 func (ll *systemLogger) Warn(v ...interface{}) { 63 v = append([]interface{}{ll.prefix}, v...) 64 ll.logger.Warn(v...) 65 } 66 67 func (ll *systemLogger) Notice(v ...interface{}) { 68 v = append([]interface{}{ll.prefix}, v...) 69 ll.logger.Notice(v...) 70 } 71 72 func (ll *systemLogger) Info(v ...interface{}) { 73 v = append([]interface{}{ll.prefix}, v...) 74 ll.logger.Info(v...) 75 } 76 77 func (ll *systemLogger) Debug(v ...interface{}) { 78 v = append([]interface{}{ll.prefix}, v...) 79 ll.logger.Debug(v...) 80 } 81 82 func (ll *systemLogger) Trace(v ...interface{}) { 83 v = append([]interface{}{ll.prefix}, v...) 84 ll.logger.Trace(v...) 85 } 86 87 func (ll *systemLogger) Fatalf(format string, v ...interface{}) { 88 ll.logger.Fatalf(ll.addPrefix(format), v...) 89 } 90 91 func (ll *systemLogger) Errorf(format string, v ...interface{}) { 92 if silentMode && format == EngineErrorFormat { 93 return 94 } 95 ll.logger.Errorf(ll.addPrefix(format), v...) 96 } 97 98 func (ll *systemLogger) Warnf(format string, v ...interface{}) { 99 ll.logger.Warnf(ll.addPrefix(format), v...) 100 } 101 102 func (ll *systemLogger) Noticef(format string, v ...interface{}) { 103 ll.logger.Noticef(ll.addPrefix(format), v...) 104 } 105 106 func (ll *systemLogger) Infof(format string, v ...interface{}) { 107 ll.logger.Infof(ll.addPrefix(format), v...) 108 } 109 110 func (ll *systemLogger) Debugf(format string, v ...interface{}) { 111 ll.logger.Debugf(ll.addPrefix(format), v...) 112 } 113 114 func (ll *systemLogger) Tracef(format string, v ...interface{}) { 115 ll.logger.Tracef(ll.addPrefix(format), v...) 116 } 117 118 func (ll *systemLogger) CtxFatalf(ctx context.Context, format string, v ...interface{}) { 119 ll.logger.CtxFatalf(ctx, ll.addPrefix(format), v...) 120 } 121 122 func (ll *systemLogger) CtxErrorf(ctx context.Context, format string, v ...interface{}) { 123 ll.logger.CtxErrorf(ctx, ll.addPrefix(format), v...) 124 } 125 126 func (ll *systemLogger) CtxWarnf(ctx context.Context, format string, v ...interface{}) { 127 ll.logger.CtxWarnf(ctx, ll.addPrefix(format), v...) 128 } 129 130 func (ll *systemLogger) CtxNoticef(ctx context.Context, format string, v ...interface{}) { 131 ll.logger.CtxNoticef(ctx, ll.addPrefix(format), v...) 132 } 133 134 func (ll *systemLogger) CtxInfof(ctx context.Context, format string, v ...interface{}) { 135 ll.logger.CtxInfof(ctx, ll.addPrefix(format), v...) 136 } 137 138 func (ll *systemLogger) CtxDebugf(ctx context.Context, format string, v ...interface{}) { 139 ll.logger.CtxDebugf(ctx, ll.addPrefix(format), v...) 140 } 141 142 func (ll *systemLogger) CtxTracef(ctx context.Context, format string, v ...interface{}) { 143 ll.logger.CtxTracef(ctx, ll.addPrefix(format), v...) 144 } 145 146 func (ll *systemLogger) addPrefix(format string) string { 147 builder := builderPool.Get().(*strings.Builder) 148 builder.Grow(len(format) + len(ll.prefix)) 149 builder.WriteString(ll.prefix) 150 builder.WriteString(format) 151 s := builder.String() 152 builder.Reset() 153 builderPool.Put(builder) // nolint:SA6002 154 return s 155 }