Skip to content

Commit

Permalink
fix: fix logging issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ArmandMeppa committed Feb 24, 2025
1 parent 101733a commit 1782b5c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 34 deletions.
2 changes: 1 addition & 1 deletion scripts/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdenti
$SERVER_NAME = if ($env:SERVER_NAME -ne $null) { $env:SERVER_NAME } else { "wazuh-agent-status" }
$CLIENT_NAME = if ($env:CLIENT_NAME -ne $null) { $env:CLIENT_NAME } else { "wazuh-agent-status-client" }
$PROFILE = if ($env:PROFILE -ne $null) { $env:PROFILE } else { "user" }
$APP_VERSION = if ($env:APP_VERSION -ne $null) { $env:APP_VERSION } else { "0.2.7" }
$APP_VERSION = if ($env:APP_VERSION -ne $null) { $env:APP_VERSION } else { "0.3.0" }

if ($PROFILE -eq "admin") {
$WAS_VERSION = $APP_VERSION
Expand Down
2 changes: 1 addition & 1 deletion scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ DESKTOP_UNIT_FOLDER=${DESKTOP_UNIT_FOLDER:-"$HOME/.config/autostart"}
DESKTOP_UNIT_FILE=${DESKTOP_UNIT_FILE:-"$DESKTOP_UNIT_FOLDER/$CLIENT_NAME.desktop"}

PROFILE=${PROFILE:-"user"}
APP_VERSION=${APP_VERSION:-"0.2.7"}
APP_VERSION=${APP_VERSION:-"0.3.0"}

# Assign app version based on profile
case "$PROFILE" in
Expand Down
59 changes: 38 additions & 21 deletions wazuh-agent-status-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"fmt"
"log"
"net"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"os"
"path/filepath"

"github.com/getlantern/systray"
"gopkg.in/natefinch/lumberjack.v2"
Expand All @@ -21,28 +21,45 @@ var embeddedFiles embed.FS

var (
statusItem, connectionItem, updateItem, versionItem *systray.MenuItem
enabledIcon, disabledIcon []byte
isMonitoringUpdate bool
enabledIcon, disabledIcon []byte
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 Expand Up @@ -309,4 +326,4 @@ func getIconPath() string {
// onExit is called when the application is terminated
func onExit() {
log.Println("Frontend application stopped")
}
}
41 changes: 30 additions & 11 deletions wazuh-agent-status/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,48 @@ import (
"io"
"os"
"os/exec"

"path/filepath"

"gopkg.in/natefinch/lumberjack.v2"
)

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 1782b5c

Please sign in to comment.