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.layout;
    18
    19  import java.util.Collections;
    20  import java.util.HashMap;
    21  import java.util.Map;
    22
    23  import org.apache.logging.log4j.core.config.Node;
    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.PluginElement;
    27  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
    28  import org.apache.logging.log4j.core.util.KeyValuePair;
    29  import org.apache.logging.log4j.message.StructuredDataId;
    30
    31  /**
    32   * A LoggerFields container.
    33   */
    34  @Plugin(name = "LoggerFields", category = Node.CATEGORY, printObject = true)
    35  public final class LoggerFields {
    36
    37      private final Map<String, String> map;
    38      private final String sdId;
    39      private final String enterpriseId;
    40      private final boolean discardIfAllFieldsAreEmpty;
    41
    42      private LoggerFields(final Map<String, String> map, final String sdId, final String enterpriseId,
    43              final boolean discardIfAllFieldsAreEmpty) {
    44          this.sdId = sdId;
    45          this.enterpriseId = enterpriseId;
    46          this.map = Collections.unmodifiableMap(map);
    47          this.discardIfAllFieldsAreEmpty = discardIfAllFieldsAreEmpty;
    48      }
    49
    50      public Map<String, String> getMap() {
    51          return this.map;
    52      }
    53
    54      @Override
    55      public String toString() {
    56          return this.map.toString();
    57      }
    58
    59      /**
    60       * Create a LoggerFields from KeyValuePairs.
    61       *
    62       * @param keyValuePairs
    63       *            An array of KeyValuePairs.
    64       * @param sdId
    65       *            The SD-ID in an SD-ELEMENT
    66       * @param enterpriseId
    67       *            The IANA assigned enterprise number
    68       * @param discardIfAllFieldsAreEmpty
    69       *            this SD-ELEMENT should be discarded if all fields are empty
    70       * @return A LoggerFields instance containing a Map&lt;String, String&gt;.
    71       */
    72      @PluginFactory
    73      public static LoggerFields createLoggerFields(
    74          @PluginElement("LoggerFields") final KeyValuePair[] keyValuePairs,
    75          @PluginAttribute("sdId") final String sdId,
    76          @PluginAttribute("enterpriseId") final String enterpriseId,
    77          @PluginAttribute(value = "discardIfAllFieldsAreEmpty", defaultBoolean = false) final boolean discardIfAllFieldsAreEmpty) {
    78          final Map<String, String> map = new HashMap<String, String>();
    79
    80          for (final KeyValuePair keyValuePair : keyValuePairs) {
    81              map.put(keyValuePair.getKey(), keyValuePair.getValue());
    82          }
    83
    84          return new LoggerFields(map, sdId, enterpriseId, discardIfAllFieldsAreEmpty);
    85      }
    86
    87      public StructuredDataId getSdId() {
    88          if (enterpriseId == null || sdId == null) {
    89              return null;
    90          }
    91          final int eId = Integer.parseInt(enterpriseId);
    92          return new StructuredDataId(sdId, eId, null, null);
    93      }
    94
    95      public boolean getDiscardIfAllFieldsAreEmpty() {
    96          return discardIfAllFieldsAreEmpty;
    97      }
    98  }