import java.util.logging.LogManager;
import java.io.File;
import java.lang.reflect.Field;
import java.util.Properties;
public static String resolveLogDirViaLogger() {
try {
LogManager manager = LogManager.getLogManager();
// Use reflection to access the private 'props' field inside java.util.logging.LogManager
Field propsField = LogManager.class.getDeclaredField("props");
propsField.setAccessible(true);
Properties loggingProperties = (Properties) propsField.get(manager);
if (loggingProperties != null) {
for (String key : loggingProperties.stringPropertyNames()) {
// Look for any configuration key ending with '.pattern'
if (key.endsWith(".pattern")) {
String patternValue = loggingProperties.getProperty(key);
if (patternValue != null && !patternValue.trim().isEmpty()) {
String cleanPattern = patternValue.trim();
// Handle standard JUL tokens if present (%t = temp dir, %h = user home)
cleanPattern = cleanPattern.replace("%h", System.getProperty("user.home"))
.replace("%t", System.getProperty("java.io.tmpdir"));
File logFilePattern = new File(cleanPattern);
File logDir = logFilePattern.getParentFile();
if (logDir != null) {
if (!logDir.exists()) {
logDir.mkdirs();
}
System.out.println("[PROFILER-AGENT] Dynamically caught target log directory: " + logDir.getAbsolutePath());
return logDir.getAbsolutePath();
}
}
}
}
}
} catch (Exception e) {
System.err.println("[PROFILER-AGENT] Warning: Reflection failed to read LogManager properties: " + e.getMessage());
}
// Hard fallback if reflection is blocked or properties aren't loaded yet
System.out.println("[PROFILER-AGENT] Falling back to standard local execution workspace context: ./logs");
return "./logs";
}
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)