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     */
017    package org.apache.logging.log4j.core.appender.rewrite;
018    
019    import org.apache.logging.log4j.Logger;
020    import org.apache.logging.log4j.core.LogEvent;
021    import org.apache.logging.log4j.core.config.Configuration;
022    import org.apache.logging.log4j.core.config.Property;
023    import org.apache.logging.log4j.core.config.plugins.Plugin;
024    import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
025    import org.apache.logging.log4j.core.config.plugins.PluginElement;
026    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
027    import org.apache.logging.log4j.core.impl.Log4jLogEvent;
028    import org.apache.logging.log4j.status.StatusLogger;
029    
030    import java.util.Arrays;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    /**
036     * This policy modifies events by replacing or possibly adding keys and values to the MapMessage.
037     */
038    @Plugin(name = "PropertiesRewritePolicy", type = "Core", elementType = "rewritePolicy", printObject = true)
039    public final class PropertiesRewritePolicy implements RewritePolicy {
040        /**
041         * Allow subclasses access to the status logger without creating another instance.
042         */
043        protected static final Logger LOGGER = StatusLogger.getLogger();
044    
045        private final Map<Property, Boolean> properties;
046    
047        private final Configuration config;
048    
049        private PropertiesRewritePolicy(final Configuration config, final List<Property> props) {
050            this.config = config;
051            this.properties = new HashMap<Property, Boolean>(props.size());
052            for (final Property prop : props) {
053                final boolean interpolate = prop.getValue().contains("${");
054                properties.put(prop, interpolate);
055            }
056        }
057    
058        /**
059         * Rewrite the event.
060         * @param source a logging event that may be returned or
061         * used to create a new logging event.
062         * @return The LogEvent after rewriting.
063         */
064        public LogEvent rewrite(final LogEvent source) {
065            final Map<String, String> props = new HashMap<String, String>(source.getContextMap());
066            for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
067                final Property prop = entry.getKey();
068                props.put(prop.getName(), entry.getValue() ?
069                    config.getSubst().replace(prop.getValue()) : prop.getValue());
070            }
071    
072            return new Log4jLogEvent(source.getLoggerName(), source.getMarker(), source.getFQCN(), source.getLevel(),
073                source.getMessage(), source.getThrown(), props, source.getContextStack(), source.getThreadName(),
074                source.getSource(), source.getMillis());
075        }
076    
077        @Override
078        public String toString() {
079            final StringBuilder sb = new StringBuilder();
080            sb.append(" {");
081            boolean first = true;
082            for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
083                if (!first) {
084                    sb.append(", ");
085                }
086                final Property prop = entry.getKey();
087                sb.append(prop.getName()).append("=").append(prop.getValue());
088                first = false;
089            }
090            sb.append("}");
091            return sb.toString();
092        }
093    
094        /**
095         * The factory method to create the PropertiesRewritePolicy.
096         * @param config The Configuration.
097         * @param props key/value pairs for the new keys and values.
098         * @return The PropertiesRewritePolicy.
099         */
100        @PluginFactory
101        public static PropertiesRewritePolicy createPolicy(@PluginConfiguration final Configuration config,
102                                                    @PluginElement("properties") final Property[] props) {
103            if (props == null || props.length == 0) {
104                LOGGER.error("Properties must be specified for the PropertiesRewritePolicy");
105                return null;
106            }
107            final List<Property> properties = Arrays.asList(props);
108            return new PropertiesRewritePolicy(config, properties);
109        }
110    }