2021-09-07 16:07:58 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
|
// or implied. See the License for the specific language governing permissions and limitations under the License.
|
|
|
|
|
2021-02-19 15:37:04 +08:00
|
|
|
// 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 (
|
2021-03-30 20:19:30 +08:00
|
|
|
"fmt"
|
2021-02-19 15:37:04 +08:00
|
|
|
"os"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2021-03-05 10:15:27 +08:00
|
|
|
"errors"
|
|
|
|
|
2021-10-15 11:46:33 +08:00
|
|
|
"github.com/uber/jaeger-client-go/utils"
|
2021-02-19 15:37:04 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2021-09-06 16:50:42 +08:00
|
|
|
"go.uber.org/zap/zaptest"
|
2021-02-19 15:37:04 +08:00
|
|
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
|
|
|
)
|
|
|
|
|
2021-10-15 11:46:33 +08:00
|
|
|
var _globalL, _globalP, _globalS, _globalR atomic.Value
|
|
|
|
var rateLimiter *utils.ReconfigurableRateLimiter
|
2021-02-19 15:37:04 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
l, p := newStdLogger()
|
|
|
|
_globalL.Store(l)
|
|
|
|
_globalP.Store(p)
|
|
|
|
s := _globalL.Load().(*zap.Logger).Sugar()
|
|
|
|
_globalS.Store(s)
|
2021-10-15 11:46:33 +08:00
|
|
|
|
|
|
|
r := utils.NewRateLimiter(1.0, 60.0)
|
|
|
|
_globalR.Store(r)
|
2021-02-19 15:37:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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...)
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:07:58 +08:00
|
|
|
// InitTestLogger initializes a logger for unit tests
|
2021-09-06 16:50:42 +08:00
|
|
|
func InitTestLogger(t zaptest.TestingT, cfg *Config, opts ...zap.Option) (*zap.Logger, *ZapProperties, error) {
|
|
|
|
writer := newTestingWriter(t)
|
|
|
|
zapOptions := []zap.Option{
|
|
|
|
// Send zap errors to the same writer and mark the test as failed if
|
|
|
|
// that happens.
|
|
|
|
zap.ErrorOutput(writer.WithMarkFailed(true)),
|
|
|
|
}
|
|
|
|
opts = append(zapOptions, opts...)
|
|
|
|
return InitLoggerWithWriteSyncer(cfg, writer, opts...)
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:37:04 +08:00
|
|
|
// 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 {
|
2021-03-30 20:19:30 +08:00
|
|
|
return nil, nil, fmt.Errorf("InitLoggerWithWriteSyncer UnmarshalText cfg.Level err:%w", err)
|
2021-02-19 15:37:04 +08:00
|
|
|
}
|
|
|
|
core := NewTextCore(newZapTextEncoder(cfg), 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) {
|
2021-03-23 18:50:13 +08:00
|
|
|
conf := &Config{Level: "debug", File: FileLogConfig{}}
|
2021-02-19 15:37:04 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-10-15 20:18:41 +08:00
|
|
|
// R returns utils.ReconfigurableRateLimiter.
|
2021-10-15 11:46:33 +08:00
|
|
|
func R() *utils.ReconfigurableRateLimiter {
|
|
|
|
return _globalR.Load().(*utils.ReconfigurableRateLimiter)
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:37:04 +08:00
|
|
|
// 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()
|
|
|
|
}
|