Codebase list libslf4j-java / 132b2cd
Imported Upstream version 1.7.20 Emmanuel Bourg 7 years ago
27 changed file(s) with 490 addition(s) and 479 deletion(s). Raw diff Collapse all Expand all
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>integration</artifactId>
44 <parent>
55 <groupId>org.slf4j</groupId>
66 <artifactId>slf4j-parent</artifactId>
7 <version>1.7.19</version>
7 <version>1.7.20</version>
88 </parent>
99
1010 <modelVersion>4.0.0</modelVersion>
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>jul-to-slf4j</artifactId>
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>osgi-over-slf4j</artifactId>
55
66 <groupId>org.slf4j</groupId>
77 <artifactId>slf4j-parent</artifactId>
8 <version>1.7.19</version>
8 <version>1.7.20</version>
99
1010 <packaging>pom</packaging>
1111 <name>SLF4J</name>
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>slf4j-android</artifactId>
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>slf4j-api</artifactId>
239239
240240 private static void emitReplayWarning(int eventCount) {
241241 Util.report("A number (" + eventCount + ") of logging calls during the initialization phase have been intercepted and are");
242 Util.report("now being replayed. These are suject to the filtering rules of the underlying logging system.");
242 Util.report("now being replayed. These are subject to the filtering rules of the underlying logging system.");
243243 Util.report("See also " + REPLAY_URL);
244244 }
245245
0 /**
1 * Copyright (c) 2004-2016 QOS.ch
2 * All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24 package org.slf4j;
25
26 import java.util.concurrent.CyclicBarrier;
27 import java.util.concurrent.atomic.AtomicLong;
28
29 public class LoggerAccessingThread extends Thread {
30 private static int LOOP_LEN = 64;
31
32 final CyclicBarrier barrier;
33 final int count;
34 final AtomicLong eventCount;
35
36 public LoggerAccessingThread(final CyclicBarrier barrier, final int count, final AtomicLong eventCount) {
37 this.barrier = barrier;
38 this.count = count;
39 this.eventCount = eventCount;
40 }
41
42 public void run() {
43 try {
44 barrier.await();
45 } catch (Exception e) {
46 e.printStackTrace();
47 }
48
49 String loggerNamePrefix = this.getClass().getName();
50 for (int i = 0; i < LOOP_LEN; i++) {
51 Logger logger = LoggerFactory.getLogger(loggerNamePrefix + "-" + count + "-" + i);
52 logger.info("in run method");
53 eventCount.getAndIncrement();
54 }
55 }
56 }
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>slf4j-ext</artifactId>
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>slf4j-jcl</artifactId>
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>slf4j-jdk14</artifactId>
4242
4343 public JDK14LoggerFactory() {
4444 loggerMap = new ConcurrentHashMap<String, Logger>();
45 // ensure jul initialization. see also SLF4J-359
46 java.util.logging.LogManager.getLogManager();
45 // ensure jul initialization. see SLF4J-359
46 // note that call to java.util.logging.LogManager.getLogManager() fails on the Google App Engine platform. See SLF4J-363
47 java.util.logging.Logger.getLogger("");
4748 }
4849
4950 /*
0 package org.slf4j.impl;
1
2 import java.util.concurrent.atomic.AtomicLong;
3 import java.util.logging.Handler;
4 import java.util.logging.LogRecord;
5
6 public class CountingHandler extends Handler {
7
8 final AtomicLong eventCount = new AtomicLong(0);
9
10 @Override
11 public void publish(LogRecord record) {
12 eventCount.getAndIncrement();
13 }
14
15 @Override
16 public void flush() {
17 }
18
19 @Override
20 public void close() throws SecurityException {
21 }
22
23 }
0 /**
1 * Copyright (c) 2004-2016 QOS.ch
2 * All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24 package org.slf4j.impl;
25
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.util.Random;
30 import java.util.concurrent.BrokenBarrierException;
31 import java.util.concurrent.CyclicBarrier;
32 import java.util.concurrent.atomic.AtomicLong;
33 import java.util.logging.Handler;
34
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerAccessingThread;
40 import org.slf4j.LoggerFactory;
41
42 public class JDK14MultithreadedInitializationTest {
43
44 final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;
45
46 final private AtomicLong eventCount = new AtomicLong(0);
47 final private CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
48
49 int diff = new Random().nextInt(10000);
50
51 java.util.logging.Logger julRootLogger = java.util.logging.Logger.getLogger("");
52 java.util.logging.Logger julOrgLogger = java.util.logging.Logger.getLogger("org");
53
54 @Before
55 public void addRecordingHandler() {
56 System.out.println("THREAD_COUNT=" + THREAD_COUNT);
57 removeAllHandlers(julRootLogger);
58 removeAllHandlers(julOrgLogger);
59 julOrgLogger.addHandler(new CountingHandler());
60 }
61
62 private void removeAllHandlers(java.util.logging.Logger logger) {
63 Handler[] handlers = logger.getHandlers();
64 for (int i = 0; i < handlers.length; i++) {
65 logger.removeHandler(handlers[i]);
66 }
67 }
68
69 @After
70 public void tearDown() throws Exception {
71 removeAllHandlers(julOrgLogger);
72 }
73
74 @Test
75 public void multiThreadedInitialization() throws InterruptedException, BrokenBarrierException {
76 @SuppressWarnings("unused")
77 LoggerAccessingThread[] accessors = harness();
78
79 Logger logger = LoggerFactory.getLogger(getClass().getName());
80 logger.info("hello");
81 eventCount.getAndIncrement();
82
83 long recordedEventCount = getRecordedEventCount();
84 assertTrue(eventCount.get() + " >= " + recordedEventCount, eventCount.get() >= recordedEventCount);
85 assertTrue(eventCount.get() + " < " + recordedEventCount + "+10", eventCount.get() < recordedEventCount + 10);
86 }
87
88 private long getRecordedEventCount() {
89 CountingHandler ra = findRecordingHandler();
90 if (ra == null) {
91 fail("failed to fing RecordingHandler");
92 }
93 return ra.eventCount.get();
94 }
95
96 private CountingHandler findRecordingHandler() {
97 Handler[] handlers = julOrgLogger.getHandlers();
98 for (Handler h : handlers) {
99 if (h instanceof CountingHandler)
100 return (CountingHandler) h;
101 }
102 return null;
103 }
104
105 private LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
106 LoggerAccessingThread[] threads = new LoggerAccessingThread[THREAD_COUNT];
107 for (int i = 0; i < THREAD_COUNT; i++) {
108 threads[i] = new LoggerAccessingThread(barrier, i, eventCount);
109 threads[i].start();
110 }
111
112 // trigger barrier
113 barrier.await();
114
115 for (int i = 0; i < THREAD_COUNT; i++) {
116 threads[i].join();
117 }
118
119 return threads;
120 }
121
122 }
+0
-153
slf4j-jdk14/src/test/java/org/slf4j/impl/MultithreadedInitializationTest.java less more
0 /**
1 * Copyright (c) 2004-2016 QOS.ch
2 * All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24 package org.slf4j.impl;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.fail;
28
29 import java.util.List;
30 import java.util.Random;
31 import java.util.concurrent.BrokenBarrierException;
32 import java.util.concurrent.CyclicBarrier;
33 import java.util.concurrent.atomic.AtomicLong;
34 import java.util.logging.Handler;
35 import java.util.logging.LogRecord;
36
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class MultithreadedInitializationTest {
44
45 final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;
46
47 private static AtomicLong EVENT_COUNT = new AtomicLong(0);
48
49 final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
50
51 int diff = new Random().nextInt(10000);
52 String packagePrefix = "org.slf4j.impl.MultithreadedInitializationTest" + diff;
53
54 java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger(packagePrefix);
55
56 @Before
57 public void addRecordingHandler() {
58 julLogger.addHandler(new RecordingHandler());
59 }
60
61 @After
62 public void tearDown() throws Exception {
63 Handler[] handlers = julLogger.getHandlers();
64 for (int i = 0; i < handlers.length; i++) {
65 if (handlers[i] instanceof RecordingHandler) {
66 julLogger.removeHandler(handlers[i]);
67 }
68 }
69 }
70
71 @Test
72 public void multiThreadedInitialization() throws InterruptedException, BrokenBarrierException {
73 System.out.println("THREAD_COUNT=" + THREAD_COUNT);
74 LoggerAccessingThread[] accessors = harness();
75
76 for (int i = 0; i < accessors.length; i++) {
77 LoggerAccessingThread accessor = accessors[i];
78 EVENT_COUNT.getAndIncrement();
79 if (accessor.logger == null) {
80 fail("logger for LoggerAccessingThread " + i + " is not set");
81 }
82 accessor.logger.info("post harness");
83 }
84
85 Logger logger = LoggerFactory.getLogger(packagePrefix + ".test");
86 logger.info("hello");
87 EVENT_COUNT.getAndIncrement();
88
89 List<LogRecord> records = getRecordedEvents();
90 assertEquals(EVENT_COUNT.get(), records.size());
91 }
92
93 private List<LogRecord> getRecordedEvents() {
94 RecordingHandler ra = findRecordingHandler();
95 if (ra == null) {
96 fail("failed to fing RecordingHandler");
97 }
98 return ra.records;
99 }
100
101 private RecordingHandler findRecordingHandler() {
102 Handler[] handlers = julLogger.getHandlers();
103 for (Handler h : handlers) {
104 if (h instanceof RecordingHandler)
105 return (RecordingHandler) h;
106 }
107 return null;
108 }
109
110 private LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
111 LoggerAccessingThread[] threads = new LoggerAccessingThread[THREAD_COUNT];
112 final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
113 for (int i = 0; i < THREAD_COUNT; i++) {
114 threads[i] = new LoggerAccessingThread(barrier, i);
115 threads[i].start();
116 }
117
118 // trigger barrier
119 barrier.await();
120
121 for (int i = 0; i < THREAD_COUNT; i++) {
122 threads[i].join();
123 }
124
125 return threads;
126 }
127
128 class LoggerAccessingThread extends Thread {
129 final CyclicBarrier barrier;
130 volatile Logger logger;
131 final int count;
132
133 LoggerAccessingThread(CyclicBarrier barrier, int count) {
134 this.barrier = barrier;
135 this.count = count;
136 }
137
138 public void run() {
139 try {
140 barrier.await();
141 } catch (Exception e) {
142 e.printStackTrace();
143 }
144 for (int i = 0; i < 64; i++) {
145 logger = LoggerFactory.getLogger(packagePrefix + ".LoggerAccessingThread" + count + "-" + i);
146 logger.info("in run method");
147 EVENT_COUNT.getAndIncrement();
148 }
149 }
150 };
151
152 }
+0
-26
slf4j-jdk14/src/test/java/org/slf4j/impl/RecordingHandler.java less more
0 package org.slf4j.impl;
1
2 import java.util.ArrayList;
3 import java.util.Collections;
4 import java.util.List;
5 import java.util.logging.Handler;
6 import java.util.logging.LogRecord;
7
8 public class RecordingHandler extends Handler {
9
10 List<LogRecord> records = Collections.synchronizedList(new ArrayList<LogRecord>());
11
12 @Override
13 public void publish(LogRecord record) {
14 records.add(record);
15 }
16
17 @Override
18 public void flush() {
19 }
20
21 @Override
22 public void close() throws SecurityException {
23 }
24
25 }
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>slf4j-log4j12</artifactId>
2626 <groupId>log4j</groupId>
2727 <artifactId>log4j</artifactId>
2828 </dependency>
29
30 <dependency>
31 <groupId>org.slf4j</groupId>
32 <artifactId>slf4j-api</artifactId>
33 <type>test-jar</type>
34 <version>${project.version}</version>
35 <scope>test</scope>
36 </dependency>
2937 </dependencies>
3038
3139 </project>
0 /**
1 * Copyright (c) 2004-2011 QOS.ch
2 * All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24 package org.slf4j.impl;
25
26 import static org.junit.Assert.assertEquals;
27
28 import java.util.List;
29 import java.util.Random;
30 import java.util.concurrent.BrokenBarrierException;
31 import java.util.concurrent.CyclicBarrier;
32 import java.util.concurrent.atomic.AtomicLong;
33
34 import org.apache.log4j.LogManager;
35 import org.apache.log4j.spi.LoggingEvent;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerAccessingThread;
41 import org.slf4j.LoggerFactory;
42
43 public class Log4j12MultithreadedInitializationTest {
44
45 // value of LogManager.DEFAULT_CONFIGURATION_KEY;
46 static String CONFIG_FILE_KEY = "log4j.configuration";
47
48 final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;
49
50 private final AtomicLong eventCount = new AtomicLong(0);
51
52 private final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
53
54 final int diff = new Random().nextInt(10000);
55 final String loggerName = this.getClass().getName();
56
57 @Before
58 public void setup() {
59 System.out.println("THREAD_COUNT=" + THREAD_COUNT);
60 }
61
62 @After
63 public void tearDown() throws Exception {
64 System.clearProperty(CONFIG_FILE_KEY);
65 }
66
67 @Test
68 public void multiThreadedInitialization() throws InterruptedException, BrokenBarrierException {
69
70 System.setProperty(CONFIG_FILE_KEY, "recursiveInitWithActivationDelay.properties");
71
72 @SuppressWarnings("unused")
73 LoggerAccessingThread[] accessors = harness();
74
75 Logger logger = LoggerFactory.getLogger(loggerName + ".slowInitialization-" + diff);
76 logger.info("hello");
77 eventCount.getAndIncrement();
78
79 List<LoggingEvent> events = getRecordedEvents();
80 int NUM_LINES_BY_RECURSIVE_APPENDER = 3;
81 assertEquals(eventCount.get() + NUM_LINES_BY_RECURSIVE_APPENDER, events.size());
82 }
83
84 private List<LoggingEvent> getRecordedEvents() {
85 org.apache.log4j.Logger root = LogManager.getRootLogger();
86
87 RecursiveAppender ra = (RecursiveAppender) root.getAppender("RECURSIVE");
88 return ra.events;
89 }
90
91 private LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
92 LoggerAccessingThread[] threads = new LoggerAccessingThread[THREAD_COUNT];
93 for (int i = 0; i < THREAD_COUNT; i++) {
94 threads[i] = new LoggerAccessingThread(barrier, i, eventCount);
95 threads[i].start();
96 }
97
98 barrier.await();
99 for (int i = 0; i < THREAD_COUNT; i++) {
100 threads[i].join();
101 }
102 return threads;
103 }
104
105 }
+0
-127
slf4j-log4j12/src/test/java/org/slf4j/impl/MultithreadedInitializationTest.java less more
0 /**
1 * Copyright (c) 2004-2011 QOS.ch
2 * All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24 package org.slf4j.impl;
25
26 import static org.junit.Assert.assertEquals;
27
28 import java.util.List;
29 import java.util.Random;
30 import java.util.concurrent.BrokenBarrierException;
31 import java.util.concurrent.CyclicBarrier;
32 import java.util.concurrent.atomic.AtomicLong;
33
34 import org.apache.log4j.LogManager;
35 import org.apache.log4j.spi.LoggingEvent;
36 import org.junit.After;
37 import org.junit.Test;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class MultithreadedInitializationTest {
42
43 // value of LogManager.DEFAULT_CONFIGURATION_KEY;
44 static String CONFIG_FILE_KEY = "log4j.configuration";
45
46 final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;
47
48 private static AtomicLong EVENT_COUNT = new AtomicLong(0);
49
50 final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
51
52 int diff = new Random().nextInt(10000);
53 String loggerName = "org.slf4j.impl.RecursiveInitializationTest";
54
55 @After
56 public void tearDown() throws Exception {
57 System.clearProperty(CONFIG_FILE_KEY);
58 }
59
60 @Test
61 public void multiThreadedInitialization() throws InterruptedException, BrokenBarrierException {
62 System.out.println("THREAD_COUNT=" + THREAD_COUNT);
63 System.setProperty(CONFIG_FILE_KEY, "recursiveInitWithActivationDelay.properties");
64 LoggerAccessingThread[] accessors = harness();
65
66 for (LoggerAccessingThread accessor : accessors) {
67 EVENT_COUNT.getAndIncrement();
68 accessor.logger.info("post harness");
69 }
70
71 Logger logger = LoggerFactory.getLogger(loggerName + ".slowInitialization-" + diff);
72 logger.info("hello");
73 EVENT_COUNT.getAndIncrement();
74
75 List<LoggingEvent> events = getRecordedEvents();
76 // 3 evetns generated by RecursiveAppender
77 assertEquals(EVENT_COUNT.get() + 3, events.size());
78 }
79
80 private List<LoggingEvent> getRecordedEvents() {
81 org.apache.log4j.Logger root = LogManager.getRootLogger();
82
83 RecursiveAppender ra = (RecursiveAppender) root.getAppender("RECURSIVE");
84 return ra.events;
85 }
86
87 private static LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
88 LoggerAccessingThread[] threads = new LoggerAccessingThread[THREAD_COUNT];
89 final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
90 for (int i = 0; i < THREAD_COUNT; i++) {
91 threads[i] = new LoggerAccessingThread(barrier, i);
92 threads[i].start();
93 }
94
95 barrier.await();
96 for (int i = 0; i < THREAD_COUNT; i++) {
97 threads[i].join();
98 }
99 return threads;
100 }
101
102 static class LoggerAccessingThread extends Thread {
103 final CyclicBarrier barrier;
104 Logger logger;
105 int count;
106
107 LoggerAccessingThread(CyclicBarrier barrier, int count) {
108 this.barrier = barrier;
109 this.count = count;
110 }
111
112 public void run() {
113 try {
114 barrier.await();
115 } catch (Exception e) {
116 e.printStackTrace();
117 }
118 for (int i = 0; i < 64; i++) {
119 logger = LoggerFactory.getLogger(this.getClass().getName() + "-" + count + "-" + i);
120 logger.info("in run method");
121 EVENT_COUNT.getAndIncrement();
122 }
123 }
124 };
125
126 }
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>slf4j-migrator</artifactId>
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>slf4j-nop</artifactId>
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>slf4j-simple</artifactId>
2020 <groupId>org.slf4j</groupId>
2121 <artifactId>slf4j-api</artifactId>
2222 </dependency>
23
24 <dependency>
25 <groupId>org.slf4j</groupId>
26 <artifactId>slf4j-api</artifactId>
27 <type>test-jar</type>
28 <version>${project.version}</version>
29 <scope>test</scope>
30 </dependency>
2331 </dependencies>
2432
2533 </project>
+0
-154
slf4j-simple/src/test/java/org/slf4j/impl/MultithreadedInitializationTest.java less more
0 /**
1 * Copyright (c) 2004-2016 QOS.ch
2 * All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24 package org.slf4j.impl;
25
26 import static org.junit.Assert.assertEquals;
27
28 import java.io.PrintStream;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Random;
33 import java.util.concurrent.BrokenBarrierException;
34 import java.util.concurrent.CyclicBarrier;
35 import java.util.concurrent.atomic.AtomicLong;
36
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.slf4j.LoggerFactoryFriend;
43
44 public class MultithreadedInitializationTest {
45
46 final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;
47
48 private static AtomicLong EVENT_COUNT = new AtomicLong(0);
49
50 private final PrintStream oldErr = System.err;
51 final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
52
53 int diff = new Random().nextInt(10000);
54 String loggerName = "org.slf4j.impl.MultithreadedInitializationTest";
55 StringPrintStream sps = new StringPrintStream(oldErr);
56
57 @Before
58 public void setup() {
59 LoggerFactoryFriend.reset();
60 System.setErr(sps);
61 }
62
63 @After
64 public void tearDown() throws Exception {
65 LoggerFactoryFriend.reset();
66 System.setErr(oldErr);
67 }
68
69 @Test
70 public void multiThreadedInitialization() throws InterruptedException, BrokenBarrierException {
71 System.out.println("THREAD_COUNT=" + THREAD_COUNT);
72 LoggerAccessingThread[] accessors = harness();
73
74 for (LoggerAccessingThread accessor : accessors) {
75 EVENT_COUNT.getAndIncrement();
76 accessor.logger.info("post harness");
77 }
78
79 Logger logger = LoggerFactory.getLogger(loggerName + ".slowInitialization-" + diff);
80 logger.info("hello");
81 EVENT_COUNT.getAndIncrement();
82
83 int NUM_LINES_IN_SLF4J_REPLAY_WARNING = 3;
84 assertEquals(EVENT_COUNT.get() + NUM_LINES_IN_SLF4J_REPLAY_WARNING, sps.stringList.size());
85 }
86
87 private static LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
88 LoggerAccessingThread[] threads = new LoggerAccessingThread[THREAD_COUNT];
89 final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
90 for (int i = 0; i < THREAD_COUNT; i++) {
91 threads[i] = new LoggerAccessingThread(barrier, i);
92 threads[i].start();
93 }
94
95 barrier.await();
96 for (int i = 0; i < THREAD_COUNT; i++) {
97 threads[i].join();
98 }
99 return threads;
100 }
101
102 static class LoggerAccessingThread extends Thread {
103 final CyclicBarrier barrier;
104 Logger logger;
105 int count;
106
107 LoggerAccessingThread(CyclicBarrier barrier, int count) {
108 this.barrier = barrier;
109 this.count = count;
110 }
111
112 public void run() {
113 try {
114 barrier.await();
115 } catch (Exception e) {
116 e.printStackTrace();
117 }
118 for (int i = 0; i < 64; i++) {
119 logger = LoggerFactory.getLogger(this.getClass().getName() + "-" + count+"-"+i);
120 logger.info("in run method");
121 EVENT_COUNT.getAndIncrement();
122 }
123 }
124 };
125
126 public static class StringPrintStream extends PrintStream {
127
128 public static final String LINE_SEP = System.getProperty("line.separator");
129 PrintStream other;
130 List<String> stringList = Collections.synchronizedList(new ArrayList<String>());
131
132 public StringPrintStream(PrintStream ps) {
133 super(ps);
134 other = ps;
135 }
136
137 public void print(String s) {
138 other.print(s);
139 stringList.add(s);
140 }
141
142 public void println(String s) {
143 other.println(s);
144 stringList.add(s);
145 }
146
147 public void println(Object o) {
148 other.println(o);
149 stringList.add(o.toString());
150 }
151 };
152
153 }
0 /**
1 * Copyright (c) 2004-2016 QOS.ch
2 * All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24 package org.slf4j.impl;
25
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.PrintStream;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Random;
33 import java.util.concurrent.BrokenBarrierException;
34 import java.util.concurrent.CyclicBarrier;
35 import java.util.concurrent.atomic.AtomicLong;
36
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerAccessingThread;
42 import org.slf4j.LoggerFactory;
43 import org.slf4j.LoggerFactoryFriend;
44
45 public class SimpleLoggerMultithreadedInitializationTest {
46
47 final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;
48
49 private final AtomicLong eventCount = new AtomicLong(0);
50 private final PrintStream oldErr = System.err;
51 private final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
52
53 final int diff = new Random().nextInt(10000);
54 final String loggerName = this.getClass().getName();
55 StringPrintStream sps = new StringPrintStream(oldErr, true);
56
57 @Before
58 public void setup() {
59 System.out.println("THREAD_COUNT=" + THREAD_COUNT);
60 System.setErr(sps);
61 System.setProperty(SimpleLogger.LOG_FILE_KEY, "System.err");
62 LoggerFactoryFriend.reset();
63 }
64
65 @After
66 public void tearDown() throws Exception {
67 LoggerFactoryFriend.reset();
68 System.clearProperty(SimpleLogger.LOG_FILE_KEY);
69 System.setErr(oldErr);
70 }
71
72 @Test
73 public void multiThreadedInitialization() throws InterruptedException, BrokenBarrierException {
74
75 @SuppressWarnings("unused")
76 LoggerAccessingThread[] accessors = harness();
77
78 Logger logger = LoggerFactory.getLogger(loggerName + diff);
79 logger.info("hello");
80 eventCount.getAndIncrement();
81
82 int NUM_LINES_IN_SLF4J_REPLAY_WARNING = 3;
83
84 long expected = eventCount.get() + NUM_LINES_IN_SLF4J_REPLAY_WARNING;
85 int actual = sps.stringList.size();
86 assertTrue(expected + " >= " + actual, expected >= actual);
87 assertTrue(expected + " < " + actual + " + 16", expected < actual + 16);
88 }
89
90 private LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
91 final LoggerAccessingThread[] threads = new LoggerAccessingThread[THREAD_COUNT];
92 for (int i = 0; i < THREAD_COUNT; i++) {
93 LoggerAccessingThread simpleLoggerThread = new LoggerAccessingThread(barrier, i, eventCount);
94 threads[i] = simpleLoggerThread;
95 simpleLoggerThread.start();
96 }
97
98 barrier.await();
99 for (int i = 0; i < THREAD_COUNT; i++) {
100 threads[i].join();
101 }
102 return threads;
103 }
104
105
106 static class StringPrintStream extends PrintStream {
107
108 public static final String LINE_SEP = System.getProperty("line.separator");
109 PrintStream other;
110 boolean duplicate = false;
111
112 List<String> stringList = Collections.synchronizedList(new ArrayList<String>());
113
114 public StringPrintStream(PrintStream ps, boolean duplicate) {
115 super(ps);
116 other = ps;
117 this.duplicate = duplicate;
118 }
119
120 public StringPrintStream(PrintStream ps) {
121 this(ps, false);
122 }
123
124 public void print(String s) {
125 if (duplicate)
126 other.print(s);
127 stringList.add(s);
128 }
129
130 public void println(String s) {
131 if (duplicate)
132 other.println(s);
133 stringList.add(s);
134 }
135
136 public void println(Object o) {
137 if (duplicate)
138 other.println(o);
139 stringList.add(o.toString());
140 }
141 };
142
143 }
66 <parent>
77 <groupId>org.slf4j</groupId>
88 <artifactId>slf4j-parent</artifactId>
9 <version>1.7.19</version>
9 <version>1.7.20</version>
1010 </parent>
1111
1212 <artifactId>slf4j-site</artifactId>