瀏覽代碼

fix #2702 add ErrorToWarnRewritePolicy

Shinsuke Sugaya 2 年之前
父節點
當前提交
beac679951

+ 64 - 0
src/main/java/org/codelibs/fess/util/ErrorToWarnRewritePolicy.java

@@ -0,0 +1,64 @@
+/*
+ * Copyright 2012-2022 CodeLibs Project and the Others.
+ *
+ * Licensed 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.codelibs.fess.util;
+
+import java.util.Arrays;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.Core;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.rewrite.RewritePolicy;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+
+@Plugin(name = "ErrorToWarnRewritePolicy", category = Core.CATEGORY_NAME, elementType = "rewritePolicy", printObject = true)
+public class ErrorToWarnRewritePolicy implements RewritePolicy {
+
+    private final String[] loggerNames;
+
+    public ErrorToWarnRewritePolicy(String[] loggerNames) {
+        this.loggerNames = loggerNames;
+    }
+
+    @Override
+    public LogEvent rewrite(LogEvent event) {
+        final String loggerName = event.getLoggerName();
+        if (loggerName == null) {
+            return event;
+        }
+        for (String name : loggerNames) {
+            if (loggerName.startsWith(name)) {
+                final Level sourceLevel = event.getLevel();
+                if (sourceLevel != Level.ERROR) {
+                    return event;
+                }
+                return new Log4jLogEvent.Builder(event).setLevel(Level.WARN).build();
+            }
+        }
+        return event;
+    }
+
+    @PluginFactory
+    public static ErrorToWarnRewritePolicy createPolicy(@PluginAttribute("loggers") final String loggerNamePrefix) {
+        String[] loggerNames = loggerNamePrefix != null
+                ? Arrays.stream(loggerNamePrefix.split(",")).map(String::trim).filter(s -> s.length() > 0).toArray(n -> new String[n])
+                : new String[0];
+        return new ErrorToWarnRewritePolicy(loggerNames);
+    }
+
+}

+ 5 - 1
src/main/webapp/WEB-INF/env/crawler/resources/log4j2.xml

@@ -13,7 +13,7 @@
 	</Properties>
 
 	<Appenders>
-		<RollingFile name="AppFile" fileName="${log.file.basedir}/${domain.name}.log"
+		<RollingFile name="AppRollingFile" fileName="${log.file.basedir}/${domain.name}.log"
 			filePattern="${log.file.basedir}/${domain.name}${backup.date.suffix}-%i.log.gz">
 			<PatternLayout>
 				<Pattern>${log.pattern}</Pattern>
@@ -25,6 +25,10 @@
 			<DefaultRolloverStrategy fileIndex="max" min="1"
 				max="${backup.max.history}" compressionLevel="9" />
 		</RollingFile>
+		<Rewrite name="AppFile">
+			<AppenderRef ref="AppRollingFile" />
+			<ErrorToWarnRewritePolicy loggers="org.apache.fontbox,org.apache.pdfbox,org.apache.poi" />
+		</Rewrite>
 		<RollingFile name="StatsFile" fileName="${log.file.basedir}/fess-urls.log"
 			filePattern="${log.file.basedir}/fess-urls${backup.date.suffix}-%i.log.gz">
 			<PatternLayout>