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  package org.apache.logging.log4j.core.lookup;
    18
    19  import java.util.MissingResourceException;
    20  import java.util.ResourceBundle;
    21
    22  import org.apache.logging.log4j.Logger;
    23  import org.apache.logging.log4j.Marker;
    24  import org.apache.logging.log4j.MarkerManager;
    25  import org.apache.logging.log4j.core.LogEvent;
    26  import org.apache.logging.log4j.core.config.plugins.Plugin;
    27  import org.apache.logging.log4j.status.StatusLogger;
    28
    29  /**
    30   * Looks up keys from resource bundles.
    31   */
    32  @Plugin(name = "bundle", category = StrLookup.CATEGORY)
    33  public class ResourceBundleLookup extends AbstractLookup {
    34
    35      private static final Logger LOGGER = StatusLogger.getLogger();
    36      private static final Marker LOOKUP = MarkerManager.getMarker("LOOKUP");
    37
    38      /**
    39       * Looks up the value for the key in the format "BundleName:BundleKey".
    40       *
    41       * For example: "com.domain.messages:MyKey".
    42       *
    43       * @param event
    44       *            The current LogEvent.
    45       * @param key
    46       *            the key to be looked up, may be null
    47       * @return The value associated with the key.
    48       */
    49      @Override
    50      public String lookup(final LogEvent event, final String key) {
    51          if (key == null) {
    52              return null;
    53          }
    54          final String[] keys = key.split(":");
    55          final int keyLen = keys.length;
    56          if (keyLen != 2) {
    57              LOGGER.warn(LOOKUP, "Bad ResourceBundle key format [{}]. Expected format is BundleName:KeyName.", key);
    58              return null;
    59          }
    60          final String bundleName = keys[0];
    61          final String bundleKey = keys[1];
    62          try {
    63              // The ResourceBundle class caches bundles, no need to cache here.
    64              return ResourceBundle.getBundle(bundleName).getString(bundleKey);
    65          } catch (final MissingResourceException e) {
    66              LOGGER.warn(LOOKUP, "Error looking up ResourceBundle [{}].", bundleName, e);
    67              return null;
    68          }
    69      }
    70  }