Log4jApiLogFactory.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.logging.impl;

  18. import java.io.IOException;
  19. import java.util.concurrent.ConcurrentHashMap;
  20. import java.util.concurrent.ConcurrentMap;

  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.logging.log4j.Level;
  24. import org.apache.logging.log4j.LogManager;
  25. import org.apache.logging.log4j.Marker;
  26. import org.apache.logging.log4j.MarkerManager;
  27. import org.apache.logging.log4j.spi.AbstractLoggerAdapter;
  28. import org.apache.logging.log4j.spi.ExtendedLogger;
  29. import org.apache.logging.log4j.spi.LoggerAdapter;
  30. import org.apache.logging.log4j.spi.LoggerContext;
  31. import org.apache.logging.log4j.util.StackLocatorUtil;

  32. /**
  33.  * Logger factory hardcoded to send everything to Log4j API.
  34.  * <p>
  35.  * Based on the `log4j-jcl` artifact from Apache Logging Services.
  36.  * </p>
  37.  *
  38.  * @since 1.3.0
  39.  */
  40. public final class Log4jApiLogFactory extends LogFactory {

  41.     private static final class Log4j2Log implements Log {

  42.         private static final String FQCN = Log4j2Log.class.getName();

  43.         private final ExtendedLogger logger;

  44.         public Log4j2Log(final ExtendedLogger logger) {
  45.             this.logger = logger;
  46.         }

  47.         @Override
  48.         public void debug(final Object message) {
  49.             logIfEnabled(Level.DEBUG, message, null);
  50.         }

  51.         @Override
  52.         public void debug(final Object message, final Throwable t) {
  53.             logIfEnabled(Level.DEBUG, message, t);
  54.         }

  55.         @Override
  56.         public void error(final Object message) {
  57.             logIfEnabled(Level.ERROR, message, null);
  58.         }

  59.         @Override
  60.         public void error(final Object message, final Throwable t) {
  61.             logIfEnabled(Level.ERROR, message, t);
  62.         }

  63.         @Override
  64.         public void fatal(final Object message) {
  65.             logIfEnabled(Level.FATAL, message, null);
  66.         }

  67.         @Override
  68.         public void fatal(final Object message, final Throwable t) {
  69.             logIfEnabled(Level.FATAL, message, t);
  70.         }

  71.         @Override
  72.         public void info(final Object message) {
  73.             logIfEnabled(Level.INFO, message, null);
  74.         }

  75.         @Override
  76.         public void info(final Object message, final Throwable t) {
  77.             logIfEnabled(Level.INFO, message, t);
  78.         }

  79.         @Override
  80.         public boolean isDebugEnabled() {
  81.             return isEnabled(Level.DEBUG);
  82.         }

  83.         private boolean isEnabled(final Level level) {
  84.             return logger.isEnabled(level, MARKER, null);
  85.         }

  86.         @Override
  87.         public boolean isErrorEnabled() {
  88.             return isEnabled(Level.ERROR);
  89.         }

  90.         @Override
  91.         public boolean isFatalEnabled() {
  92.             return isEnabled(Level.FATAL);
  93.         }

  94.         @Override
  95.         public boolean isInfoEnabled() {
  96.             return isEnabled(Level.INFO);
  97.         }

  98.         @Override
  99.         public boolean isTraceEnabled() {
  100.             return isEnabled(Level.TRACE);
  101.         }

  102.         @Override
  103.         public boolean isWarnEnabled() {
  104.             return isEnabled(Level.WARN);
  105.         }

  106.         private void logIfEnabled(final Level level, final Object message, final Throwable t) {
  107.             if (message instanceof CharSequence) {
  108.                 logger.logIfEnabled(FQCN, level, MARKER, (CharSequence) message, t);
  109.             } else {
  110.                 logger.logIfEnabled(FQCN, level, MARKER, message, t);
  111.             }
  112.         }

  113.         @Override
  114.         public void trace(final Object message) {
  115.             logIfEnabled(Level.TRACE, message, null);
  116.         }

  117.         @Override
  118.         public void trace(final Object message, final Throwable t) {
  119.             logIfEnabled(Level.TRACE, message, t);
  120.         }

  121.         @Override
  122.         public void warn(final Object message) {
  123.             logIfEnabled(Level.WARN, message, null);
  124.         }

  125.         @Override
  126.         public void warn(final Object message, final Throwable t) {
  127.             logIfEnabled(Level.WARN, message, t);
  128.         }
  129.     }
  130.     private static final class LogAdapter extends AbstractLoggerAdapter<Log> {

  131.         @Override
  132.         protected LoggerContext getContext() {
  133.             return getContext(LogManager.getFactory().isClassLoaderDependent() ? StackLocatorUtil.getCallerClass(
  134.                     LogFactory.class) : null);
  135.         }

  136.         @Override
  137.         protected Log newLogger(final String name, final LoggerContext context) {
  138.             return new Log4j2Log(context.getLogger(name));
  139.         }

  140.     }

  141.     private static final String[] EMPTY_ARRAY = {};

  142.     /**
  143.      * Marker used by all messages coming from Apache Commons Logging.
  144.      */
  145.     private static final Marker MARKER = MarkerManager.getMarker("COMMONS-LOGGING");

  146.     /**
  147.      * Caches Log instances
  148.      */
  149.     private final LoggerAdapter<Log> adapter = new LogAdapter();

  150.     private final ConcurrentMap<String, Object> attributes = new ConcurrentHashMap<>();

  151.     /**
  152.      * Constructs a new instance.
  153.      */
  154.     public Log4jApiLogFactory() {
  155.         // empty
  156.     }

  157.     @Override
  158.     public Object getAttribute(final String name) {
  159.         return attributes.get(name);
  160.     }

  161.     @Override
  162.     public String[] getAttributeNames() {
  163.         return attributes.keySet().toArray(EMPTY_ARRAY);
  164.     }

  165.     @Override
  166.     public Log getInstance(final Class<?> clazz) {
  167.         return getInstance(clazz.getName());
  168.     }

  169.     @Override
  170.     public Log getInstance(final String name) {
  171.         return adapter.getLogger(name);
  172.     }

  173.     /**
  174.      * This method is supposed to clear all loggers. In this implementation it will clear all the logger
  175.      * wrappers but the loggers managed by the underlying logger context will not be.
  176.      */
  177.     @Override
  178.     public void release() {
  179.         try {
  180.             adapter.close();
  181.         } catch (final IOException ignored) {
  182.             // Ignore
  183.         }
  184.     }

  185.     @Override
  186.     public void removeAttribute(final String name) {
  187.         attributes.remove(name);
  188.     }

  189.     @Override
  190.     public void setAttribute(final String name, final Object value) {
  191.         if (value != null) {
  192.             attributes.put(name, value);
  193.         } else {
  194.             removeAttribute(name);
  195.         }
  196.     }
  197. }