123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- // Copyright 2019 PingCAP, Inc.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package log
- import (
- "errors"
- "os"
- "sync"
- "sync/atomic"
- "go.uber.org/zap"
- "go.uber.org/zap/zapcore"
- lumberjack "gopkg.in/natefinch/lumberjack.v2"
- )
- var _globalL, _globalP, _globalS atomic.Value
- var registerOnce sync.Once
- func init() {
- l, p := newStdLogger()
- _globalL.Store(l)
- _globalP.Store(p)
- s := _globalL.Load().(*zap.Logger).Sugar()
- _globalS.Store(s)
- }
- // InitLogger initializes a zap logger.
- func InitLogger(cfg *Config, opts ...zap.Option) (*zap.Logger, *ZapProperties, error) {
- var output zapcore.WriteSyncer
- if len(cfg.File.Filename) > 0 {
- lg, err := initFileLog(&cfg.File)
- if err != nil {
- return nil, nil, err
- }
- output = zapcore.AddSync(lg)
- } else {
- stdOut, _, err := zap.Open([]string{"stdout"}...)
- if err != nil {
- return nil, nil, err
- }
- output = stdOut
- }
- return InitLoggerWithWriteSyncer(cfg, output, opts...)
- }
- // InitLoggerWithWriteSyncer initializes a zap logger with specified write syncer.
- func InitLoggerWithWriteSyncer(cfg *Config, output zapcore.WriteSyncer, opts ...zap.Option) (*zap.Logger, *ZapProperties, error) {
- level := zap.NewAtomicLevel()
- err := level.UnmarshalText([]byte(cfg.Level))
- if err != nil {
- return nil, nil, err
- }
- encoder := newZapTextEncoder(cfg)
- registerOnce.Do(func() {
- err = zap.RegisterEncoder(ZapEncodingName, func(zapcore.EncoderConfig) (zapcore.Encoder, error) {
- return encoder, nil
- })
- })
- if err != nil {
- return nil, nil, err
- }
- core := NewTextCore(encoder, output, level)
- opts = append(cfg.buildOptions(output), opts...)
- lg := zap.New(core, opts...)
- r := &ZapProperties{
- Core: core,
- Syncer: output,
- Level: level,
- }
- return lg, r, nil
- }
- // initFileLog initializes file based logging options.
- func initFileLog(cfg *FileLogConfig) (*lumberjack.Logger, error) {
- if st, err := os.Stat(cfg.Filename); err == nil {
- if st.IsDir() {
- return nil, errors.New("can't use directory as log file name")
- }
- }
- if cfg.MaxSize == 0 {
- cfg.MaxSize = defaultLogMaxSize
- }
- // use lumberjack to logrotate
- return &lumberjack.Logger{
- Filename: cfg.Filename,
- MaxSize: cfg.MaxSize,
- MaxBackups: cfg.MaxBackups,
- MaxAge: cfg.MaxDays,
- LocalTime: true,
- }, nil
- }
- func newStdLogger() (*zap.Logger, *ZapProperties) {
- conf := &Config{Level: "info", File: FileLogConfig{}}
- lg, r, _ := InitLogger(conf)
- return lg, r
- }
- // L returns the global Logger, which can be reconfigured with ReplaceGlobals.
- // It's safe for concurrent use.
- func L() *zap.Logger {
- return _globalL.Load().(*zap.Logger)
- }
- // S returns the global SugaredLogger, which can be reconfigured with
- // ReplaceGlobals. It's safe for concurrent use.
- func S() *zap.SugaredLogger {
- return _globalS.Load().(*zap.SugaredLogger)
- }
- // ReplaceGlobals replaces the global Logger and SugaredLogger.
- // It's safe for concurrent use.
- func ReplaceGlobals(logger *zap.Logger, props *ZapProperties) {
- _globalL.Store(logger)
- _globalS.Store(logger.Sugar())
- _globalP.Store(props)
- }
- // Sync flushes any buffered log entries.
- func Sync() error {
- err := L().Sync()
- if err != nil {
- return err
- }
- return S().Sync()
- }
|