DEV Community

Query Filter
Query Filter

Posted on

bridhe-8

import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.Handler;
import java.util.logging.FileHandler;

public static String resolveLogDirViaLogger() {
    try {
        // 1. Get the global LogManager and find handlers assigned to the root or global scope
        Logger rootLogger = LogManager.getLogManager().getLogger("");

        if (rootLogger != null) {
            for (Handler handler : rootLogger.getHandlers()) {
                // 2. Look for the file handler responsible for writing logs to disk
                if (handler instanceof FileHandler) {
                    // This can return a pattern like "/opt/CPLS/logs/qff_bridge.%g.log" 
                    // or a tokenized string containing custom properties
                    String pattern = System.getProperty("java.util.logging.FileHandler.pattern");

                    if (pattern == null || pattern.trim().isEmpty()) {
                        // Safe fallback fallback inside the handler settings
                        pattern = LogManager.getLogManager().getProperty("java.util.logging.FileHandler.pattern");
                    }

                    if (pattern != null && !pattern.trim().isEmpty()) {
                        File logFilePattern = new File(pattern.trim());
                        File logDir = logFilePattern.getParentFile();

                        if (logDir != null) {
                            System.out.println("[PROFILER-AGENT] Successfully grabbed active logging output path: " + logDir.getAbsolutePath());
                            return logDir.getAbsolutePath();
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        System.err.println("[PROFILER-AGENT] Warning: Failed to query active logging handlers.");
    }

    // 3. True local fallback if handlers haven't initialized or are console-only locally
    System.out.println("[PROFILER-AGENT] Falling back to standard local execution workspace context: ./logs");
    return "./logs";
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)