@@ -220,6 +220,98 @@ func NewJSONLogger(opts ...Option) (*zap.Logger, error) {
220220 return log , nil
221221}
222222
223+ // NewTextLogger return a simple-encoder zap logger,
224+ func NewTextLogger (opts ... Option ) (* zap.Logger , error ) {
225+ opt := & option {level : DefaultLevel , fields : make (map [string ]string )}
226+ for _ , f := range opts {
227+ f (opt )
228+ }
229+
230+ timeLayout := DefaultTimeLayout
231+ if opt .timeLayout != "" {
232+ timeLayout = opt .timeLayout
233+ }
234+
235+ // similar to zap.NewProductionEncoderConfig()
236+ encoderConfig := zapcore.EncoderConfig {
237+ TimeKey : "time" ,
238+ LevelKey : "level" ,
239+ NameKey : "logger" , // used by logger.Named(key); optional; useless
240+ CallerKey : "caller" ,
241+ MessageKey : "msg" ,
242+ StacktraceKey : "stacktrace" , // use by zap.AddStacktrace; optional; useless
243+ LineEnding : zapcore .DefaultLineEnding ,
244+ EncodeLevel : zapcore .LowercaseLevelEncoder , // 小写编码器
245+ EncodeTime : func (t time.Time , enc zapcore.PrimitiveArrayEncoder ) {
246+ enc .AppendString (t .Format (timeLayout ))
247+ },
248+ EncodeDuration : zapcore .MillisDurationEncoder ,
249+ EncodeCaller : zapcore .ShortCallerEncoder , // 全路径编码器
250+ }
251+
252+ if opt .highlighting {
253+ encoderConfig .EncodeLevel = zapcore .LowercaseColorLevelEncoder
254+ }
255+
256+ jsonEncoder := zapcore .NewConsoleEncoder (encoderConfig )
257+
258+ // Initialize atomicLevel with the initial logging level
259+ atomicLevel = zap .NewAtomicLevelAt (opt .level )
260+
261+ // lowPriority usd by info\debug\warn
262+ lowPriority := zap .LevelEnablerFunc (func (lvl zapcore.Level ) bool {
263+ return lvl >= atomicLevel .Level () && lvl < zapcore .ErrorLevel
264+ })
265+
266+ // highPriority usd by error\panic\fatal
267+ highPriority := zap .LevelEnablerFunc (func (lvl zapcore.Level ) bool {
268+ return lvl >= atomicLevel .Level () && lvl >= zapcore .ErrorLevel
269+ })
270+
271+ stdout := zapcore .Lock (os .Stdout ) // lock for concurrent safe
272+ stderr := zapcore .Lock (os .Stderr ) // lock for concurrent safe
273+
274+ core := zapcore .NewTee ()
275+
276+ if ! opt .disableConsole {
277+ core = zapcore .NewTee (
278+ zapcore .NewCore (jsonEncoder ,
279+ zapcore .NewMultiWriteSyncer (stdout ),
280+ lowPriority ,
281+ ),
282+ zapcore .NewCore (jsonEncoder ,
283+ zapcore .NewMultiWriteSyncer (stderr ),
284+ highPriority ,
285+ ),
286+ )
287+ }
288+
289+ if opt .file != nil {
290+ core = zapcore .NewTee (core ,
291+ zapcore .NewCore (jsonEncoder ,
292+ zapcore .AddSync (opt .file ),
293+ zap .LevelEnablerFunc (func (lvl zapcore.Level ) bool {
294+ return lvl >= atomicLevel .Level ()
295+ }),
296+ ),
297+ )
298+ }
299+
300+ log := zap .New (core ,
301+ zap .AddCaller (),
302+ zap .ErrorOutput (stderr ),
303+ )
304+
305+ for key , value := range opt .fields {
306+ log = log .WithOptions (zap .Fields (zapcore.Field {Key : key , Type : zapcore .StringType , String : value }))
307+ }
308+
309+ // 给全局logger对象
310+ logger = log
311+
312+ return log , nil
313+ }
314+
223315var _ Meta = (* meta )(nil )
224316
225317// Meta key-value
0 commit comments