001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.layout; 018 019import java.util.Collections; 020import java.util.HashMap; 021import java.util.Map; 022 023import org.apache.logging.log4j.core.config.Node; 024import org.apache.logging.log4j.core.config.plugins.Plugin; 025import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 026import org.apache.logging.log4j.core.config.plugins.PluginElement; 027import org.apache.logging.log4j.core.config.plugins.PluginFactory; 028import org.apache.logging.log4j.core.util.KeyValuePair; 029import org.apache.logging.log4j.message.StructuredDataId; 030 031/** 032 * A LoggerFields container. 033 */ 034@Plugin(name = "LoggerFields", category = Node.CATEGORY, printObject = true) 035public final class LoggerFields { 036 037 private final Map<String, String> map; 038 private final String sdId; 039 private final String enterpriseId; 040 private final boolean discardIfAllFieldsAreEmpty; 041 042 private LoggerFields(final Map<String, String> map, final String sdId, final String enterpriseId, 043 final boolean discardIfAllFieldsAreEmpty) { 044 this.sdId = sdId; 045 this.enterpriseId = enterpriseId; 046 this.map = Collections.unmodifiableMap(map); 047 this.discardIfAllFieldsAreEmpty = discardIfAllFieldsAreEmpty; 048 } 049 050 public Map<String, String> getMap() { 051 return this.map; 052 } 053 054 @Override 055 public String toString() { 056 return this.map.toString(); 057 } 058 059 /** 060 * Create a LoggerFields from KeyValuePairs. 061 * 062 * @param keyValuePairs 063 * An array of KeyValuePairs. 064 * @param sdId 065 * The SD-ID in an SD-ELEMENT 066 * @param enterpriseId 067 * The IANA assigned enterprise number 068 * @param discardIfAllFieldsAreEmpty 069 * this SD-ELEMENT should be discarded if all fields are empty 070 * @return A LoggerFields instance containing a Map<String, String>. 071 */ 072 @PluginFactory 073 public static LoggerFields createLoggerFields( 074 @PluginElement("LoggerFields") final KeyValuePair[] keyValuePairs, 075 @PluginAttribute("sdId") final String sdId, 076 @PluginAttribute("enterpriseId") final String enterpriseId, 077 @PluginAttribute(value = "discardIfAllFieldsAreEmpty") final boolean discardIfAllFieldsAreEmpty) { 078 final Map<String, String> map = new HashMap<>(); 079 080 for (final KeyValuePair keyValuePair : keyValuePairs) { 081 map.put(keyValuePair.getKey(), keyValuePair.getValue()); 082 } 083 084 return new LoggerFields(map, sdId, enterpriseId, discardIfAllFieldsAreEmpty); 085 } 086 087 public StructuredDataId getSdId() { 088 if (enterpriseId == null || sdId == null) { 089 return null; 090 } 091 final int eId = Integer.parseInt(enterpriseId); 092 return new StructuredDataId(sdId, eId, null, null); 093 } 094 095 public boolean getDiscardIfAllFieldsAreEmpty() { 096 return discardIfAllFieldsAreEmpty; 097 } 098}