View Javadoc

    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
    18  package org.apache.logging.log4j.jul;
    19
    20  import java.util.logging.Filter;
    21  import java.util.logging.Level;
    22  import java.util.logging.LogRecord;
    23  import java.util.logging.Logger;
    24
    25  import org.apache.logging.log4j.message.Message;
    26  import org.apache.logging.log4j.spi.ExtendedLogger;
    27
    28  /**
    29   * Log4j API implementation of the JUL {@link Logger} class. <strong>Note that this implementation does
    30   * <em>not</em> use the {@link java.util.logging.Handler} class.</strong> Instead, logging is delegated to the
    31   * underlying Log4j {@link org.apache.logging.log4j.Logger} which may be implemented in one of many different ways.
    32   * Consult the documentation for your Log4j Provider for more details.
    33   * <p>Note that the methods {@link #getParent()} and {@link #setLevel(java.util.logging.Level)} are not supported by
    34   * this implementation. If you need support for these methods, then you'll need to use log4j-core. The
    35   * {@link #getParent()} method will not fail (thanks to JUL API limitations), but it won't necessarily be
    36   * accurate!</p>
    37   * <p>Also note that {@link #setParent(java.util.logging.Logger)} is explicitly unsupported. Parent loggers are
    38   * determined using the syntax of the logger name; not through an arbitrary graph of loggers.</p>
    39   *
    40   * @since 2.1
    41   */
    42  public class ApiLogger extends Logger {
    43
    44      private final WrappedLogger logger;
    45      private static final String FQCN = ApiLogger.class.getName();
    46
    47      ApiLogger(final ExtendedLogger logger) {
    48          super(logger.getName(), null);
    49          super.setLevel(LevelTranslator.toJavaLevel(logger.getLevel()));
    50          this.logger = new WrappedLogger(logger);
    51      }
    52
    53      @Override
    54      public void log(final LogRecord record) {
    55          if (isFiltered(record)) {
    56              return;
    57          }
    58          final org.apache.logging.log4j.Level level = LevelTranslator.toLevel(record.getLevel());
    59          final Message message = logger.getMessageFactory().newMessage(record.getMessage(), record.getParameters());
    60          final Throwable thrown = record.getThrown();
    61          logger.logIfEnabled(FQCN, level, null, message, thrown);
    62      }
    63
    64      // support for Logger.getFilter()/Logger.setFilter()
    65      boolean isFiltered(final LogRecord logRecord) {
    66          final Filter filter = getFilter();
    67          return filter != null && !filter.isLoggable(logRecord);
    68      }
    69
    70      @Override
    71      public boolean isLoggable(final Level level) {
    72          return logger.isEnabled(LevelTranslator.toLevel(level));
    73      }
    74
    75      @Override
    76      public String getName() {
    77          return logger.getName();
    78      }
    79
    80      @Override
    81      public void setLevel(final Level newLevel) throws SecurityException {
    82          throw new UnsupportedOperationException("Cannot set level through log4j-api");
    83      }
    84
    85      /**
    86       * Provides access to {@link Logger#setLevel(java.util.logging.Level)}. This method should only be used by child
    87       * classes.
    88       *
    89       * @see Logger#setLevel(java.util.logging.Level)
    90       */
    91      protected void doSetLevel(final Level newLevel) throws SecurityException {
    92          super.setLevel(newLevel);
    93      }
    94
    95      /**
    96       * Unsupported operation.
    97       * @throws UnsupportedOperationException always
    98       */
    99      @Override
    100     public void setParent(final Logger parent) {
    101         throw new UnsupportedOperationException("Cannot set parent logger");
    102     }
    103
    104     @Override
    105     public void log(final Level level, final String msg) {
    106         logger.log(LevelTranslator.toLevel(level), msg);
    107     }
    108
    109     @Override
    110     public void log(final Level level, final String msg, final Object param1) {
    111         logger.log(LevelTranslator.toLevel(level), msg, param1);
    112     }
    113
    114     @Override
    115     public void log(final Level level, final String msg, final Object[] params) {
    116         logger.log(LevelTranslator.toLevel(level), msg, params);
    117     }
    118
    119     @Override
    120     public void log(final Level level, final String msg, final Throwable thrown) {
    121         logger.log(LevelTranslator.toLevel(level), msg, thrown);
    122     }
    123
    124     @Override
    125     public void logp(final Level level, final String sourceClass, final String sourceMethod, final String msg) {
    126         log(level, msg);
    127     }
    128
    129     @Override
    130     public void logp(final Level level, final String sourceClass, final String sourceMethod, final String msg,
    131                      final Object param1) {
    132         log(level, msg, param1);
    133     }
    134
    135     @Override
    136     public void logp(final Level level, final String sourceClass, final String sourceMethod, final String msg,
    137                      final Object[] params) {
    138         log(level, msg, params);
    139     }
    140
    141     @Override
    142     public void logp(final Level level, final String sourceClass, final String sourceMethod, final String msg,
    143                      final Throwable thrown) {
    144         log(level, msg, thrown);
    145     }
    146
    147     @Override
    148     public void logrb(final Level level, final String sourceClass, final String sourceMethod, final String bundleName,
    149                       final String msg) {
    150         log(level, msg);
    151     }
    152
    153     @Override
    154     public void logrb(final Level level, final String sourceClass, final String sourceMethod, final String bundleName,
    155                       final String msg, final Object param1) {
    156         log(level, msg, param1);
    157     }
    158
    159     @Override
    160     public void logrb(final Level level, final String sourceClass, final String sourceMethod, final String bundleName,
    161                       final String msg, final Object[] params) {
    162         log(level, msg, params);
    163     }
    164
    165     @Override
    166     public void logrb(final Level level, final String sourceClass, final String sourceMethod, final String bundleName,
    167                       final String msg, final Throwable thrown) {
    168         log(level, msg, thrown);
    169     }
    170
    171     @Override
    172     public void entering(final String sourceClass, final String sourceMethod) {
    173         logger.entry();
    174     }
    175
    176     @Override
    177     public void entering(final String sourceClass, final String sourceMethod, final Object param1) {
    178         logger.entry(param1);
    179     }
    180
    181     @Override
    182     public void entering(final String sourceClass, final String sourceMethod, final Object[] params) {
    183         logger.entry(params);
    184     }
    185
    186     @Override
    187     public void exiting(final String sourceClass, final String sourceMethod) {
    188         logger.exit();
    189     }
    190
    191     @Override
    192     public void exiting(final String sourceClass, final String sourceMethod, final Object result) {
    193         logger.exit(result);
    194     }
    195
    196     @Override
    197     public void throwing(final String sourceClass, final String sourceMethod, final Throwable thrown) {
    198         logger.throwing(thrown);
    199     }
    200
    201     @Override
    202     public void severe(final String msg) {
    203         logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.ERROR, null, msg);
    204     }
    205
    206     @Override
    207     public void warning(final String msg) {
    208         logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.WARN, null, msg);
    209     }
    210
    211     @Override
    212     public void info(final String msg) {
    213         logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.INFO, null, msg);
    214     }
    215
    216     @Override
    217     public void config(final String msg) {
    218         logger.logIfEnabled(FQCN, LevelTranslator.CONFIG, null, msg);
    219     }
    220
    221     @Override
    222     public void fine(final String msg) {
    223         logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.DEBUG, null, msg);
    224     }
    225
    226     @Override
    227     public void finer(final String msg) {
    228         logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.TRACE, null, msg);
    229     }
    230
    231     @Override
    232     public void finest(final String msg) {
    233         logger.logIfEnabled(FQCN, LevelTranslator.FINEST, null, msg);
    234     }
    235 }