|
@@ -10,6 +10,7 @@ import (
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
"os"
|
|
"os"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
|
|
+ "strings"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -36,6 +37,14 @@ func NewLog(conf *viper.Viper) *Logger {
|
|
func NewServiceLog(conf *viper.Viper, serviceType ServiceType) *Logger {
|
|
func NewServiceLog(conf *viper.Viper, serviceType ServiceType) *Logger {
|
|
var logPath string
|
|
var logPath string
|
|
|
|
|
|
|
|
+ // 从环境变量检查服务类型
|
|
|
|
+ envServiceType := os.Getenv("SERVICE_TYPE")
|
|
|
|
+ if envServiceType == "api" {
|
|
|
|
+ serviceType = API
|
|
|
|
+ } else if envServiceType == "task" {
|
|
|
|
+ serviceType = Task
|
|
|
|
+ }
|
|
|
|
+
|
|
// 根据服务类型和日期生成日志文件名
|
|
// 根据服务类型和日期生成日志文件名
|
|
logFormat := conf.GetString("log.log_format") // 日期格式,如"2006-01-02"
|
|
logFormat := conf.GetString("log.log_format") // 日期格式,如"2006-01-02"
|
|
// 确保有默认的日期格式
|
|
// 确保有默认的日期格式
|
|
@@ -44,6 +53,10 @@ func NewServiceLog(conf *viper.Viper, serviceType ServiceType) *Logger {
|
|
}
|
|
}
|
|
currentDate := time.Now().Format(logFormat)
|
|
currentDate := time.Now().Format(logFormat)
|
|
|
|
|
|
|
|
+ // 显示调试信息
|
|
|
|
+ fmt.Printf("[日志初始化] 服务类型: %s, 日期: %s\n",
|
|
|
|
+ serviceTypeToString(serviceType), currentDate)
|
|
|
|
+
|
|
if serviceType == API && conf.IsSet("log.api_log_file") {
|
|
if serviceType == API && conf.IsSet("log.api_log_file") {
|
|
// 使用API日志路径
|
|
// 使用API日志路径
|
|
logPath = conf.GetString("log.api_log_file")
|
|
logPath = conf.GetString("log.api_log_file")
|
|
@@ -57,6 +70,25 @@ func NewServiceLog(conf *viper.Viper, serviceType ServiceType) *Logger {
|
|
logPath = conf.GetString("log.log_file_name")
|
|
logPath = conf.GetString("log.log_file_name")
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 处理相对路径,兼容不同的工作目录
|
|
|
|
+ if strings.HasPrefix(logPath, "./") {
|
|
|
|
+ basePath := "/data/app" // 容器中的日志基础路径
|
|
|
|
+ if _, err := os.Stat(basePath); os.IsNotExist(err) {
|
|
|
|
+ // 如果/data/app不存在,则使用当前目录
|
|
|
|
+ workDir, err := os.Getwd()
|
|
|
|
+ if err == nil {
|
|
|
|
+ basePath = workDir
|
|
|
|
+ } else {
|
|
|
|
+ basePath = "."
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 将相对路径转换为绝对路径
|
|
|
|
+ logPath = strings.Replace(logPath, "./", basePath+"/", 1)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 显示实际使用的日志路径
|
|
|
|
+ fmt.Printf("[日志路径] %s\n", logPath)
|
|
|
|
+
|
|
// 确保日志目录存在
|
|
// 确保日志目录存在
|
|
if err := ensureDirExists(logPath); err != nil {
|
|
if err := ensureDirExists(logPath); err != nil {
|
|
// 如果创建目录失败,回退到临时目录
|
|
// 如果创建目录失败,回退到临时目录
|