|
|
@ -1,9 +1,12 @@ |
|
|
|
package log |
|
|
|
|
|
|
|
import ( |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"io/fs" |
|
|
|
"io/ioutil" |
|
|
|
"log" |
|
|
|
"os" |
|
|
|
"path" |
|
|
|
|
|
|
|
"github.com/BurntSushi/toml" |
|
|
@ -37,6 +40,7 @@ type Logger interface { |
|
|
|
|
|
|
|
func init() { |
|
|
|
var ( |
|
|
|
cfg LogCfg |
|
|
|
logCfg GlobalCfg |
|
|
|
) |
|
|
|
|
|
|
@ -45,7 +49,7 @@ func init() { |
|
|
|
if _, err := toml.DecodeFile(ConfigFile, &logCfg); err != nil { |
|
|
|
//log.Fatalf("Read config file err:%s", err.Error())
|
|
|
|
log.Println("failed to open config file") |
|
|
|
if err = InitLog(""); nil != err { |
|
|
|
if err = InitLog("", cfg); nil != err { |
|
|
|
log.Println(err.Error()) |
|
|
|
} |
|
|
|
return |
|
|
@ -55,15 +59,15 @@ func init() { |
|
|
|
cfg.OutputPaths = cfg.LogPath + cfg.APPName + ".log" |
|
|
|
cfg.ErrorOutputPaths = cfg.LogPath + cfg.APPName + ".error" |
|
|
|
|
|
|
|
err := InitLog(cfg.LogConf) |
|
|
|
err := InitLog(cfg.LogConf, cfg) |
|
|
|
if err != nil { |
|
|
|
log.Printf("[InitLog] warn: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func ReloadLogger(configFile string) { |
|
|
|
|
|
|
|
var ( |
|
|
|
cfg LogCfg |
|
|
|
logCfg GlobalCfg |
|
|
|
) |
|
|
|
|
|
|
@ -72,21 +76,65 @@ func ReloadLogger(configFile string) { |
|
|
|
log.Fatalf("Read config file err:%s", err.Error()) |
|
|
|
} |
|
|
|
cfg = logCfg.Logs |
|
|
|
|
|
|
|
cfg.OutputPaths = cfg.LogPath + cfg.APPName + ".log" |
|
|
|
cfg.ErrorOutputPaths = cfg.LogPath + cfg.APPName + ".error" |
|
|
|
|
|
|
|
err := InitLog(cfg.LogConf) |
|
|
|
err := InitLog(cfg.LogConf, cfg) |
|
|
|
if err != nil { |
|
|
|
log.Printf("[ReloadLogger] warn: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func InitLog(logConfFile string) error { |
|
|
|
if logConfFile == "" { |
|
|
|
InitLogger(nil) |
|
|
|
return fmt.Errorf("log configure file name is nil") |
|
|
|
func RotateLogger(cfg LogCfg) { |
|
|
|
cfg.OutputPaths = cfg.LogInfoPath + cfg.APPName + ".log" |
|
|
|
cfg.ErrorOutputPaths = cfg.LogErrPath + cfg.APPName + ".error" |
|
|
|
if _, err := os.Stat(cfg.OutputPaths); errors.Is(err, fs.ErrNotExist) { |
|
|
|
if _, err = os.Create(cfg.OutputPaths); nil != err { |
|
|
|
log.Printf("[RotateLogger] error: %s", err.Error()) |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
//if _, err := os.Stat(cfg.ErrorOutputPaths); errors.Is(err, fs.ErrNotExist) {
|
|
|
|
// if _, err = os.Create(cfg.ErrorOutputPaths); nil != err {
|
|
|
|
// log.Printf("[RotateLogger] error: %s", err.Error())
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
err := InitLog(cfg.LogConf, cfg) |
|
|
|
if err != nil { |
|
|
|
log.Printf("[RotateLogger] warn: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var defaultZapConfig = []byte(` |
|
|
|
level: "info" |
|
|
|
development: false |
|
|
|
disableCaller: false |
|
|
|
disableStacktrace: false |
|
|
|
sampling: |
|
|
|
encoding: "console" |
|
|
|
|
|
|
|
# encoder |
|
|
|
encoderConfig: |
|
|
|
messageKey: "message" |
|
|
|
levelKey: "level" |
|
|
|
timeKey: "time" |
|
|
|
nameKey: "logger" |
|
|
|
callerKey: "caller" |
|
|
|
stacktraceKey: "stacktrace" |
|
|
|
lineEnding: "" |
|
|
|
# levelEncoder: "capitalColor" |
|
|
|
timeEncoder: "iso8601" |
|
|
|
durationEncoder: "seconds" |
|
|
|
callerEncoder: "short" |
|
|
|
nameEncoder: "" |
|
|
|
`) |
|
|
|
|
|
|
|
func InitLog(logConfFile string, cfg LogCfg) error { |
|
|
|
conf := &zap.Config{} |
|
|
|
if logConfFile != "" { |
|
|
|
//InitLogger(nil)
|
|
|
|
//return fmt.Errorf("log configure file name is nil")
|
|
|
|
if path.Ext(logConfFile) != ".yml" { |
|
|
|
InitLogger(nil) |
|
|
|
return fmt.Errorf("log configure file name{%s} suffix must be .yml", logConfFile) |
|
|
@ -98,17 +146,47 @@ func InitLog(logConfFile string) error { |
|
|
|
return fmt.Errorf("ioutil.ReadFile(file:%s) = error:%v", logConfFile, err) |
|
|
|
} |
|
|
|
|
|
|
|
conf := &zap.Config{} |
|
|
|
err = yaml.Unmarshal(confFileStream, conf) |
|
|
|
if err != nil { |
|
|
|
InitLogger(nil) |
|
|
|
return fmt.Errorf("[Unmarshal]init logger error: %v", err) |
|
|
|
} |
|
|
|
} else { |
|
|
|
conf = &zap.Config{ |
|
|
|
Level: zap.NewAtomicLevel(), |
|
|
|
Development: false, |
|
|
|
DisableCaller: false, |
|
|
|
DisableStacktrace: false, |
|
|
|
Sampling: nil, |
|
|
|
Encoding: "console", |
|
|
|
EncoderConfig: zapcore.EncoderConfig{ |
|
|
|
MessageKey: "message", |
|
|
|
LevelKey: "level", |
|
|
|
TimeKey: "time", |
|
|
|
NameKey: "logger", |
|
|
|
CallerKey: "caller", |
|
|
|
StacktraceKey: "stacktrace", |
|
|
|
LineEnding: "", |
|
|
|
EncodeLevel: zapcore.LowercaseLevelEncoder, |
|
|
|
EncodeTime: zapcore.EpochMillisTimeEncoder, |
|
|
|
EncodeDuration: zapcore.SecondsDurationEncoder, |
|
|
|
EncodeCaller: zapcore.ShortCallerEncoder, |
|
|
|
EncodeName: nil, |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
err := yaml.Unmarshal(defaultZapConfig, conf) |
|
|
|
if err != nil { |
|
|
|
InitLogger(nil) |
|
|
|
return fmt.Errorf("[Unmarshal]init logger error: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
log.Println("to set yuanex logger ...") |
|
|
|
|
|
|
|
conf.OutputPaths = append(conf.OutputPaths, cfg.OutputPaths) |
|
|
|
conf.ErrorOutputPaths = append(conf.ErrorOutputPaths, cfg.ErrorOutputPaths) |
|
|
|
log.Println("OutputPahts -->", conf.OutputPaths) |
|
|
|
log.Println("ErrorOutputPahts -->", conf.ErrorOutputPaths) |
|
|
|
InitLogger(conf) |
|
|
|
|
|
|
|