mirror of
https://github.com/laoxong/nofx.git
synced 2026-06-04 09:58:22 +08:00
feat: write logs to data directory
- Log files are saved to data/nofx_YYYY-MM-DD.log - Removed decision_logs mount from docker-compose
This commit is contained in:
@@ -17,9 +17,8 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "${NOFX_BACKEND_PORT:-8080}:8080"
|
- "${NOFX_BACKEND_PORT:-8080}:8080"
|
||||||
volumes:
|
volumes:
|
||||||
# Data directory for database (new images use data/data.db)
|
# Data directory for database and logs
|
||||||
- ./data:/app/data
|
- ./data:/app/data
|
||||||
- ./decision_logs:/app/decision_logs
|
|
||||||
- /etc/localtime:/etc/localtime:ro
|
- /etc/localtime:/etc/localtime:ro
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
|
|||||||
+25
-2
@@ -2,10 +2,12 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@@ -13,6 +15,8 @@ import (
|
|||||||
var (
|
var (
|
||||||
// Log is the global logger instance
|
// Log is the global logger instance
|
||||||
Log *logrus.Logger
|
Log *logrus.Logger
|
||||||
|
// logFile holds the current log file handle
|
||||||
|
logFile *os.File
|
||||||
)
|
)
|
||||||
|
|
||||||
// compactFormatter is a custom formatter for cleaner log output
|
// compactFormatter is a custom formatter for cleaner log output
|
||||||
@@ -78,7 +82,23 @@ func Init(cfg *Config) error {
|
|||||||
|
|
||||||
// Set compact formatter
|
// Set compact formatter
|
||||||
Log.SetFormatter(&compactFormatter{})
|
Log.SetFormatter(&compactFormatter{})
|
||||||
Log.SetOutput(os.Stdout)
|
|
||||||
|
// Setup log file output (write to both stdout and file)
|
||||||
|
logDir := "data"
|
||||||
|
if err := os.MkdirAll(logDir, 0755); err == nil {
|
||||||
|
logFileName := filepath.Join(logDir, fmt.Sprintf("nofx_%s.log", time.Now().Format("2006-01-02")))
|
||||||
|
f, err := os.OpenFile(logFileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||||
|
if err == nil {
|
||||||
|
logFile = f
|
||||||
|
// Write to both stdout and file
|
||||||
|
Log.SetOutput(io.MultiWriter(os.Stdout, f))
|
||||||
|
} else {
|
||||||
|
Log.SetOutput(os.Stdout)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.SetOutput(os.Stdout)
|
||||||
|
}
|
||||||
|
|
||||||
Log.SetReportCaller(true)
|
Log.SetReportCaller(true)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -92,7 +112,10 @@ func InitWithSimpleConfig(level string) error {
|
|||||||
|
|
||||||
// Shutdown gracefully shuts down the logger
|
// Shutdown gracefully shuts down the logger
|
||||||
func Shutdown() {
|
func Shutdown() {
|
||||||
// Reserved for future extensions
|
if logFile != nil {
|
||||||
|
logFile.Close()
|
||||||
|
logFile = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|||||||
Reference in New Issue
Block a user