You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by gr...@apache.org on 2022/11/20 03:13:37 UTC

[tika] branch logging-refactoring updated (83d5e6508 -> 34687cdb0)

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

grossws pushed a change to branch logging-refactoring
in repository https://gitbox.apache.org/repos/asf/tika.git


 discard 83d5e6508 Migrate tika-parsers-standard-modules to tika-logging
 discard f66394ab4 Migrate tika-app to tika-logging
 discard 414b96338 Implement tika-logging-*
     new 9fcc90a00 fixup! Replace commons-logging with jcl-over-slf4j
     new 6b95dd2f3 Implement tika-logging-*
     new 12ca313d0 Migrate tika-app to tika-logging
     new 34687cdb0 Migrate tika-parsers-standard-modules to tika-logging

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (83d5e6508)
            \
             N -- N -- N   refs/heads/logging-refactoring (34687cdb0)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 tika-parsers/tika-parsers-ml/tika-parser-nlp-module/pom.xml | 11 +++++++++++
 1 file changed, 11 insertions(+)


[tika] 03/04: Migrate tika-app to tika-logging

Posted by gr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

grossws pushed a commit to branch logging-refactoring
in repository https://gitbox.apache.org/repos/asf/tika.git

commit 12ca313d0e59b042f1cc4b85225b77a7c8c816e5
Author: Konstantin Gribov <gr...@gmail.com>
AuthorDate: Sat Nov 19 22:25:20 2022 +0300

    Migrate tika-app to tika-logging
---
 tika-app/pom.xml                                        | 17 +++++------------
 tika-app/src/main/java/org/apache/tika/cli/TikaCLI.java |  4 ++--
 2 files changed, 7 insertions(+), 14 deletions(-)

diff --git a/tika-app/pom.xml b/tika-app/pom.xml
index e9d624236..2cb85d382 100644
--- a/tika-app/pom.xml
+++ b/tika-app/pom.xml
@@ -74,20 +74,13 @@
 
     <!-- logging -->
     <dependency>
-      <groupId>org.apache.logging.log4j</groupId>
-      <artifactId>log4j-core</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.logging.log4j</groupId>
-      <artifactId>log4j-slf4j2-impl</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>jcl-over-slf4j</artifactId>
+      <groupId>org.apache.tika</groupId>
+      <artifactId>tika-logging-common</artifactId>
     </dependency>
     <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>log4j-over-slf4j</artifactId>
+      <groupId>org.apache.tika</groupId>
+      <artifactId>tika-logging-log4j2-impl</artifactId>
+      <scope>runtime</scope>
     </dependency>
   </dependencies>
 
diff --git a/tika-app/src/main/java/org/apache/tika/cli/TikaCLI.java b/tika-app/src/main/java/org/apache/tika/cli/TikaCLI.java
index ee7feacfc..ec331fe39 100644
--- a/tika-app/src/main/java/org/apache/tika/cli/TikaCLI.java
+++ b/tika-app/src/main/java/org/apache/tika/cli/TikaCLI.java
@@ -58,7 +58,6 @@ import javax.xml.transform.stream.StreamResult;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.input.CloseShieldInputStream;
-import org.apache.logging.log4j.Level;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.xml.sax.ContentHandler;
@@ -80,6 +79,7 @@ import org.apache.tika.fork.ForkParser;
 import org.apache.tika.gui.TikaGUI;
 import org.apache.tika.io.TikaInputStream;
 import org.apache.tika.language.detect.LanguageHandler;
+import org.apache.tika.logging.api.LoggingConfigurator;
 import org.apache.tika.metadata.Metadata;
 import org.apache.tika.metadata.TikaCoreProperties;
 import org.apache.tika.metadata.serialization.JsonMetadata;
@@ -361,7 +361,7 @@ public class TikaCLI {
             pipeMode = false;
             version();
         } else if (arg.equals("-v") || arg.equals("--verbose")) {
-            org.apache.logging.log4j.core.config.Configurator.setRootLevel(Level.DEBUG);
+            LoggingConfigurator.setRootLoggerLevel("DEBUG");
         } else if (arg.equals("-g") || arg.equals("--gui")) {
             pipeMode = false;
             if (configFilePath != null) {


[tika] 01/04: fixup! Replace commons-logging with jcl-over-slf4j

Posted by gr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

grossws pushed a commit to branch logging-refactoring
in repository https://gitbox.apache.org/repos/asf/tika.git

commit 9fcc90a00ba22edf084712c5e5500b18a1b6cb24
Author: Konstantin Gribov <gr...@gmail.com>
AuthorDate: Sun Nov 20 05:55:25 2022 +0300

    fixup! Replace commons-logging with jcl-over-slf4j
---
 tika-parsers/tika-parsers-ml/tika-parser-nlp-module/pom.xml | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/pom.xml b/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/pom.xml
index 00154510f..0c1785e5c 100644
--- a/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/pom.xml
+++ b/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/pom.xml
@@ -84,6 +84,12 @@
     <dependency>
       <groupId>org.apache.httpcomponents</groupId>
       <artifactId>httpclient</artifactId>
+      <exclusions>
+        <exclusion>
+          <groupId>commons-logging</groupId>
+          <artifactId>commons-logging</artifactId>
+        </exclusion>
+      </exclusions>
     </dependency>
     <dependency>
       <groupId>org.apache.httpcomponents</groupId>
@@ -142,6 +148,11 @@
       </exclusions>
     </dependency>
 
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>jcl-over-slf4j</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>log4j-over-slf4j</artifactId>


[tika] 02/04: Implement tika-logging-*

Posted by gr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

grossws pushed a commit to branch logging-refactoring
in repository https://gitbox.apache.org/repos/asf/tika.git

commit 6b95dd2f36c1c5c49732e20f33049ad1a56c251a
Author: Konstantin Gribov <gr...@gmail.com>
AuthorDate: Thu Nov 10 03:54:54 2022 +0300

    Implement tika-logging-*
    
    * `tika-logging-common` provides simple API and SPI to configure underlying logging backend
    * `tika-logging-slf4j-bridges` brings slf4j bridges for java.util.logging, Apache Commons Logging (JCL) and Apache Log4j 1.2
    * `tika-logging-log4j2-impl` implements SPI to configure Apache Log4j 2.x and brings slf4j-api 2.x
---
 pom.xml                                            |  1 +
 tika-bom/pom.xml                                   | 17 +++++
 tika-logging/pom.xml                               | 42 ++++++++++++
 tika-logging/tika-logging-common/pom.xml           | 42 ++++++++++++
 .../tika/logging/api/LoggerLevelChangeContext.java | 32 +++++++++
 .../tika/logging/api/LoggingConfigurator.java      | 66 +++++++++++++++++++
 .../logging/spi/LoggingConfiguratorProvider.java   | 76 ++++++++++++++++++++++
 tika-logging/tika-logging-log4j2-impl/pom.xml      | 52 +++++++++++++++
 .../logging/log4j2/Log4j2LoggingConfigurator.java  | 38 +++++++++++
 ...he.tika.logging.spi.LoggingConfiguratorProvider |  1 +
 .../log4j2/Log4j2LoggingConfiguratorTest.java      | 73 +++++++++++++++++++++
 tika-logging/tika-logging-slf4j-bridges/pom.xml    | 50 ++++++++++++++
 tika-parent/pom.xml                                | 10 +++
 13 files changed, 500 insertions(+)

diff --git a/pom.xml b/pom.xml
index f0d0e3627..9637ddb02 100644
--- a/pom.xml
+++ b/pom.xml
@@ -58,6 +58,7 @@
     <module>tika-example</module>
     <module>tika-java7</module>
     <module>tika-detectors</module>
+    <module>tika-logging</module>
   </modules>
 
   <properties>
diff --git a/tika-bom/pom.xml b/tika-bom/pom.xml
index 95f378b69..5c3b87be3 100644
--- a/tika-bom/pom.xml
+++ b/tika-bom/pom.xml
@@ -424,6 +424,23 @@
         <artifactId>tika-httpclient-commons</artifactId>
         <version>2.6.1-SNAPSHOT</version>
       </dependency>
+
+      <!-- Tika logging modules -->
+      <dependency>
+        <groupId>org.apache.tika</groupId>
+        <artifactId>tika-logging-common</artifactId>
+        <version>2.6.1-SNAPSHOT</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.tika</groupId>
+        <artifactId>tika-logging-slf4j-bridges</artifactId>
+        <version>2.6.1-SNAPSHOT</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.tika</groupId>
+        <artifactId>tika-logging-log4j2-impl</artifactId>
+        <version>2.6.1-SNAPSHOT</version>
+      </dependency>
     </dependencies>
   </dependencyManagement>
 </project>
diff --git a/tika-logging/pom.xml b/tika-logging/pom.xml
new file mode 100644
index 000000000..16ce07719
--- /dev/null
+++ b/tika-logging/pom.xml
@@ -0,0 +1,42 @@
+<?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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.tika</groupId>
+    <artifactId>tika</artifactId>
+    <version>2.6.1-SNAPSHOT</version>
+    <relativePath>../pom.xml</relativePath>
+  </parent>
+
+  <artifactId>tika-logging</artifactId>
+  <packaging>pom</packaging>
+
+  <name>Apache Tika logging modules</name>
+
+  <modules>
+    <module>tika-logging-common</module>
+    <module>tika-logging-slf4j-bridges</module>
+    <module>tika-logging-log4j2-impl</module>
+  </modules>
+</project>
diff --git a/tika-logging/tika-logging-common/pom.xml b/tika-logging/tika-logging-common/pom.xml
new file mode 100644
index 000000000..dce030798
--- /dev/null
+++ b/tika-logging/tika-logging-common/pom.xml
@@ -0,0 +1,42 @@
+<?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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.tika</groupId>
+    <artifactId>tika-logging</artifactId>
+    <version>2.6.1-SNAPSHOT</version>
+    <relativePath>../pom.xml</relativePath>
+  </parent>
+
+  <artifactId>tika-logging-common</artifactId>
+
+  <name>Apache Tika logging API and SPI</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.tika</groupId>
+      <artifactId>tika-core</artifactId>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/tika-logging/tika-logging-common/src/main/java/org/apache/tika/logging/api/LoggerLevelChangeContext.java b/tika-logging/tika-logging-common/src/main/java/org/apache/tika/logging/api/LoggerLevelChangeContext.java
new file mode 100644
index 000000000..717a99c60
--- /dev/null
+++ b/tika-logging/tika-logging-common/src/main/java/org/apache/tika/logging/api/LoggerLevelChangeContext.java
@@ -0,0 +1,32 @@
+/*
+ * 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.tika.logging.api;
+
+public class LoggerLevelChangeContext implements AutoCloseable {
+  private final String loggerName;
+  private final String originalLevel;
+
+  public LoggerLevelChangeContext(String loggerName, String currentLevel) {
+    this.loggerName = loggerName;
+    this.originalLevel = LoggingConfigurator.setLoggerLevel(loggerName, currentLevel);
+  }
+
+  @Override
+  public void close() throws Exception {
+    LoggingConfigurator.setLoggerLevel(loggerName, originalLevel);
+  }
+}
diff --git a/tika-logging/tika-logging-common/src/main/java/org/apache/tika/logging/api/LoggingConfigurator.java b/tika-logging/tika-logging-common/src/main/java/org/apache/tika/logging/api/LoggingConfigurator.java
new file mode 100644
index 000000000..1b12b25a4
--- /dev/null
+++ b/tika-logging/tika-logging-common/src/main/java/org/apache/tika/logging/api/LoggingConfigurator.java
@@ -0,0 +1,66 @@
+/*
+ * 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.tika.logging.api;
+
+import org.apache.tika.logging.spi.LoggingConfiguratorProvider;
+
+public class LoggingConfigurator {
+  private static final LoggingConfiguratorProvider INSTANCE = LoggingConfiguratorProvider.Holder.INSTANCE;
+
+  private static final String ROOT_LOGGER_NAME = INSTANCE.getRootLoggerName();
+
+  private LoggingConfigurator() {
+  }
+
+  public static String getLoggerLevel(String loggerName) {
+    return INSTANCE.getLoggerLevel(loggerName);
+  }
+
+  public static String getLoggerLevel(Class<?> clazz) {
+    return getLoggerLevel(clazz.getCanonicalName());
+  }
+
+  public static String getRootLoggerLevel() {
+    return getLoggerLevel(ROOT_LOGGER_NAME);
+  }
+
+  public static String setLoggerLevel(String loggerName, String level) {
+    String originalLevel = INSTANCE.getLoggerLevel(loggerName);
+    INSTANCE.setLoggerLevel(loggerName, level);
+    return originalLevel;
+  }
+
+  public static String setLoggerLevel(Class<?> clazz, String level) {
+    return setLoggerLevel(clazz.getCanonicalName(), level);
+  }
+
+  public static String setRootLoggerLevel(String level) {
+    return setLoggerLevel(ROOT_LOGGER_NAME, level);
+  }
+
+  public static LoggerLevelChangeContext withLoggerLevel(String loggerName, String level) {
+    return new LoggerLevelChangeContext(loggerName, level);
+  }
+
+  public static LoggerLevelChangeContext withLoggerLevel(Class<?> clazz, String level) {
+    return new LoggerLevelChangeContext(clazz.getCanonicalName(), level);
+  }
+
+  public static LoggerLevelChangeContext withRootLoggerLevel(String level) {
+    return new LoggerLevelChangeContext(ROOT_LOGGER_NAME, level);
+  }
+}
diff --git a/tika-logging/tika-logging-common/src/main/java/org/apache/tika/logging/spi/LoggingConfiguratorProvider.java b/tika-logging/tika-logging-common/src/main/java/org/apache/tika/logging/spi/LoggingConfiguratorProvider.java
new file mode 100644
index 000000000..ec55a31e4
--- /dev/null
+++ b/tika-logging/tika-logging-common/src/main/java/org/apache/tika/logging/spi/LoggingConfiguratorProvider.java
@@ -0,0 +1,76 @@
+/*
+ * 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.tika.logging.spi;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.apache.tika.config.ServiceLoader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public interface LoggingConfiguratorProvider {
+  String getRootLoggerName();
+
+  String getLoggerLevel(String loggerName);
+
+  void setLoggerLevel(String loggerName, String level);
+
+  class Holder {
+    private static final Logger LOG = LoggerFactory.getLogger(Holder.class);
+
+    private static final ServiceLoader LOADER = new ServiceLoader();
+
+    public static final LoggingConfiguratorProvider INSTANCE;
+
+    static {
+      List<LoggingConfiguratorProvider> configurators = LOADER.loadStaticServiceProviders(LoggingConfiguratorProvider.class);
+      if (configurators.isEmpty()) {
+        INSTANCE = NoOpLoggingConfiguratorProvider.INSTANCE;
+      } else {
+        INSTANCE = configurators.get(0);
+      }
+
+      if (configurators.size() > 1) {
+        LOG.warn("Found several {} instances: {}",
+            LoggingConfiguratorProvider.class.getCanonicalName(),
+            configurators.stream().map(c -> c.getClass().getCanonicalName()).collect(Collectors.joining(", ")));
+      }
+    }
+  }
+
+  class NoOpLoggingConfiguratorProvider implements LoggingConfiguratorProvider {
+    public static final LoggingConfiguratorProvider INSTANCE = new NoOpLoggingConfiguratorProvider();
+
+    private NoOpLoggingConfiguratorProvider() {
+    }
+
+    @Override
+    public String getRootLoggerName() {
+      return "";
+    }
+
+    @Override
+    public String getLoggerLevel(String loggerName) {
+      return "INFO";
+    }
+
+    @Override
+    public void setLoggerLevel(String loggerName, String level) {
+    }
+  }
+}
diff --git a/tika-logging/tika-logging-log4j2-impl/pom.xml b/tika-logging/tika-logging-log4j2-impl/pom.xml
new file mode 100644
index 000000000..f869dc917
--- /dev/null
+++ b/tika-logging/tika-logging-log4j2-impl/pom.xml
@@ -0,0 +1,52 @@
+<?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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.tika</groupId>
+    <artifactId>tika-logging</artifactId>
+    <version>2.6.1-SNAPSHOT</version>
+    <relativePath>../pom.xml</relativePath>
+  </parent>
+
+  <artifactId>tika-logging-log4j2-impl</artifactId>
+
+  <name>Apache Tika logging implementation :: Log4j 2.x</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.tika</groupId>
+      <artifactId>tika-logging-common</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-core</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-slf4j2-impl</artifactId>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/tika-logging/tika-logging-log4j2-impl/src/main/java/org/apache/tika/logging/log4j2/Log4j2LoggingConfigurator.java b/tika-logging/tika-logging-log4j2-impl/src/main/java/org/apache/tika/logging/log4j2/Log4j2LoggingConfigurator.java
new file mode 100644
index 000000000..50d094c1c
--- /dev/null
+++ b/tika-logging/tika-logging-log4j2-impl/src/main/java/org/apache/tika/logging/log4j2/Log4j2LoggingConfigurator.java
@@ -0,0 +1,38 @@
+/*
+ * 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.tika.logging.log4j2;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.tika.logging.spi.LoggingConfiguratorProvider;
+
+public class Log4j2LoggingConfigurator implements LoggingConfiguratorProvider {
+  @Override
+  public String getRootLoggerName() {
+    return LogManager.ROOT_LOGGER_NAME;
+  }
+
+  @Override
+  public String getLoggerLevel(String loggerName) {
+    return LogManager.getLogger(loggerName).getLevel().name();
+  }
+
+  @Override
+  public void setLoggerLevel(String loggerName, String level) {
+    Configurator.setLevel(loggerName, level);
+  }
+}
diff --git a/tika-logging/tika-logging-log4j2-impl/src/main/resources/META-INF/services/org.apache.tika.logging.spi.LoggingConfiguratorProvider b/tika-logging/tika-logging-log4j2-impl/src/main/resources/META-INF/services/org.apache.tika.logging.spi.LoggingConfiguratorProvider
new file mode 100644
index 000000000..7e7a8904a
--- /dev/null
+++ b/tika-logging/tika-logging-log4j2-impl/src/main/resources/META-INF/services/org.apache.tika.logging.spi.LoggingConfiguratorProvider
@@ -0,0 +1 @@
+org.apache.tika.logging.log4j2.Log4j2LoggingConfigurator
\ No newline at end of file
diff --git a/tika-logging/tika-logging-log4j2-impl/src/test/java/org/apache/tika/logging/log4j2/Log4j2LoggingConfiguratorTest.java b/tika-logging/tika-logging-log4j2-impl/src/test/java/org/apache/tika/logging/log4j2/Log4j2LoggingConfiguratorTest.java
new file mode 100644
index 000000000..9df718689
--- /dev/null
+++ b/tika-logging/tika-logging-log4j2-impl/src/test/java/org/apache/tika/logging/log4j2/Log4j2LoggingConfiguratorTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.tika.logging.log4j2;
+
+import java.util.stream.Stream;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.tika.logging.api.LoggerLevelChangeContext;
+import org.apache.tika.logging.api.LoggingConfigurator;
+import org.junit.jupiter.api.*;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.params.provider.Arguments.arguments;
+
+class Log4j2LoggingConfiguratorTest {
+  @BeforeEach
+  void initClass() {
+    Configurator.setRootLevel(Level.INFO);
+    Configurator.setLevel(LoggingConfigurator.class, Level.WARN);
+  }
+
+  @ParameterizedTest
+  @MethodSource("loggerLevelArgs")
+  void getLoggerLevelReturnsCorrectValue(Level expected, Level ignored, String loggerName) {
+    assertEquals(expected, LogManager.getLogger(loggerName).getLevel());
+    assertEquals(expected.name(), LoggingConfigurator.getLoggerLevel(loggerName));
+  }
+
+  @ParameterizedTest
+  @MethodSource("loggerLevelArgs")
+  void setLoggerLevel(Level before, Level after, String loggerName) {
+    String originalLevel = LoggingConfigurator.setLoggerLevel(loggerName, after.name());
+    assertEquals(before.name(), originalLevel);
+    assertEquals(after, LogManager.getLogger(loggerName).getLevel());
+    assertEquals(after.name(), LoggingConfigurator.getLoggerLevel(loggerName));
+  }
+
+  @ParameterizedTest
+  @MethodSource("loggerLevelArgs")
+  void withLoggerLevel(Level before, Level after, String loggerName) throws Exception {
+    try (LoggerLevelChangeContext ignored = LoggingConfigurator.withLoggerLevel(loggerName, after.name())) {
+      assertEquals(after, LogManager.getLogger(loggerName).getLevel());
+    }
+    assertEquals(before, LogManager.getLogger(loggerName).getLevel());
+  }
+
+  static Stream<Arguments> loggerLevelArgs() {
+    return Stream.of(
+        arguments(Level.INFO, Level.ERROR, LogManager.ROOT_LOGGER_NAME),
+        arguments(Level.WARN, Level.ERROR, LoggingConfigurator.class.getCanonicalName()),
+        arguments(Level.INFO, Level.ERROR, Log4j2LoggingConfigurator.class.getCanonicalName())
+    );
+  }
+}
diff --git a/tika-logging/tika-logging-slf4j-bridges/pom.xml b/tika-logging/tika-logging-slf4j-bridges/pom.xml
new file mode 100644
index 000000000..d5172d53d
--- /dev/null
+++ b/tika-logging/tika-logging-slf4j-bridges/pom.xml
@@ -0,0 +1,50 @@
+<?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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.tika</groupId>
+    <artifactId>tika-logging</artifactId>
+    <version>2.6.1-SNAPSHOT</version>
+    <relativePath>../pom.xml</relativePath>
+  </parent>
+
+  <artifactId>tika-logging-slf4j-bridges</artifactId>
+
+  <name>Apache Tika logging slf4j bridges</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>jcl-over-slf4j</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>jul-to-slf4j</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>log4j-over-slf4j</artifactId>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/tika-parent/pom.xml b/tika-parent/pom.xml
index b766c2224..0cae946fc 100644
--- a/tika-parent/pom.xml
+++ b/tika-parent/pom.xml
@@ -893,6 +893,16 @@
         <artifactId>jcl-over-slf4j</artifactId>
         <version>${slf4j.version}</version>
       </dependency>
+      <dependency>
+        <groupId>org.slf4j</groupId>
+        <artifactId>jul-to-slf4j</artifactId>
+        <version>${slf4j.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.slf4j</groupId>
+        <artifactId>log4j-over-slf4j</artifactId>
+        <version>${slf4j.version}</version>
+      </dependency>
       <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>


[tika] 04/04: Migrate tika-parsers-standard-modules to tika-logging

Posted by gr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

grossws pushed a commit to branch logging-refactoring
in repository https://gitbox.apache.org/repos/asf/tika.git

commit 34687cdb05bc4a6553627c6221786482d67014a2
Author: Konstantin Gribov <gr...@gmail.com>
AuthorDate: Sat Nov 19 22:29:58 2022 +0300

    Migrate tika-parsers-standard-modules to tika-logging
---
 .../tika-parsers-standard-modules/pom.xml          | 25 ++++++----------------
 .../tika/parser/microsoft/WordParserTest.java      | 14 +++---------
 2 files changed, 9 insertions(+), 30 deletions(-)

diff --git a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/pom.xml b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/pom.xml
index 43be87b5c..049f0dac5 100644
--- a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/pom.xml
+++ b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/pom.xml
@@ -41,28 +41,15 @@
       <scope>provided</scope>
     </dependency>
     <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>jcl-over-slf4j</artifactId>
-      <scope>provided</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>jul-to-slf4j</artifactId>
-      <scope>provided</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>log4j-over-slf4j</artifactId>
+      <groupId>org.apache.tika</groupId>
+      <artifactId>tika-logging-slf4j-bridges</artifactId>
+      <version>${project.version}</version>
       <scope>provided</scope>
     </dependency>
     <dependency>
-      <groupId>org.apache.logging.log4j</groupId>
-      <artifactId>log4j-core</artifactId>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.logging.log4j</groupId>
-      <artifactId>log4j-slf4j2-impl</artifactId>
+      <groupId>org.apache.tika</groupId>
+      <artifactId>tika-logging-log4j2-impl</artifactId>
+      <version>${project.version}</version>
       <scope>test</scope>
     </dependency>
   </dependencies>
diff --git a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/test/java/org/apache/tika/parser/microsoft/WordParserTest.java b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/test/java/org/apache/tika/parser/microsoft/WordParserTest.java
index 51d574e82..1365cefdb 100644
--- a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/test/java/org/apache/tika/parser/microsoft/WordParserTest.java
+++ b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/test/java/org/apache/tika/parser/microsoft/WordParserTest.java
@@ -26,16 +26,14 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Locale;
 
-import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.config.LoggerConfig;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 import org.xml.sax.ContentHandler;
 
 import org.apache.tika.TikaTest;
 import org.apache.tika.config.TikaConfig;
+import org.apache.tika.logging.api.LoggerLevelChangeContext;
+import org.apache.tika.logging.api.LoggingConfigurator;
 import org.apache.tika.metadata.DublinCore;
 import org.apache.tika.metadata.Metadata;
 import org.apache.tika.metadata.Office;
@@ -361,17 +359,11 @@ public class WordParserTest extends TikaTest {
     @Test
     public void testExceptions1() throws Exception {
         XMLResult xml;
-        LoggerContext ctx = (LoggerContext) LogManager.getContext();
-        LoggerConfig lc = ctx.getConfiguration().getRootLogger();
-        Level originalLevel = lc.getLevel();
-        lc.setLevel(Level.ERROR);
-        try {
+        try (LoggerLevelChangeContext ignored = LoggingConfigurator.withRootLoggerLevel("ERROR")) {
             xml = getXML("testException1.doc");
             assertContains("total population", xml.xml);
             xml = getXML("testException2.doc");
             assertContains("electric charge", xml.xml);
-        } finally {
-            lc.setLevel(originalLevel);
         }
     }