Skip to content

Commit

Permalink
Fixed error logging of throwable
Browse files Browse the repository at this point in the history
  • Loading branch information
seaside1 committed Oct 7, 2023
1 parent 51e3403 commit 1c5bcb0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/openhab/automation/jrule/internal/JRuleLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public static void warn(Logger logger, String logPrefix, String message, Object.
logger.warn(PREFIX_WARN_LOG, logPrefix, logMessage.getMessage());
}

public static void error(Logger logger, String logPrefix, Throwable t, String message, Object... parameters) {
final FormattingTuple logMessage = MessageFormatter.arrayFormat(message, parameters);
final FormattingTuple finalLogmessage = MessageFormatter.format(PREFIX_ERROR_LOG,
new String[] { logPrefix, logMessage.getMessage() });
error(logger, t, finalLogmessage.getMessage());
}

private static void error(Logger logger, Throwable t, String message) {
logger.error(message, t);
}

public static void error(Logger logger, String logPrefix, String message, Object... parameters) {
final FormattingTuple logMessage = MessageFormatter.arrayFormat(message, parameters);
logger.error(PREFIX_ERROR_LOG, logPrefix, logMessage.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ public void loadClassesFromJar(ClassLoader classLoader, File sourceFolder, Strin
loadClass(classLoader, relativePathToFullClassname(jarEntryName), createInstance);
}
} catch (IllegalArgumentException | SecurityException | IOException e) {
logError("Error loading classes from jarfile {} due to {}", jarItem.getAbsolutePath(), e);
logError(e, "Error loading classes from jarfile {}", jarItem.getAbsolutePath());
}
// Best effort
});
} catch (Exception e) {
logError("Error loading classes from jarfile: {}", e);
logError(e, "Error loading classes from jarfile: {}");
}
}

Expand Down Expand Up @@ -151,8 +151,7 @@ public void loadClass(ClassLoader classLoader, String className, boolean createI
final Object obj = loadedClass.getDeclaredConstructor().newInstance();
logDebug("Created instance: {} obj: {}", className, obj);
} catch (Exception x) {
logError("Could not create create instance using default constructor: {}", className);
logger.error("Failed to load class", x);
logError(x, "Could not create create instance using default constructor: {}", className);
}
}
}
Expand All @@ -172,7 +171,7 @@ public void loadClassesFromFolder(ClassLoader classLoader, File rootFolder, Stri
.map(this::relativePathToFullClassname).filter(e -> e.startsWith(onlyInRootPackage))
.collect(Collectors.toList());
} catch (IOException e) {
logError("Error loading classes in {} due to {}", rootFolder.getAbsolutePath(), e);
logError(e, "Error loading classes in {} due to {}", rootFolder.getAbsolutePath());
}

// classFiles is now in the form "packageRoot.subPackage.classname", filtered by prefix in onlyInRootPackage
Expand Down Expand Up @@ -205,14 +204,14 @@ private boolean compileGeneratedSource(File sourceFolder) {
.filter(f -> f.getFileName().toString().endsWith(JRuleConstants.JAVA_FILE_TYPE)).map(Path::toFile)
.collect(Collectors.toList());
} catch (IOException e) {
logError("Error listing source files in folder: {}", sourceFolder.getAbsolutePath(), e);
logError(e, "Error listing source files in folder: {}", sourceFolder.getAbsolutePath());
}
try (Stream<Path> paths = Files.walk(Paths.get(sourceFolder.toURI()))) {
javaClassFiles = paths.filter(Files::isRegularFile) // is a file
.filter(f -> f.getFileName().toString().endsWith(JRuleConstants.CLASS_FILE_TYPE)).map(Path::toFile)
.collect(Collectors.toList());
} catch (IOException e) {
logError("Error listing class files in folder: {}", sourceFolder.getAbsolutePath(), e);
logError(e, "Error listing class files in folder: {}", sourceFolder.getAbsolutePath());
}

Map<String, File> classFiles = new HashMap<>();
Expand Down Expand Up @@ -272,7 +271,7 @@ public boolean compile(List<File> javaSourceFiles, String classPath) {
}
fileManager.close();
} catch (Exception x) {
logError("Compiler threw error {}", x.toString());
logError(x, "Compiler threw error {}");
}

return false;
Expand Down Expand Up @@ -304,7 +303,7 @@ private Optional<String> getOpenHABJar(String jarPrefix) {
.filter(path -> path.getFileName().toString().startsWith(jarPrefix))
.map(path -> path.toFile().getAbsolutePath()).findFirst();
} catch (IOException e) {
logError(e.getMessage());
logError(e, "Failed to get CoreJar");
return Optional.empty();
}
return coreJarPath;
Expand Down Expand Up @@ -338,7 +337,7 @@ public boolean compileRules() {
logWarn("Found no java rules to compile and use in folder {}", jRuleConfig.getRulesDirectory());
}
} catch (IOException e) {
logError("Error listing java files in folder: {}", jRuleConfig.getRulesDirectory(), e);
logError(e, "Error listing java files in folder: {}", jRuleConfig.getRulesDirectory());

}
return false;
Expand All @@ -349,7 +348,7 @@ public List<URL> getExtLibsAsUrls() {
final File[] extLibsFiles = getExtLibsAsFiles();
return Arrays.stream(extLibsFiles).map(this::getUrl).collect(Collectors.toList());
} catch (Exception x) {
logError("Failed to get extLib urls");
logError(x, "Failed to get extLib urls");
return new ArrayList<>();
}
}
Expand All @@ -359,7 +358,7 @@ public List<URL> getJarRulesAsUrls() {
final File[] jarRulesFiles = getJarRulesAsFiles();
return Arrays.stream(jarRulesFiles).map(this::getUrl).collect(Collectors.toList());
} catch (Exception x) {
logError("Failed to get jar-rules urls");
logError(x, "Failed to get jar-rules urls");
return new ArrayList<>();
}
}
Expand All @@ -368,7 +367,7 @@ private URL getUrl(File f) {
try {
return f.toURI().toURL();
} catch (MalformedURLException e) {
logError("Failed to convert to URL: {}", f.getAbsolutePath(), e);
logError(e, "Failed to convert to URL: {}", f.getAbsolutePath());
}
return null;
}
Expand Down Expand Up @@ -428,6 +427,10 @@ private void logError(String message, Object... parameters) {
JRuleLog.error(logger, LOG_NAME_COMPILER, message, parameters);
}

private void logError(Throwable t, String message, Object... parameters) {
JRuleLog.error(logger, LOG_NAME_COMPILER, t, message);
}

private void logWarn(String message, Object... parameters) {
JRuleLog.warn(logger, LOG_NAME_COMPILER, message, parameters);
}
Expand Down

0 comments on commit 1c5bcb0

Please sign in to comment.