Author: chetanm
Date: Tue Jul 18 06:16:16 2017
New Revision: 1802241

URL: http://svn.apache.org/viewvc?rev=1802241&view=rev
Log:
OAK-6409 - Oak-run indexing: improved (user friendly) output

Added:
    
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/LoggingInitializer.java
   (with props)
    jackrabbit/oak/trunk/oak-run/src/main/resources/logback-indexing.xml   
(with props)
Modified:
    
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/IndexCommand.java

Modified: 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/IndexCommand.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/IndexCommand.java?rev=1802241&r1=1802240&r2=1802241&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/IndexCommand.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/IndexCommand.java
 Tue Jul 18 06:16:16 2017
@@ -74,6 +74,7 @@ public class IndexCommand implements Com
         //Clean up before setting up NodeStore as the temp
         //directory might be used by NodeStore for cache stuff like 
persistentCache
         setupDirectories(indexOpts);
+        setupLogging(indexOpts);
 
         if (indexOpts.isReindex() && opts.getCommonOpts().isReadWrite()) {
             performReindexInReadWriteMode(indexOpts);
@@ -263,11 +264,14 @@ public class IndexCommand implements Com
         //TODO Do not clean if restarting
         String[] dirListing = workDir.list();
         if (dirListing != null && dirListing.length != 0) {
-            log.info("Cleaning existing work directory {}", 
workDir.getAbsolutePath());
             FileUtils.cleanDirectory(workDir);
         }
     }
 
+    private static void setupLogging(IndexOptions indexOpts) throws 
IOException {
+        new LoggingInitializer(indexOpts.getWorkDir()).init();
+    }
+
     private static String now() {
         return ISO8601.format(Calendar.getInstance());
     }

Added: 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/LoggingInitializer.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/LoggingInitializer.java?rev=1802241&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/LoggingInitializer.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/LoggingInitializer.java
 Tue Jul 18 06:16:16 2017
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.index;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.classic.util.ContextInitializer;
+import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.util.StatusPrinter;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.FilenameUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Configures the logging based on logback-indexing.xml. This file
+ * would be copied to work directory and then logging would be
+ * configured based on that
+ *
+ * The log file is configured for auto scan so any change made while
+ * oak-run is in progress would be picked up
+ */
+public class LoggingInitializer {
+    private static final String LOGBACK_INDEX_XML = "logback-indexing.xml";
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final File workDir;
+
+    public LoggingInitializer(File workDir) {
+        this.workDir = workDir;
+    }
+
+    public void init() throws IOException {
+        //If custom config file defined then disable the default logic
+        if (System.getProperty(ContextInitializer.CONFIG_FILE_PROPERTY) != 
null) {
+            return;
+        }
+
+        File config = copyDefaultConfig();
+        configureLogback(config);
+        log.info("Logging configured from {}", config.getAbsolutePath());
+        log.info("Any change in logging config would be picked up");
+        log.info("Logs would be written to {}", new File(workDir, 
"indexing.log"));
+    }
+
+    private void configureLogback(File config) {
+        LoggerContext context = (LoggerContext) 
LoggerFactory.getILoggerFactory();
+
+        try {
+            JoranConfigurator configurator = new JoranConfigurator();
+            configurator.setContext(context);
+            System.setProperty("oak.workDir", 
FilenameUtils.normalizeNoEndSeparator(workDir.getAbsolutePath()));
+            // Call context.reset() to clear any previous configuration, e.g. 
default
+            // configuration. For multi-step configuration, omit calling 
context.reset().
+            context.reset();
+            configurator.doConfigure(config);
+        } catch (JoranException je) {
+            // StatusPrinter will handle this
+        }
+        StatusPrinter.printInCaseOfErrorsOrWarnings(context);
+    }
+
+    private File copyDefaultConfig() throws IOException {
+        URL url = getClass().getResource("/" + LOGBACK_INDEX_XML);
+        File dest = new File(workDir, LOGBACK_INDEX_XML);
+        try (InputStream is = url.openStream()) {
+            FileUtils.copyInputStreamToFile(is, dest);
+        }
+        return dest;
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/LoggingInitializer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-run/src/main/resources/logback-indexing.xml
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/resources/logback-indexing.xml?rev=1802241&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-run/src/main/resources/logback-indexing.xml (added)
+++ jackrabbit/oak/trunk/oak-run/src/main/resources/logback-indexing.xml Tue 
Jul 18 06:16:16 2017
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  -->
+<configuration scan="true" scanPeriod="1 second">
+
+  <appender name="index" class="ch.qos.logback.core.FileAppender">
+    <file>${oak.workDir}/indexing.log</file>
+    <encoder>
+      <pattern>%d %-5level [%thread] %logger{30} %marker- %msg %n</pattern>
+    </encoder>
+  </appender>
+
+  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+    <target>System.out</target>
+    <encoder>
+      <pattern>%d{HH:mm:ss} - %msg%n</pattern>
+    </encoder>
+  </appender>
+
+  <!-- For index tooling -->
+  <logger name="org.apache.jackrabbit.oak.index" level="INFO">
+    <appender-ref ref="STDOUT" />
+  </logger>
+  <logger name="org.apache.jackrabbit.oak.plugins.index.importer" level="INFO">
+    <appender-ref ref="STDOUT" />
+  </logger>
+  <logger name="org.apache.jackrabbit.oak.plugins.index.IndexUpdate" 
level="INFO">
+    <appender-ref ref="STDOUT" />
+  </logger>
+  <logger name="org.apache.jackrabbit.oak.plugins.index.progress" level="INFO">
+    <appender-ref ref="STDOUT" />
+  </logger>
+
+  <root level="INFO">
+    <appender-ref ref="index" />
+  </root>
+
+</configuration>

Propchange: jackrabbit/oak/trunk/oak-run/src/main/resources/logback-indexing.xml
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to