blob: fe57a332e375f1dfd74c6c9721ff12a0d50db530 [file] [log] [blame]
/*
* 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.logging.log4j.core.layout;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.BasicConfigurationFactory;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
import static org.junit.Assert.assertTrue;
/**
*
*/
public class SerializedLayoutTest {
LoggerContext ctx = (LoggerContext) LogManager.getContext();
Logger root = ctx.getLogger("");
static ConfigurationFactory cf = new BasicConfigurationFactory();
@BeforeClass
public static void setupClass() {
ConfigurationFactory.setConfigurationFactory(cf);
LoggerContext ctx = (LoggerContext) LogManager.getContext();
ctx.reconfigure();
}
@AfterClass
public static void cleanupClass() {
ConfigurationFactory.removeConfigurationFactory(cf);
}
private static final String body =
"<log4j:message><![CDATA[empty mdc]]></log4j:message>\r";
private static final String[] expected = {
"Logger=root Level=DEBUG Messagestarting mdc pattern test",
"Logger=root Level=DEBUG Messageempty mdc",
"Logger=root Level=DEBUG Messagefilled mdc",
"Logger=root Level=ERROR Messagefinished mdc pattern test"
};
/**
* Test case for MDC conversion pattern.
*/
@Test
public void testLayout() throws Exception {
// set up appender
SerializedLayout layout = SerializedLayout.createLayout();
ListAppender appender = new ListAppender("List", null, layout, false, true);
appender.start();
// set appender on root and set level to debug
root.addAppender(appender);
root.setLevel(Level.DEBUG);
// output starting message
root.debug("starting mdc pattern test");
root.debug("empty mdc");
ThreadContext.put("key1", "value1");
ThreadContext.put("key2", "value2");
root.debug("filled mdc");
ThreadContext.remove("key1");
ThreadContext.remove("key2");
root.error("finished mdc pattern test", new NullPointerException("test"));
appender.stop();
List<byte[]> data = appender.getData();
assertTrue(data.size() > 0);
int i = 0;
for (byte[] item : data) {
ByteArrayInputStream bais = new ByteArrayInputStream(item);
ObjectInputStream ois = new ObjectInputStream(bais);
LogEvent event;
try {
event = (LogEvent) ois.readObject();
} catch (IOException ioe) {
System.err.println("Exception processing item " + i);
throw ioe;
}
assertTrue("Incorrect event", event.toString().equals(expected[i]));
++i;
}
}
}