| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You 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. |
| */ |
| |
| package logger |
| |
| import ( |
| "io/ioutil" |
| "log" |
| "os" |
| "path" |
| ) |
| |
| import ( |
| "github.com/dubbogo/getty" |
| perrors "github.com/pkg/errors" |
| "go.uber.org/zap" |
| "go.uber.org/zap/zapcore" |
| "gopkg.in/yaml.v2" |
| ) |
| |
| import ( |
| "github.com/apache/dubbo-go/common/constant" |
| ) |
| |
| var ( |
| logger Logger |
| ) |
| |
| // DubboLogger ... |
| type DubboLogger struct { |
| Logger |
| dynamicLevel zap.AtomicLevel |
| } |
| |
| // Logger ... |
| type Logger interface { |
| Info(args ...interface{}) |
| Warn(args ...interface{}) |
| Error(args ...interface{}) |
| Debug(args ...interface{}) |
| |
| Infof(fmt string, args ...interface{}) |
| Warnf(fmt string, args ...interface{}) |
| Errorf(fmt string, args ...interface{}) |
| Debugf(fmt string, args ...interface{}) |
| } |
| |
| func init() { |
| logConfFile := os.Getenv(constant.APP_LOG_CONF_FILE) |
| err := InitLog(logConfFile) |
| if err != nil { |
| log.Printf("[InitLog] warn: %v", err) |
| } |
| } |
| |
| // InitLog ... |
| func InitLog(logConfFile string) error { |
| if logConfFile == "" { |
| InitLogger(nil) |
| return perrors.New("log configure file name is nil") |
| } |
| if path.Ext(logConfFile) != ".yml" { |
| InitLogger(nil) |
| return perrors.Errorf("log configure file name{%s} suffix must be .yml", logConfFile) |
| } |
| |
| confFileStream, err := ioutil.ReadFile(logConfFile) |
| if err != nil { |
| InitLogger(nil) |
| return perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", logConfFile, err) |
| } |
| |
| conf := &zap.Config{} |
| err = yaml.Unmarshal(confFileStream, conf) |
| if err != nil { |
| InitLogger(nil) |
| return perrors.Errorf("[Unmarshal]init logger error: %v", err) |
| } |
| |
| InitLogger(conf) |
| |
| return nil |
| } |
| |
| // InitLogger ... |
| func InitLogger(conf *zap.Config) { |
| var zapLoggerConfig zap.Config |
| if conf == nil { |
| zapLoggerConfig = zap.NewDevelopmentConfig() |
| zapLoggerEncoderConfig := zapcore.EncoderConfig{ |
| TimeKey: "time", |
| LevelKey: "level", |
| NameKey: "logger", |
| CallerKey: "caller", |
| MessageKey: "message", |
| StacktraceKey: "stacktrace", |
| EncodeLevel: zapcore.CapitalColorLevelEncoder, |
| EncodeTime: zapcore.ISO8601TimeEncoder, |
| EncodeDuration: zapcore.SecondsDurationEncoder, |
| EncodeCaller: zapcore.ShortCallerEncoder, |
| } |
| zapLoggerConfig.EncoderConfig = zapLoggerEncoderConfig |
| } else { |
| zapLoggerConfig = *conf |
| } |
| zapLogger, _ := zapLoggerConfig.Build(zap.AddCallerSkip(1)) |
| //logger = zapLogger.Sugar() |
| logger = &DubboLogger{Logger: zapLogger.Sugar(), dynamicLevel: zapLoggerConfig.Level} |
| |
| // set getty log |
| getty.SetLogger(logger) |
| } |
| |
| // SetLogger ... |
| func SetLogger(log Logger) { |
| logger = log |
| getty.SetLogger(logger) |
| } |
| |
| // GetLogger ... |
| func GetLogger() Logger { |
| return logger |
| } |
| |
| // SetLoggerLevel ... |
| func SetLoggerLevel(level string) bool { |
| if l, ok := logger.(OpsLogger); ok { |
| l.SetLoggerLevel(level) |
| return true |
| } |
| return false |
| } |
| |
| // OpsLogger ... |
| type OpsLogger interface { |
| Logger |
| SetLoggerLevel(level string) |
| } |
| |
| // SetLoggerLevel ... |
| func (dl *DubboLogger) SetLoggerLevel(level string) { |
| l := new(zapcore.Level) |
| l.Set(level) |
| dl.dynamicLevel.SetLevel(*l) |
| } |