View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.util;
17  
18  import java.util.Arrays;
19  
20  import org.apache.logging.log4j.Level;
21  import org.apache.logging.log4j.core.Core;
22  import org.apache.logging.log4j.core.LogEvent;
23  import org.apache.logging.log4j.core.appender.rewrite.RewritePolicy;
24  import org.apache.logging.log4j.core.config.plugins.Plugin;
25  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
26  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
27  import org.apache.logging.log4j.core.impl.Log4jLogEvent;
28  
29  /**
30   * Log4j rewrite policy that converts ERROR level log events to WARN level for specified loggers.
31   * This policy is useful for downgrading the severity of log events from certain loggers
32   * to prevent them from being treated as critical errors.
33   */
34  @Plugin(name = "ErrorToWarnRewritePolicy", category = Core.CATEGORY_NAME, elementType = "rewritePolicy", printObject = true)
35  public class ErrorToWarnRewritePolicy implements RewritePolicy {
36  
37      /** Array of logger name prefixes to apply the ERROR to WARN conversion */
38      private final String[] loggerNames;
39  
40      /**
41       * Constructs a new ErrorToWarnRewritePolicy with the specified logger names.
42       *
43       * @param loggerNames array of logger name prefixes to convert ERROR to WARN
44       */
45      public ErrorToWarnRewritePolicy(final String[] loggerNames) {
46          this.loggerNames = loggerNames;
47      }
48  
49      /**
50       * Rewrites log events by converting ERROR level to WARN level for matching loggers.
51       *
52       * @param event the log event to potentially rewrite
53       * @return the original event or a new event with WARN level if conversion applied
54       */
55      @Override
56      public LogEvent rewrite(final LogEvent event) {
57          final String loggerName = event.getLoggerName();
58          if (loggerName == null) {
59              return event;
60          }
61          for (final String name : loggerNames) {
62              if (loggerName.startsWith(name)) {
63                  final Level sourceLevel = event.getLevel();
64                  if (sourceLevel != Level.ERROR) {
65                      return event;
66                  }
67                  return new Log4jLogEvent.Builder(event).setLevel(Level.WARN).build();
68              }
69          }
70          return event;
71      }
72  
73      /**
74       * Factory method to create an ErrorToWarnRewritePolicy instance.
75       *
76       * @param loggerNamePrefix comma-separated list of logger name prefixes
77       * @return a new ErrorToWarnRewritePolicy instance
78       */
79      @PluginFactory
80      public static ErrorToWarnRewritePolicy createPolicy(@PluginAttribute("loggers") final String loggerNamePrefix) {
81          final String[] loggerNames = loggerNamePrefix != null
82                  ? Arrays.stream(loggerNamePrefix.split(",")).map(String::trim).filter(s -> s.length() > 0).toArray(n -> new String[n])
83                  : new String[0];
84          return new ErrorToWarnRewritePolicy(loggerNames);
85      }
86  
87  }