|
24 | 24 | package org.fao.geonet.util; |
25 | 25 |
|
26 | 26 | import org.apache.commons.lang.StringUtils; |
| 27 | +import org.apache.logging.log4j.LogManager; |
27 | 28 | import org.apache.logging.log4j.ThreadContext; |
| 29 | +import org.apache.logging.log4j.core.appender.routing.RoutingAppender; |
| 30 | +import org.apache.logging.log4j.core.config.Configuration; |
| 31 | +import org.apache.logging.log4j.core.LoggerContext; |
| 32 | +import org.apache.logging.log4j.core.config.Node; |
28 | 33 | import org.fao.geonet.ApplicationContextHolder; |
29 | 34 | import org.fao.geonet.kernel.setting.SettingManager; |
30 | 35 | import org.fao.geonet.kernel.setting.Settings; |
| 36 | +import org.fao.geonet.utils.Log; |
31 | 37 |
|
| 38 | +import java.nio.file.Paths; |
32 | 39 | import java.text.SimpleDateFormat; |
33 | 40 | import java.util.Date; |
34 | 41 | import java.util.TimeZone; |
| 42 | +import java.nio.file.Path; |
35 | 43 |
|
36 | 44 | public class LogUtil { |
37 | 45 |
|
@@ -74,6 +82,46 @@ public static String initializeHarvesterLog(String type, String name) { |
74 | 82 | ThreadContext.put("logfile", logfile); |
75 | 83 | ThreadContext.put("timeZone", timeZoneSetting); |
76 | 84 |
|
77 | | - return logfile; |
| 85 | + try { |
| 86 | + return getHarvesterLogfilePath(); |
| 87 | + } catch (Exception e) { |
| 88 | + Log.error("Error retrieving harvester logfile path. Defaulting to base file name.", e); |
| 89 | + return logfile; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Retrieves the path to the current harvester logfile based on Log4J configuration. |
| 95 | + * |
| 96 | + * @return the path to the harvester logfile |
| 97 | + */ |
| 98 | + public static String getHarvesterLogfilePath() { |
| 99 | + // Get the top-level log directory |
| 100 | + Path logDir = Paths.get(Log.getLogfile().getParent()); |
| 101 | + |
| 102 | + // Access Log4J configuration |
| 103 | + LoggerContext ctx = (LoggerContext) LogManager.getContext(false); |
| 104 | + Configuration config = ctx.getConfiguration(); |
| 105 | + |
| 106 | + // Get the Routing appender |
| 107 | + RoutingAppender routing = config.getAppender("Harvester"); |
| 108 | + if (routing == null) { |
| 109 | + throw new IllegalStateException("Routing appender 'Harvester' not found"); |
| 110 | + } |
| 111 | + |
| 112 | + // Find the first <File> node |
| 113 | + Node fileNode = routing.getRoutes().getRoutes()[0] |
| 114 | + .getNode() |
| 115 | + .getChildren() |
| 116 | + .stream() |
| 117 | + .filter(n -> "File".equalsIgnoreCase(n.getName())) |
| 118 | + .findFirst() |
| 119 | + .orElseThrow(() -> new IllegalStateException("No <File> node found in Harvester routes")); |
| 120 | + |
| 121 | + // Resolve the full path using the StrSubstitutor |
| 122 | + Path fullPath = Paths.get(config.getStrSubstitutor().replace(fileNode.getAttributes().get("fileName"))); |
| 123 | + |
| 124 | + // Return the path relative to the log directory |
| 125 | + return logDir.relativize(fullPath).toString(); |
78 | 126 | } |
79 | 127 | } |
0 commit comments