From 441555b7576849c51dd4991587c18244620c9b5d Mon Sep 17 00:00:00 2001 From: Nicolas Adment <39568358+nadment@users.noreply.github.com> Date: Thu, 1 Aug 2024 22:56:28 +0200 Subject: [PATCH] Fix transform WriteToLog NPE #4145 --- .../org/apache/hop/core/logging/LogLevel.java | 38 +++++++------------ .../transforms/writetolog/WriteToLog.java | 38 ++++--------------- .../writetolog/WriteToLogDialog.java | 6 ++- .../transforms/writetolog/WriteToLogMeta.java | 4 +- 4 files changed, 28 insertions(+), 58 deletions(-) diff --git a/core/src/main/java/org/apache/hop/core/logging/LogLevel.java b/core/src/main/java/org/apache/hop/core/logging/LogLevel.java index 507e7b955b5..8f8adae2fd3 100644 --- a/core/src/main/java/org/apache/hop/core/logging/LogLevel.java +++ b/core/src/main/java/org/apache/hop/core/logging/LogLevel.java @@ -22,32 +22,22 @@ import org.apache.hop.metadata.api.IEnumHasCodeAndDescription; public enum LogLevel implements IEnumHasCodeAndDescription { - NOTHING(0, "Nothing"), - ERROR(1, "Error"), - MINIMAL(2, "Minimal"), - BASIC(3, "Basic"), - DETAILED(4, "Detailed"), - DEBUG(5, "Debug"), - ROWLEVEL(6, "Rowlevel"); - - private static final Class PKG = LogLevel.class; - - public static final String[] logLevelDescriptions = { - BaseMessages.getString(PKG, "LogWriter.Level.Nothing.LongDesc"), - BaseMessages.getString(PKG, "LogWriter.Level.Error.LongDesc"), - BaseMessages.getString(PKG, "LogWriter.Level.Minimal.LongDesc"), - BaseMessages.getString(PKG, "LogWriter.Level.Basic.LongDesc"), - BaseMessages.getString(PKG, "LogWriter.Level.Detailed.LongDesc"), - BaseMessages.getString(PKG, "LogWriter.Level.Debug.LongDesc"), - BaseMessages.getString(PKG, "LogWriter.Level.Rowlevel.LongDesc"), - }; + NOTHING(0, "Nothing", "LogWriter.Level.Nothing.LongDesc"), + ERROR(1, "Error", "LogWriter.Level.Error.LongDesc"), + MINIMAL(2, "Minimal", "LogWriter.Level.Minimal.LongDesc"), + BASIC(3, "Basic", "LogWriter.Level.Basic.LongDesc"), + DETAILED(4, "Detailed", "LogWriter.Level.Detailed.LongDesc"), + DEBUG(5, "Debug", "LogWriter.Level.Debug.LongDesc"), + ROWLEVEL(6, "Rowlevel", "LogWriter.Level.Rowlevel.LongDesc"); private final int level; private final String code; + private final String description; - private LogLevel(int level, String code) { + private LogLevel(int level, String code, String description) { this.level = level; this.code = code; + this.description = BaseMessages.getString(LogLevel.class, description); } public int getLevel() { @@ -61,7 +51,7 @@ public String getCode() { @Override public String getDescription() { - return logLevelDescriptions[level]; + return description; } /** @@ -70,8 +60,8 @@ public String getDescription() { * @return the log level or BASIC if nothing matches. */ @Deprecated(since = "2.10") - public static LogLevel getLogLevelForCode(String code) { - return IEnumHasCode.lookupCode(LogLevel.class, code, LogLevel.BASIC); + public static LogLevel getLogLevelForCode(final String code) { + return lookupCode(code); } /** @@ -164,7 +154,7 @@ public static String[] getLogLevelDescriptions() { /** * @return An array of log level codes, sorted by level (0==Nothing, 6=Row Level) */ - public static String[] logLogLevelCodes() { + public static String[] getLogLevelCodes() { return IEnumHasCode.getCodes(LogLevel.class); } } diff --git a/plugins/transforms/writetolog/src/main/java/org/apache/hop/pipeline/transforms/writetolog/WriteToLog.java b/plugins/transforms/writetolog/src/main/java/org/apache/hop/pipeline/transforms/writetolog/WriteToLog.java index 8f87220e885..e5bed4b63da 100644 --- a/plugins/transforms/writetolog/src/main/java/org/apache/hop/pipeline/transforms/writetolog/WriteToLog.java +++ b/plugins/transforms/writetolog/src/main/java/org/apache/hop/pipeline/transforms/writetolog/WriteToLog.java @@ -83,7 +83,7 @@ public boolean processRow() throws HopException { } } data.fieldnr = data.fieldnrs.length; - data.logLevel = meta.getLogLevel(); + data.logLevel = (meta.getLogLevel() == null) ? LogLevel.BASIC : meta.getLogLevel(); data.logMessage = Const.NVL(this.resolve(meta.getLogMessage()), ""); if (!Utils.isEmpty(data.logMessage)) { data.logMessage += Const.CR + Const.CR; @@ -128,35 +128,13 @@ public boolean processRow() throws HopException { /** Output message to log */ private void setLog(final LogLevel loglevel, final StringBuilder msg) { switch (loglevel) { - case ERROR: - // Log level = ERREUR - logError(msg.toString()); - break; - case MINIMAL: - // Log level = MINIMAL - logMinimal(msg.toString()); - break; - case BASIC: - // Log level = BASIC - logBasic(msg.toString()); - break; - case DETAILED: - // Log level = DETAILED - logDetailed(msg.toString()); - break; - case DEBUG: - // Log level = DEBUG - logDebug(msg.toString()); - break; - case ROWLEVEL: - // Log level = ROW LEVEL - logRowlevel(msg.toString()); - break; - case NOTHING: - // Log level = NOTHING - break; - default: - break; + case BASIC -> logBasic(msg.toString()); + case ERROR -> logError(msg.toString()); + case MINIMAL -> logMinimal(msg.toString()); + case DETAILED -> logDetailed(msg.toString()); + case DEBUG -> logDebug(msg.toString()); + case ROWLEVEL -> logRowlevel(msg.toString()); + case NOTHING -> {} } } diff --git a/plugins/transforms/writetolog/src/main/java/org/apache/hop/pipeline/transforms/writetolog/WriteToLogDialog.java b/plugins/transforms/writetolog/src/main/java/org/apache/hop/pipeline/transforms/writetolog/WriteToLogDialog.java index 5ff61e9a2a5..ddd8fbadd0d 100644 --- a/plugins/transforms/writetolog/src/main/java/org/apache/hop/pipeline/transforms/writetolog/WriteToLogDialog.java +++ b/plugins/transforms/writetolog/src/main/java/org/apache/hop/pipeline/transforms/writetolog/WriteToLogDialog.java @@ -328,9 +328,11 @@ public void getData() { wLimitRows.setSelection(input.isLimitRows()); wLimitRowsNumber.setText("" + input.getLimitRowsNumber()); - if (input.getLogLevel() != null) { - wLoglevel.select(input.getLogLevel().getLevel()); + LogLevel logLevel = input.getLogLevel(); + if (logLevel == null) { + logLevel = LogLevel.BASIC; } + wLoglevel.select(logLevel.getLevel()); if (input.getLogMessage() != null) { wLogMessage.setText(input.getLogMessage()); diff --git a/plugins/transforms/writetolog/src/main/java/org/apache/hop/pipeline/transforms/writetolog/WriteToLogMeta.java b/plugins/transforms/writetolog/src/main/java/org/apache/hop/pipeline/transforms/writetolog/WriteToLogMeta.java index faf09a7f53b..97db4d13eec 100644 --- a/plugins/transforms/writetolog/src/main/java/org/apache/hop/pipeline/transforms/writetolog/WriteToLogMeta.java +++ b/plugins/transforms/writetolog/src/main/java/org/apache/hop/pipeline/transforms/writetolog/WriteToLogMeta.java @@ -174,7 +174,7 @@ public void check( if (prev == null || prev.size() == 0) { cr = new CheckResult( - CheckResult.TYPE_RESULT_WARNING, + ICheckResult.TYPE_RESULT_WARNING, BaseMessages.getString(PKG, "WriteToLogMeta.CheckResult.NotReceivingFields"), transformMeta); remarks.add(cr); @@ -209,7 +209,7 @@ public void check( if (logFields.isEmpty()) { cr = new CheckResult( - CheckResult.TYPE_RESULT_WARNING, + ICheckResult.TYPE_RESULT_WARNING, BaseMessages.getString(PKG, "WriteToLogMeta.CheckResult.NoFieldsEntered"), transformMeta);