fix #2702 add ErrorToWarnRewritePolicy

This commit is contained in:
Shinsuke Sugaya 2022-12-27 13:35:31 +09:00
parent df18b81094
commit beac679951
2 changed files with 69 additions and 1 deletions

View file

@ -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);
}
}

View file

@ -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>