diff --git a/src/main/java/org/openhab/automation/jrule/internal/JRuleLog.java b/src/main/java/org/openhab/automation/jrule/internal/JRuleLog.java index e099f24e..7c0fc095 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/JRuleLog.java +++ b/src/main/java/org/openhab/automation/jrule/internal/JRuleLog.java @@ -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()); diff --git a/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java b/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java index 4d3911a8..3770fbd3 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java @@ -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: {}"); } } @@ -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); } } } @@ -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 @@ -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 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 classFiles = new HashMap<>(); @@ -272,7 +271,7 @@ public boolean compile(List javaSourceFiles, String classPath) { } fileManager.close(); } catch (Exception x) { - logError("Compiler threw error {}", x.toString()); + logError(x, "Compiler threw error {}"); } return false; @@ -304,7 +303,7 @@ private Optional 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; @@ -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; @@ -349,7 +348,7 @@ public List 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<>(); } } @@ -359,7 +358,7 @@ public List 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<>(); } } @@ -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; } @@ -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); }