org.apache.log4j.layout.Log4j1XmlLayout Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache license, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the license for the specific language governing permissions and
* limitations under the license.
*/
package org.apache.log4j.layout;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Node;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
import org.apache.logging.log4j.core.layout.ByteBufferDestination;
import org.apache.logging.log4j.core.util.Transform;
import org.apache.logging.log4j.util.Strings;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Port of XMLLayout in Log4j 1.x. Provided for compatibility with existing Log4j 1 configurations.
*
* Originally developed by Ceki Gülcü, Mathias Bogaert.
*/
@Plugin(name = "Log4j1XmlLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
public final class Log4j1XmlLayout extends AbstractStringLayout {
private final boolean locationInfo;
private final boolean properties;
@PluginFactory
public static Log4j1XmlLayout createLayout(
// @formatter:off
@PluginAttribute(value = "locationInfo", defaultBoolean = false) final boolean locationInfo,
@PluginAttribute(value = "properties", defaultBoolean = false) final boolean properties
// @formatter:on
) {
return new Log4j1XmlLayout(locationInfo, properties);
}
private Log4j1XmlLayout(final boolean locationInfo, final boolean properties) {
super(StandardCharsets.UTF_8);
this.locationInfo = locationInfo;
this.properties = properties;
}
public boolean isLocationInfo() {
return locationInfo;
}
public boolean isProperties() {
return properties;
}
@Override
public void encode(final LogEvent event, final ByteBufferDestination destination) {
final StringBuilder text = getStringBuilder();
formatTo(event, text);
getStringBuilderEncoder().encode(text, destination);
}
@Override
public String toSerializable(final LogEvent event) {
final StringBuilder text = getStringBuilder();
formatTo(event, text);
return text.toString();
}
private void formatTo(final LogEvent event, final StringBuilder buf) {
// We yield to the \r\n heresy.
buf.append("\r\n");
buf.append(" \r\n");
final List ndc = event.getContextStack().asList();
if (!ndc.isEmpty()) {
buf.append(" \r\n");
}
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
final
Throwable thrown = event.getThrown();
if (thrown != null) {
buf.append(" \r\n");
}
if (locationInfo) {
final StackTraceElement source = event.getSource();
if (source != null) {
buf.append(" \r\n");
}
}
if (properties) {
final Map contextMap = event.getContextMap();
if (!contextMap.isEmpty()) {
buf.append("\r\n");
final Object[] keys = contextMap.keySet().toArray();
Arrays.sort(keys);
for (final Object key1 : keys) {
final String key = key1.toString();
final String val = contextMap.get(key);
if (val != null) {
buf.append(" \r\n");
}
}
buf.append(" \r\n");
}
}
buf.append(" \r\n\r\n");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy