Skip to content

Commit

Permalink
Merge pull request #33 from ADORSYS-GIS/develop
Browse files Browse the repository at this point in the history
fix: fix logging issue
  • Loading branch information
ArmandMeppa authored Feb 24, 2025
2 parents 622dc70 + 54aca19 commit bd2c8f8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 26 deletions.
49 changes: 33 additions & 16 deletions wazuh-agent-status-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,41 @@ var (
isMonitoringUpdate bool
)

// Set up log rotation using lumberjack.func init() {
func init() {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("failed to get home directory: %v", err)
}

logFilePath := filepath.Join(homeDir, ".wazuh-agent-status-client.log")

log.SetOutput(&lumberjack.Logger{
Filename: logFilePath,
MaxSize: 10,
MaxBackups: 3,
MaxAge: 28,
Compress: true,
})
func getUserLogFilePath() string {
var logDir string

switch runtime.GOOS {
case "linux":
logDir = filepath.Join(os.Getenv("HOME"), ".wazuh")
case "darwin":
logDir = filepath.Join(os.Getenv("HOME"), "Library", "Logs", "wazuh")
case "windows":
logDir = filepath.Join(os.Getenv("APPDATA"), "wazuh", "logs")
default:
logDir = "./logs" // Fallback
}

// Ensure the directory exists
if err := os.MkdirAll(logDir, 0755); err != nil {
log.Fatalf("failed to create log directory: %v", err)
}

return filepath.Join(logDir, "wazuh-agent-status-client.log")
}

// Set up log rotation using lumberjack.func init() {
func init() {
logFilePath := getUserLogFilePath()

log.SetOutput(&lumberjack.Logger{
Filename: logFilePath,
MaxSize: 10,
MaxBackups: 3,
MaxAge: 30,
Compress: true,
})
}

// Main entry point
func main() {
log.Println("Starting frontend...")
Expand Down
39 changes: 29 additions & 10 deletions wazuh-agent-status/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,40 @@ import (
const versionURL = "https://raw.githubusercontent.com/ADORSYS-GIS/wazuh-agent/main/version.txt"
var isUpdateInProgress bool // Flag to track if the update is in progress

func init() {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("failed to get home directory: %v", err)
func getSystemLogFilePath() string {
var logDir string

switch runtime.GOOS {
case "linux", "darwin":
logDir = "/var/log"
case "windows":
logDir = "C:\\ProgramData\\wazuh\\logs"
default:
logDir = "./logs"
}

// Ensure the directory exists (Windows only, since /var/log usually exists)
if runtime.GOOS == "windows" {
if err := os.MkdirAll(logDir, 0755); err != nil {
log.Fatalf("failed to create log directory: %v", err)
}
}

logFilePath := filepath.Join(homeDir, "wazuh-agent-status.log")
return filepath.Join(logDir, "wazuh-agent-status.log")
}

func init() {
logFilePath := getSystemLogFilePath()

log.SetOutput(&lumberjack.Logger{
Filename: logFilePath,
MaxSize: 10,
MaxBackups: 3,
MaxAge: 28,
Compress: true,
Filename: logFilePath,
MaxSize: 10, // MB
MaxBackups: 3,
MaxAge: 28, // days
Compress: true,
})

log.Printf("Logging to: %s", logFilePath) // Debugging info
}

func main() {
Expand Down

0 comments on commit bd2c8f8

Please sign in to comment.