DEV Community

Query Filter
Query Filter

Posted on

bridge-7

public static String resolveLogDir() {
    try {
        // 1. First, check your application's explicit config property
        String configPath = System.getProperty("config_file");

        // 2. If missing, look for the standard JVM logging configuration file flag
        if (configPath == null || configPath.trim().isEmpty()) {
            configPath = System.getProperty("java.util.logging.config.file");
        }

        // 3. If we found a path from either property, extract its parent directory layout
        if (configPath != null && !configPath.trim().isEmpty()) {
            File configFile = new File(configPath.trim());
            File configDir = configFile.getParentFile(); // e.g., /opt/CPLS/config or /opt/CPLS/conf

            if (configDir != null) {
                File appHome = configDir.getParentFile(); // Up to root: /opt/CPLS
                if (appHome != null) {
                    File resolvedLogFolder = new File(appHome, "logs");
                    System.out.println("[PROFILER-AGENT] Dynamically resolved log directory: " + resolvedLogFolder.getAbsolutePath());
                    return resolvedLogFolder.getAbsolutePath();
                }
            }
        }

        // Fallback check based on execution context folder name
        String userDir = System.getProperty("user.dir");
        if (userDir != null && (userDir.contains("CPLS") || userDir.contains("cpls"))) {
            File resolvedLogFolder = new File(userDir, "logs");
            return resolvedLogFolder.getAbsolutePath();
        }

    } catch (Exception e) {
        System.err.println("[PROFILER-AGENT] Warning: Failed to parse system properties for logging path.");
    }

    // Local fallback
    return "./logs";
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)