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.pattern; 018 019import java.util.Map; 020import java.util.Set; 021import java.util.TreeSet; 022 023import org.apache.logging.log4j.core.LogEvent; 024import org.apache.logging.log4j.core.config.plugins.Plugin; 025 026/** 027 * Able to handle the contents of the LogEvent's MDC and either 028 * output the entire contents of the properties in a similar format to the 029 * java.util.Hashtable.toString(), or to output the value of a specific key 030 * within the property bundle 031 * when this pattern converter has the option set. 032 */ 033@Plugin(name = "MdcPatternConverter", category = PatternConverter.CATEGORY) 034@ConverterKeys({ "X", "mdc", "MDC" }) 035public final class MdcPatternConverter extends LogEventPatternConverter { 036 /** 037 * Name of property to output. 038 */ 039 private final String key; 040 private final String[] keys; 041 private final boolean full; 042 043 /** 044 * Private constructor. 045 * 046 * @param options options, may be null. 047 */ 048 private MdcPatternConverter(final String[] options) { 049 super(options != null && options.length > 0 ? "MDC{" + options[0] + '}' : "MDC", "mdc"); 050 if (options != null && options.length > 0) { 051 full = false; 052 if (options[0].indexOf(',') > 0) { 053 keys = options[0].split(","); 054 key = null; 055 } else { 056 keys = null; 057 key = options[0]; 058 } 059 } else { 060 full = true; 061 key = null; 062 keys = null; 063 } 064 } 065 066 /** 067 * Obtains an instance of PropertiesPatternConverter. 068 * 069 * @param options options, may be null or first element contains name of property to format. 070 * @return instance of PropertiesPatternConverter. 071 */ 072 public static MdcPatternConverter newInstance(final String[] options) { 073 return new MdcPatternConverter(options); 074 } 075 076 /** 077 * {@inheritDoc} 078 */ 079 @Override 080 public void format(final LogEvent event, final StringBuilder toAppendTo) { 081 final Map<String, String> contextMap = event.getContextMap(); 082 // if there is no additional options, we output every single 083 // Key/Value pair for the MDC in a similar format to Hashtable.toString() 084 if (full) { 085 if (contextMap == null || contextMap.isEmpty()) { 086 toAppendTo.append("{}"); 087 return; 088 } 089 final StringBuilder sb = new StringBuilder("{"); 090 final Set<String> keys = new TreeSet<>(contextMap.keySet()); 091 for (final String key : keys) { 092 if (sb.length() > 1) { 093 sb.append(", "); 094 } 095 sb.append(key).append('=').append(contextMap.get(key)); 096 097 } 098 sb.append('}'); 099 toAppendTo.append(sb); 100 } else { 101 if (keys != null) { 102 if (contextMap == null || contextMap.isEmpty()) { 103 toAppendTo.append("{}"); 104 return; 105 } 106 // Print all the keys in the array that have a value. 107 final StringBuilder sb = new StringBuilder("{"); 108 for (String key : keys) { 109 key = key.trim(); 110 if (contextMap.containsKey(key)) { 111 if (sb.length() > 1) { 112 sb.append(", "); 113 } 114 sb.append(key).append('=').append(contextMap.get(key)); 115 } 116 } 117 sb.append('}'); 118 toAppendTo.append(sb); 119 } else if (contextMap != null){ 120 // otherwise they just want a single key output 121 final Object val = contextMap.get(key); 122 123 if (val != null) { 124 toAppendTo.append(val); 125 } 126 } 127 } 128 } 129}