|
|
@@ -1,49 +1,80 @@
|
|
|
package fslog
|
|
|
|
|
|
import (
|
|
|
- "log"
|
|
|
+ "go.uber.org/zap"
|
|
|
+ "go.uber.org/zap/zapcore"
|
|
|
"os"
|
|
|
+ "path/filepath"
|
|
|
"runtime/debug"
|
|
|
)
|
|
|
|
|
|
-const (
|
|
|
- levelInfo = "INFO"
|
|
|
- levelDebug = "DEBUG"
|
|
|
- levelError = "ERROR"
|
|
|
- levelWarning = "WARNING"
|
|
|
-)
|
|
|
+var logger *zap.Logger
|
|
|
|
|
|
-const (
|
|
|
- logHeaderFormat = `[level=%s service=%s module=%s subject=%s object=%s operation=%s] %s`
|
|
|
-)
|
|
|
+const logDir = "logs"
|
|
|
|
|
|
func init() {
|
|
|
- log.SetOutput(os.Stdout)
|
|
|
- log.SetFlags(log.Ldate | log.Ltime)
|
|
|
+ if !pathExists(logDir) {
|
|
|
+ err := os.MkdirAll(logDir, os.ModeDir|os.ModePerm)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ logFilePath := filepath.Join(logDir, "log")
|
|
|
+ file, err := os.Create(logFilePath)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ developmentEncoderConfig := zap.NewDevelopmentEncoderConfig()
|
|
|
+ developmentEncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
|
|
+
|
|
|
+ encoder := zapcore.NewConsoleEncoder(developmentEncoderConfig)
|
|
|
+ core := zapcore.NewTee(
|
|
|
+ zapcore.NewCore(encoder, zapcore.AddSync(file), zapcore.DebugLevel),
|
|
|
+ zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), zapcore.DebugLevel),
|
|
|
+ )
|
|
|
+
|
|
|
+ logger = zap.New(core, zap.AddCaller())
|
|
|
+}
|
|
|
+
|
|
|
+func Info(serviceName string, funcName string, callName string, message string) {
|
|
|
+ callerFields := getCallerInfoForLog(serviceName, funcName, callName)
|
|
|
+ logger.Info(message, callerFields...)
|
|
|
}
|
|
|
|
|
|
-func Info(serviceName string, moduleName string, subject string, object string,
|
|
|
- operation string, content string) {
|
|
|
- sendConsoleLog(levelInfo, serviceName, moduleName, subject, object, operation, content)
|
|
|
+func Error(serviceName string, funcName string, callName string, message string) {
|
|
|
+ callerFields := getCallerInfoForLog(serviceName, funcName, callName)
|
|
|
+ logger.Error(message+"\n"+string(debug.Stack()), callerFields...)
|
|
|
}
|
|
|
|
|
|
-func Error(serviceName string, moduleName string, subject string,
|
|
|
- object string, operation string, content string) {
|
|
|
- sendConsoleLog(levelError, serviceName, moduleName, subject, object, operation,
|
|
|
- content+"\n"+string(debug.Stack()))
|
|
|
+func Debug(serviceName string, funcName string, callName string, message string) {
|
|
|
+ callerFields := getCallerInfoForLog(serviceName, funcName, callName)
|
|
|
+ logger.Debug(message, callerFields...)
|
|
|
}
|
|
|
|
|
|
-func Debug(serviceName string, moduleName string, subject string,
|
|
|
- object string, operation string, content string) {
|
|
|
- sendConsoleLog(levelDebug, serviceName, moduleName, subject, object, operation, content)
|
|
|
+func Warning(serviceName string, funcName string, callName string, message string) {
|
|
|
+ callerFields := getCallerInfoForLog(serviceName, funcName, callName)
|
|
|
+ logger.Warn(message, callerFields...)
|
|
|
}
|
|
|
|
|
|
-func Warning(serviceName string, moduleName string, subject string,
|
|
|
- object string, operation string, content string) {
|
|
|
- sendConsoleLog(levelWarning, serviceName, moduleName, subject, object, operation, content)
|
|
|
+func getCallerInfoForLog(serviceName string, funcName string, callName string) []zap.Field {
|
|
|
+ return []zap.Field{
|
|
|
+ zap.String("service", serviceName),
|
|
|
+ zap.String("function", funcName),
|
|
|
+ zap.String("call", callName),
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-func sendConsoleLog(level string, serviceName string, moduleName string, subject string,
|
|
|
- object string, operation string, content string) {
|
|
|
- log.Printf(logHeaderFormat, level, serviceName, moduleName, subject, object, operation, content)
|
|
|
+func pathExists(path string) bool {
|
|
|
+ _, err := os.Stat(path)
|
|
|
+ if err == nil {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ if os.IsNotExist(err) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ return false
|
|
|
}
|