Skip to content

Commit

Permalink
ongoing work, does not compile
Browse files Browse the repository at this point in the history
  • Loading branch information
ceki committed Mar 31, 2016
1 parent 29e7da0 commit 81b2376
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
17 changes: 15 additions & 2 deletions slf4j-api/src/main/java/org/slf4j/LoggerFactory.java
Expand Up @@ -183,6 +183,20 @@ static void failedBinding(Throwable t) {
Util.report("Failed to instantiate SLF4J LoggerFactory", t);
}

private final static void fixSubstitutedLoggers() {
List<SubstituteLogger> loggers = SUBST_FACTORY.getLoggers();

if (loggers.isEmpty()) {
return;
}

for (SubstituteLogger subLogger : loggers) {
Logger logger = getLogger(subLogger.getName());
subLogger.setDelegate(logger);
}

SUBST_FACTORY.clear();
}
private static void replayEvents() {
final LinkedBlockingQueue<SubstituteLoggingEvent> queue = SUBST_FACTORY.getEventQueue();
final int queueSize = queue.size();
Expand Down Expand Up @@ -219,8 +233,7 @@ private static void replaySingleEvent(SubstituteLoggingEvent event) {
SubstituteLogger substLogger = event.getLogger();
String loggerName = substLogger.getName();
if (substLogger.isDelegateNull()) {
Logger logger = getLogger(loggerName);
substLogger.setDelegate(logger);
throw new IllegalStateException("Delegate logger cannot be null at this state.");
}

if (substLogger.isDelegateNOP()) {
Expand Down
14 changes: 12 additions & 2 deletions slf4j-api/src/main/java/org/slf4j/helpers/SubstituteLogger.java
Expand Up @@ -53,9 +53,12 @@ public class SubstituteLogger implements Logger {
private EventRecodingLogger eventRecodingLogger;
private Queue<SubstituteLoggingEvent> eventQueue;

public SubstituteLogger(String name, Queue<SubstituteLoggingEvent> eventQueue) {
private final boolean createdPostInitialization;

public SubstituteLogger(String name, Queue<SubstituteLoggingEvent> eventQueue, boolean createdPostInitialization) {
this.name = name;
this.eventQueue = eventQueue;
this.createdPostInitialization = createdPostInitialization;
}

public String getName() {
Expand Down Expand Up @@ -327,7 +330,14 @@ public int hashCode() {
* instance.
*/
Logger delegate() {
return _delegate != null ? _delegate : getEventRecordingLogger();
if(_delegate != null) {
return _delegate;
}
if(createdPostInitialization) {
return NOPLogger.NOP_LOGGER;
} else {
return getEventRecordingLogger();
}
}

private Logger getEventRecordingLogger() {
Expand Down
Expand Up @@ -42,6 +42,8 @@
*/
public class SubstituteLoggerFactory implements ILoggerFactory {

boolean postInitialization = false;

final ConcurrentMap<String, SubstituteLogger> loggers = new ConcurrentHashMap<String, SubstituteLogger>();

final LinkedBlockingQueue<SubstituteLoggingEvent> eventQueue = new LinkedBlockingQueue<SubstituteLoggingEvent>();
Expand Down
Expand Up @@ -50,6 +50,7 @@ public void run() {
String loggerNamePrefix = this.getClass().getName();
for (int i = 0; i < LOOP_LEN; i++) {
Logger logger = LoggerFactory.getLogger(loggerNamePrefix + "-" + count + "-" + i);
Thread.yield();
logger.info("in run method");
eventCount.getAndIncrement();
}
Expand Down
Expand Up @@ -24,7 +24,7 @@
*/
package org.slf4j.impl;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.Random;
Expand Down Expand Up @@ -82,8 +82,7 @@ public void multiThreadedInitialization() throws InterruptedException, BrokenBar
eventCount.getAndIncrement();

long recordedEventCount = getRecordedEventCount();
assertTrue(eventCount.get() + " >= " + recordedEventCount, eventCount.get() >= recordedEventCount);
assertTrue(eventCount.get() + " < " + recordedEventCount + "+10", eventCount.get() < recordedEventCount + 10);
assertEquals(eventCount.get(), recordedEventCount);
}

private long getRecordedEventCount() {
Expand Down
Expand Up @@ -24,7 +24,7 @@
*/
package org.slf4j.impl;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;

import java.io.PrintStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -84,8 +84,7 @@ public void multiThreadedInitialization() throws InterruptedException, BrokenBar

long expected = eventCount.get() + NUM_LINES_IN_SLF4J_REPLAY_WARNING;
int actual = sps.stringList.size();
assertTrue(expected + " >= " + actual, expected >= actual);
assertTrue(expected + " < " + actual + " + 16", expected < actual + 16);
assertEquals(expected, actual);
}

private LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
Expand Down
9 changes: 9 additions & 0 deletions slf4j-site/src/site/pages/news.html
Expand Up @@ -33,6 +33,15 @@ <h1>SLF4J News</h1>

<hr noshade="noshade" size="1"/>

<h3>xxx April, 2016 - Release of SLF4J 1.7.21</h3>

<p><code>LoggerFactory</code> makes sure to release all resources
referenced by SubstituteLoggerFactory post-initialization. See also
<a href="http://jira.qos.ch/browse/SLF4J-366">SLF4J-366</a>
</p>

<hr noshade="noshade" size="1"/>

<h3>March 29th, 2016 - Release of SLF4J 1.7.20</h3>

<p>Fixed initialization problem encountered on the Google App
Expand Down

0 comments on commit 81b2376

Please sign in to comment.