You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by mi...@apache.org on 2017/01/17 20:54:14 UTC
[23/32] logging-log4j2 git commit: LOG4J2-1769 Make sure that
GelfLayout is not affected by the same issue
LOG4J2-1769 Make sure that GelfLayout is not affected by the same issue
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/b34236d8
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/b34236d8
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/b34236d8
Branch: refs/heads/LOG4J2-1748and1780-remove-ExecutorService-from-LoggerContext
Commit: b34236d8beb2175ff36074a8cf7a40ca73fae59d
Parents: b331da4
Author: Mikael St�ldal <mi...@magine.com>
Authored: Mon Jan 16 17:50:01 2017 +0100
Committer: Mikael St�ldal <mi...@magine.com>
Committed: Mon Jan 16 17:50:01 2017 +0100
----------------------------------------------------------------------
.../ConcurrentLoggingWithGelfLayoutTest.java | 116 +++++++++++++++++++
.../src/test/resources/log4j2-gelf-layout.xml | 31 +++++
2 files changed, 147 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b34236d8/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/ConcurrentLoggingWithGelfLayoutTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/ConcurrentLoggingWithGelfLayoutTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/ConcurrentLoggingWithGelfLayoutTest.java
new file mode 100644
index 0000000..af04b35
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/ConcurrentLoggingWithGelfLayoutTest.java
@@ -0,0 +1,116 @@
+/*
+ * 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 java.io.File;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.junit.AfterClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.endsWith;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.junit.Assert.*;
+
+/**
+ * Test for LOG4J2-1769, kind of.
+ *
+ * @since 2.8
+ */
+public class ConcurrentLoggingWithGelfLayoutTest {
+ @ClassRule
+ public static LoggerContextRule context = new LoggerContextRule("log4j2-gelf-layout.xml");
+ private static final String PATH = "target/test-gelf-layout.log";
+
+ @AfterClass
+ public static void after() {
+ new File(PATH).delete();
+ }
+
+ @Test
+ public void testConcurrentLogging() throws Throwable {
+ final Logger log = context.getLogger(ConcurrentLoggingWithGelfLayoutTest.class);
+ final Set<Thread> threads = Collections.synchronizedSet(new HashSet<Thread>());
+ final List<Throwable> thrown = Collections.synchronizedList(new ArrayList<Throwable>());
+
+ for (int x = 0; x < Runtime.getRuntime().availableProcessors() * 2; x++) {
+ final Thread t = new LoggingThread(threads, log);
+ threads.add(t);
+
+ // Appender is configured with ignoreExceptions="false";
+ // any exceptions are propagated to the caller, so we can catch them here.
+ t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
+ @Override
+ public void uncaughtException(final Thread t, final Throwable e) {
+ thrown.add(e);
+ }
+ });
+ t.start();
+ }
+
+ while (!threads.isEmpty()) {
+ log.info("not done going to sleep...");
+ Thread.sleep(10);
+ }
+
+ // if any error occurred, fail this test
+ if (!thrown.isEmpty()) {
+ throw thrown.get(0);
+ }
+
+ // simple test to ensure content is not corrupted
+ if (new File(PATH).exists()) {
+ final List<String> lines = Files.readAllLines(new File(PATH).toPath(), Charset.defaultCharset());
+ for (final String line : lines) {
+ assertThat(line, startsWith("{\"version\":\"1.1\",\"host\":\"myself\",\"timestamp\":"));
+ assertThat(line, endsWith("\"}"));
+ }
+ }
+ }
+
+ private class LoggingThread extends Thread {
+ private final Set<Thread> threads;
+ private final Logger log;
+
+ LoggingThread(final Set<Thread> threads, final Logger log) {
+ this.threads = threads;
+ this.log = log;
+ }
+
+ @Override
+ public void run() {
+ log.info(threads.size());
+ try {
+ for (int i = 0; i < 64; i++) {
+ log.info("First message.");
+ log.info("Second message.");
+ }
+ } finally {
+ threads.remove(this);
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b34236d8/log4j-core/src/test/resources/log4j2-gelf-layout.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/log4j2-gelf-layout.xml b/log4j-core/src/test/resources/log4j2-gelf-layout.xml
new file mode 100644
index 0000000..94cef12
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j2-gelf-layout.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+
+-->
+<Configuration status="INFO" packages="">
+ <Appenders>
+ <File name="file" ignoreExceptions="false" fileName="target/test-gelf-layout.log" append="false">
+ <GelfLayout host="myself" compressionType="OFF"/>
+ </File>
+ </Appenders>
+
+ <Loggers>
+ <Root level="INFO">
+ <AppenderRef ref="file"/>
+ </Root>
+ </Loggers>
+</Configuration>