You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hop.apache.org by ha...@apache.org on 2023/03/29 10:45:03 UTC

[hop] branch master updated: fix #2712 Add a logfile flag to hop-run

This is an automated email from the ASF dual-hosted git repository.

hansva pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hop.git


The following commit(s) were added to refs/heads/master by this push:
     new adf8b8e48a fix #2712 Add a logfile flag to hop-run
     new c70fb4fd66 Merge pull request #2773 from sramazzina/2712
adf8b8e48a is described below

commit adf8b8e48a42ba2fddc470e37f44e90f42c26799
Author: sramazzina <se...@serasoft.it>
AuthorDate: Thu Mar 23 23:58:13 2023 +0100

    fix #2712 Add a logfile flag to hop-run
---
 .../modules/ROOT/pages/hop-run/index.adoc          |  5 +++
 .../src/main/java/org/apache/hop/run/HopRun.java   | 46 ++++++++++++++++------
 2 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/docs/hop-user-manual/modules/ROOT/pages/hop-run/index.adoc b/docs/hop-user-manual/modules/ROOT/pages/hop-run/index.adoc
index 05cfdd2cc2..68cc493827 100644
--- a/docs/hop-user-manual/modules/ROOT/pages/hop-run/index.adoc
+++ b/docs/hop-user-manual/modules/ROOT/pages/hop-run/index.adoc
@@ -55,6 +55,7 @@ Usage: <main class> [-ho] [-e=<environmentOption>] [-f=<filename>]
   -j, --project=<projectOption>
                           The name of the project to use
   -l, --level=<level>     The debug level, one of NOTHING, ERROR, MINIMAL, BASIC, DETAILED, DEBUG, ROWLEVEL
+      --logfile=<logFile>   Write Hop console log to a file
   -m, --metadata-export=<metadataExportFile>
                           A file containing exported metadata in JSON format
   -o, --printoptions      Print the used options
@@ -94,6 +95,10 @@ Check the xref:projects/projects-environments.adoc[documentation on environments
 |```--level```
 |The debug level, one of NOTHING, ERROR, MINIMAL, BASIC, DETAILED, DEBUG, ROWLEVEL
 
+|```-lf```
+|```--logfile```
+|Write Hop console log to a file specified by the user
+
 |```-m```
 |```--metadata-export```
 |A file containing exported metadata in JSON format.  See also the metadata export option in xref:hop-tools/hop-conf/hop-conf.adoc[Hop Conf]
diff --git a/engine/src/main/java/org/apache/hop/run/HopRun.java b/engine/src/main/java/org/apache/hop/run/HopRun.java
index dc9161c5dc..bcf43d1f8c 100644
--- a/engine/src/main/java/org/apache/hop/run/HopRun.java
+++ b/engine/src/main/java/org/apache/hop/run/HopRun.java
@@ -29,11 +29,7 @@ import org.apache.hop.core.config.plugin.IConfigOptions;
 import org.apache.hop.core.exception.HopException;
 import org.apache.hop.core.extension.ExtensionPointHandler;
 import org.apache.hop.core.extension.HopExtensionPoint;
-import org.apache.hop.core.logging.HopLogStore;
-import org.apache.hop.core.logging.ILogChannel;
-import org.apache.hop.core.logging.LogChannel;
-import org.apache.hop.core.logging.LogLevel;
-import org.apache.hop.core.logging.LoggingObject;
+import org.apache.hop.core.logging.*;
 import org.apache.hop.core.metadata.SerializableMetadataProvider;
 import org.apache.hop.core.parameters.INamedParameterDefinitions;
 import org.apache.hop.core.parameters.INamedParameters;
@@ -41,6 +37,7 @@ import org.apache.hop.core.parameters.UnknownParamException;
 import org.apache.hop.core.plugins.IPlugin;
 import org.apache.hop.core.plugins.JarCache;
 import org.apache.hop.core.plugins.PluginRegistry;
+import org.apache.hop.core.util.Utils;
 import org.apache.hop.core.variables.IVariables;
 import org.apache.hop.core.variables.Variables;
 import org.apache.hop.core.vfs.HopVfs;
@@ -72,7 +69,7 @@ import java.util.Map;
 
 @Command(versionProvider = HopVersionProvider.class)
 public class HopRun implements Runnable, IHasHopMetadataProvider {
-  
+
   @Option(
       names = {"-f", "--file"},
       description = "The filename of the workflow or pipeline to run")
@@ -80,7 +77,8 @@ public class HopRun implements Runnable, IHasHopMetadataProvider {
 
   @Option(
       names = {"-l", "--level"},
-      description = "The debug level, one of NOTHING, ERROR, MINIMAL, BASIC, DETAILED, DEBUG, ROWLEVEL")
+      description =
+          "The debug level, one of NOTHING, ERROR, MINIMAL, BASIC, DETAILED, DEBUG, ROWLEVEL")
   private String level;
 
   @Option(
@@ -89,11 +87,17 @@ public class HopRun implements Runnable, IHasHopMetadataProvider {
       description = "Displays this help message and quits.")
   private boolean helpRequested;
 
-  @Option(names = {"-v", "--version"},
+  @Option(
+      names = {"-v", "--version"},
       versionHelp = true,
       description = "Print version information and exit")
   private boolean versionRequested;
-  
+
+  @Option(
+      names = {"-lf", "--logfile"},
+      description = "Write Hop console log to a file")
+  private String logFile;
+
   @Option(
       names = {"-p", "--parameters"},
       description = "A comma separated list of PARAMETER=VALUE pairs",
@@ -136,6 +140,7 @@ public class HopRun implements Runnable, IHasHopMetadataProvider {
   @Override
   public void run() {
     validateOptions();
+    FileLoggingEventListener fileLoggingEventListener = null;
 
     try {
       log = new LogChannel("HopRun");
@@ -174,6 +179,11 @@ public class HopRun implements Runnable, IHasHopMetadataProvider {
         }
       }
 
+      if (!Utils.isEmpty(logFile)) {
+        fileLoggingEventListener = new FileLoggingEventListener(logFile, false);
+        HopLogStore.getAppender().addLoggingEventListener(fileLoggingEventListener);
+      }
+
       if (isPipeline()) {
         runPipeline(cmd, log);
       }
@@ -186,6 +196,10 @@ public class HopRun implements Runnable, IHasHopMetadataProvider {
     } catch (Exception e) {
       throw new ExecutionException(
           cmd, "There was an error during execution of file '" + filename + "'", e);
+    } finally {
+      if (fileLoggingEventListener != null) {
+        HopLogStore.getAppender().removeLoggingEventListener(fileLoggingEventListener);
+      }
     }
   }
 
@@ -244,7 +258,7 @@ public class HopRun implements Runnable, IHasHopMetadataProvider {
       //
       if (StringUtils.isEmpty(configuration.getRunConfiguration())) {
         PipelineRunConfiguration defaultRunConfiguration =
-                PipelineRunConfiguration.findDefault(metadataProvider);
+            PipelineRunConfiguration.findDefault(metadataProvider);
         if (defaultRunConfiguration != null) {
           configuration.setRunConfiguration(defaultRunConfiguration.getName());
         }
@@ -331,7 +345,7 @@ public class HopRun implements Runnable, IHasHopMetadataProvider {
       //
       if (StringUtils.isEmpty(configuration.getRunConfiguration())) {
         WorkflowRunConfiguration defaultRunConfiguration =
-                WorkflowRunConfiguration.findDefault(metadataProvider);
+            WorkflowRunConfiguration.findDefault(metadataProvider);
         if (defaultRunConfiguration != null) {
           configuration.setRunConfiguration(defaultRunConfiguration.getName());
         }
@@ -366,7 +380,7 @@ public class HopRun implements Runnable, IHasHopMetadataProvider {
       WorkflowMeta workflowMeta) {
     try {
       String runConfigurationName = variables.resolve(configuration.getRunConfiguration());
-      //Create a logging object to push down the correct loglevel to the Workflow
+      // Create a logging object to push down the correct loglevel to the Workflow
       //
       LoggingObject workflowLog = new LoggingObject(log);
       workflowLog.setLogLevel(configuration.getLogLevel());
@@ -765,6 +779,14 @@ public class HopRun implements Runnable, IHasHopMetadataProvider {
     this.log = log;
   }
 
+  public String getLogFile() {
+    return logFile;
+  }
+
+  public void setLogFile(String logFile) {
+    this.logFile = logFile;
+  }
+
   /**
    * @param metadataProvider The metadataProvider to set
    */