github.com/polarismesh/polaris@v1.17.8/common/log/default.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package log 19 20 import ( 21 "fmt" 22 23 "go.uber.org/zap/zapcore" 24 ) 25 26 var defaultScope = RegisterScope(DefaultLoggerName, "Unscoped logging messages.", 0) 27 28 // Fatal outputs a message at fatal level. 29 func Fatal(msg string, fields ...zapcore.Field) { 30 if defaultScope.GetOutputLevel() >= FatalLevel { 31 defaultScope.emit(zapcore.FatalLevel, defaultScope.GetStackTraceLevel() >= FatalLevel, msg, fields) 32 } 33 } 34 35 // Fatala uses fmt.Sprint to construct and log a message at fatal level. 36 func Fatala(args ...interface{}) { 37 if defaultScope.GetOutputLevel() >= FatalLevel { 38 defaultScope.emit(zapcore.FatalLevel, defaultScope.GetStackTraceLevel() >= FatalLevel, fmt.Sprint(args...), nil) 39 } 40 } 41 42 // Fatalf uses fmt.Sprintf to construct and log a MESSAGE at fatal level. 43 func Fatalf(template string, args ...interface{}) { 44 if defaultScope.GetOutputLevel() >= FatalLevel { 45 msg := template 46 if len(args) > 0 { 47 msg = fmt.Sprintf(template, args...) 48 } 49 defaultScope.emit(zapcore.FatalLevel, defaultScope.GetStackTraceLevel() >= FatalLevel, msg, nil) 50 } 51 } 52 53 // FatalEnabled returns whether output of messages using this scope is currently enabled for fatal-level output. 54 func FatalEnabled() bool { 55 return defaultScope.GetOutputLevel() >= FatalLevel 56 } 57 58 // Error outputs a message at error level. 59 func Error(msg string, fields ...zapcore.Field) { 60 if defaultScope.GetOutputLevel() >= ErrorLevel { 61 defaultScope.emit(zapcore.ErrorLevel, defaultScope.GetStackTraceLevel() >= ErrorLevel, msg, fields) 62 } 63 } 64 65 // Errora uses fmt.Sprint to construct and log a message at error level. 66 func Errora(args ...interface{}) { 67 if defaultScope.GetOutputLevel() >= ErrorLevel { 68 defaultScope.emit(zapcore.ErrorLevel, defaultScope.GetStackTraceLevel() >= ErrorLevel, fmt.Sprint(args...), nil) 69 } 70 } 71 72 // Errorf uses fmt.Sprintf to construct and log a message at error level. 73 func Errorf(template string, args ...interface{}) { 74 if defaultScope.GetOutputLevel() >= ErrorLevel { 75 msg := template 76 if len(args) > 0 { 77 msg = fmt.Sprintf(template, args...) 78 } 79 defaultScope.emit(zapcore.ErrorLevel, defaultScope.GetStackTraceLevel() >= ErrorLevel, msg, nil) 80 } 81 } 82 83 // ErrorEnabled returns whether output of messages using this scope is currently enabled for error-level output. 84 func ErrorEnabled() bool { 85 return defaultScope.GetOutputLevel() >= ErrorLevel 86 } 87 88 // Warn outputs a message at warn level. 89 func Warn(msg string, fields ...zapcore.Field) { 90 if defaultScope.GetOutputLevel() >= WarnLevel { 91 defaultScope.emit(zapcore.WarnLevel, defaultScope.GetStackTraceLevel() >= WarnLevel, msg, fields) 92 } 93 } 94 95 // Warna uses fmt.Sprint to construct and log a message at warn level. 96 func Warna(args ...interface{}) { 97 if defaultScope.GetOutputLevel() >= WarnLevel { 98 defaultScope.emit(zapcore.WarnLevel, defaultScope.GetStackTraceLevel() >= WarnLevel, fmt.Sprint(args...), nil) 99 } 100 } 101 102 // Warnf uses fmt.Sprintf to construct and log a message at warn level. 103 func Warnf(template string, args ...interface{}) { 104 if defaultScope.GetOutputLevel() >= WarnLevel { 105 msg := template 106 if len(args) > 0 { 107 msg = fmt.Sprintf(template, args...) 108 } 109 defaultScope.emit(zapcore.WarnLevel, defaultScope.GetStackTraceLevel() >= WarnLevel, msg, nil) 110 } 111 } 112 113 // WarnEnabled returns whether output of messages using this scope is currently enabled for warn-level output. 114 func WarnEnabled() bool { 115 return defaultScope.GetOutputLevel() >= WarnLevel 116 } 117 118 // Info outputs a message at info level. 119 func Info(msg string, fields ...zapcore.Field) { 120 if defaultScope.GetOutputLevel() >= InfoLevel { 121 defaultScope.emit(zapcore.InfoLevel, defaultScope.GetStackTraceLevel() >= InfoLevel, msg, fields) 122 } 123 } 124 125 // Infoa uses fmt.Sprint to construct and log a message at info level. 126 func Infoa(args ...interface{}) { 127 if defaultScope.GetOutputLevel() >= InfoLevel { 128 defaultScope.emit(zapcore.InfoLevel, defaultScope.GetStackTraceLevel() >= InfoLevel, fmt.Sprint(args...), nil) 129 } 130 } 131 132 // Infof uses fmt.Sprintf to construct and log a message at info level. 133 func Infof(template string, args ...interface{}) { 134 if defaultScope.GetOutputLevel() >= InfoLevel { 135 msg := template 136 if len(args) > 0 { 137 msg = fmt.Sprintf(template, args...) 138 } 139 defaultScope.emit(zapcore.InfoLevel, defaultScope.GetStackTraceLevel() >= InfoLevel, msg, nil) 140 } 141 } 142 143 // InfoEnabled returns whether output of messages using this scope is currently enabled for info-level output. 144 func InfoEnabled() bool { 145 return defaultScope.GetOutputLevel() >= InfoLevel 146 } 147 148 // Debug outputs a message at debug level. 149 func Debug(msg string, fields ...zapcore.Field) { 150 if defaultScope.GetOutputLevel() >= DebugLevel { 151 defaultScope.emit(zapcore.DebugLevel, defaultScope.GetStackTraceLevel() >= DebugLevel, msg, fields) 152 } 153 } 154 155 // Debuga uses fmt.Sprint to construct and log a message at debug level. 156 func Debuga(args ...interface{}) { 157 if defaultScope.GetOutputLevel() >= DebugLevel { 158 defaultScope.emit(zapcore.DebugLevel, defaultScope.GetStackTraceLevel() >= DebugLevel, fmt.Sprint(args...), nil) 159 } 160 } 161 162 // Debugf uses fmt.Sprintf to construct and log a message at debug level. 163 func Debugf(template string, args ...interface{}) { 164 if defaultScope.GetOutputLevel() >= DebugLevel { 165 msg := template 166 if len(args) > 0 { 167 msg = fmt.Sprintf(template, args...) 168 } 169 defaultScope.emit(zapcore.DebugLevel, defaultScope.GetStackTraceLevel() >= DebugLevel, msg, nil) 170 } 171 } 172 173 // DebugEnabled returns whether output of messages using this scope is currently enabled for debug-level output. 174 func DebugEnabled() bool { 175 return defaultScope.GetOutputLevel() >= DebugLevel 176 }