github.com/go-spring/spring-base@v1.1.3/log/log_logger.go (about) 1 /* 2 * Copyright 2012-2019 the original author or 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 * https://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 log 18 19 import ( 20 "context" 21 22 "github.com/go-spring/spring-base/atomic" 23 ) 24 25 type Logger struct { 26 value atomic.Value 27 name string 28 } 29 30 // wrapperConfig atomic.Value 要求底层数据完全一致。 31 type wrapperConfig struct { 32 config privateConfig 33 } 34 35 func newLogger(name string) *Logger { 36 return &Logger{name: name} 37 } 38 39 // Name returns the logger's name. 40 func (l *Logger) Name() string { 41 return l.name 42 } 43 44 func (l *Logger) config() privateConfig { 45 return l.value.Load().(*wrapperConfig).config 46 } 47 48 func (l *Logger) reconfigure(config privateConfig) { 49 l.value.Store(&wrapperConfig{config}) 50 } 51 52 func (l *Logger) Level() Level { 53 return l.config().getLevel() 54 } 55 56 func (l *Logger) Appenders() []Appender { 57 c := l.config() 58 var appenders []Appender 59 for _, ref := range c.getAppenders() { 60 appenders = append(appenders, ref.appender) 61 } 62 return appenders 63 } 64 65 // WithSkip 创建包含 skip 信息的 Entry 。 66 func (l *Logger) WithSkip(n int) *Entry { 67 return &Entry{pub: l.config(), skip: n} 68 } 69 70 // WithTag 创建包含 tag 信息的 Entry 。 71 func (l *Logger) WithTag(tag string) *Entry { 72 return &Entry{pub: l.config(), tag: tag} 73 } 74 75 // WithContext 创建包含 context.Context 对象的 Entry 。 76 func (l *Logger) WithContext(ctx context.Context) *Entry { 77 return &Entry{pub: l.config(), ctx: ctx} 78 } 79 80 // Trace outputs log with level TraceLevel. 81 func (l *Logger) Trace(args ...interface{}) *Event { 82 return l.WithSkip(1).Trace(args...) 83 } 84 85 // Tracef outputs log with level TraceLevel. 86 func (l *Logger) Tracef(format string, args ...interface{}) *Event { 87 return l.WithSkip(1).Tracef(format, args...) 88 } 89 90 // Tracew outputs log with level TraceLevel. 91 func (l *Logger) Tracew(fields ...Field) *Event { 92 return l.WithSkip(1).Tracew(fields...) 93 } 94 95 // Debug outputs log with level DebugLevel. 96 func (l *Logger) Debug(args ...interface{}) *Event { 97 return l.WithSkip(1).Debug(args...) 98 } 99 100 // Debugf outputs log with level DebugLevel. 101 func (l *Logger) Debugf(format string, args ...interface{}) *Event { 102 return l.WithSkip(1).Debugf(format, args...) 103 } 104 105 // Debugw outputs log with level DebugLevel. 106 func (l *Logger) Debugw(fields ...Field) *Event { 107 return l.WithSkip(1).Debugw(fields...) 108 } 109 110 // Info outputs log with level InfoLevel. 111 func (l *Logger) Info(args ...interface{}) *Event { 112 return l.WithSkip(1).Info(args...) 113 } 114 115 // Infof outputs log with level InfoLevel. 116 func (l *Logger) Infof(format string, args ...interface{}) *Event { 117 return l.WithSkip(1).Infof(format, args...) 118 } 119 120 // Infow outputs log with level InfoLevel. 121 func (l *Logger) Infow(fields ...Field) *Event { 122 return l.WithSkip(1).Infow(fields...) 123 } 124 125 // Warn outputs log with level WarnLevel. 126 func (l *Logger) Warn(args ...interface{}) *Event { 127 return l.WithSkip(1).Warn(args...) 128 } 129 130 // Warnf outputs log with level WarnLevel. 131 func (l *Logger) Warnf(format string, args ...interface{}) *Event { 132 return l.WithSkip(1).Warnf(format, args...) 133 } 134 135 // Warnw outputs log with level WarnLevel. 136 func (l *Logger) Warnw(fields ...Field) *Event { 137 return l.WithSkip(1).Warnw(fields...) 138 } 139 140 // Error outputs log with level ErrorLevel. 141 func (l *Logger) Error(args ...interface{}) *Event { 142 return l.WithSkip(1).Error(args...) 143 } 144 145 // Errorf outputs log with level ErrorLevel. 146 func (l *Logger) Errorf(format string, args ...interface{}) *Event { 147 return l.WithSkip(1).Errorf(format, args...) 148 } 149 150 // Errorw outputs log with level ErrorLevel. 151 func (l *Logger) Errorw(fields ...Field) *Event { 152 return l.WithSkip(1).Errorw(fields...) 153 } 154 155 // Panic outputs log with level PanicLevel. 156 func (l *Logger) Panic(args ...interface{}) *Event { 157 return l.WithSkip(1).Panic(args...) 158 } 159 160 // Panicf outputs log with level PanicLevel. 161 func (l *Logger) Panicf(format string, args ...interface{}) *Event { 162 return l.WithSkip(1).Panicf(format, args...) 163 } 164 165 // Panicw outputs log with level PanicLevel. 166 func (l *Logger) Panicw(fields ...Field) *Event { 167 return l.WithSkip(1).Panicw(fields...) 168 } 169 170 // Fatal outputs log with level FatalLevel. 171 func (l *Logger) Fatal(args ...interface{}) *Event { 172 return l.WithSkip(1).Fatal(args...) 173 } 174 175 // Fatalf outputs log with level FatalLevel. 176 func (l *Logger) Fatalf(format string, args ...interface{}) *Event { 177 return l.WithSkip(1).Fatalf(format, args...) 178 } 179 180 // Fatalw outputs log with level FatalLevel. 181 func (l *Logger) Fatalw(fields ...Field) *Event { 182 return l.WithSkip(1).Fatalw(fields...) 183 }