-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
General clean up of implementation. Added more tests and README.
- Loading branch information
Showing
18 changed files
with
935 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
= A log4j2 implementation for JBoss Log Manager | ||
|
||
An implementation of the log4j-api for the jboss-logmanager. | ||
|
||
== Usage | ||
|
||
[source,xml] | ||
---- | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-api</artifactId> | ||
<version>${version.org.apache.logging.log4j}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.logmanager</groupId> | ||
<artifactId>log4j2-jboss-logmanager</artifactId> | ||
<version>${version.org.jboss.logmanager.log4j2-jboss-logmanager}</version> | ||
</dependency> | ||
---- | ||
|
||
|
||
== Building | ||
|
||
---- | ||
mvn clean install | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,12 @@ | |
|
||
package org.jboss.logmanager.log4j; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
import org.apache.logging.log4j.message.MessageFactory; | ||
import org.apache.logging.log4j.spi.AbstractLogger; | ||
import org.apache.logging.log4j.spi.ExtendedLogger; | ||
import org.apache.logging.log4j.spi.LoggerContext; | ||
import org.jboss.logmanager.LogContext; | ||
|
@@ -31,8 +35,9 @@ | |
* | ||
* @author <a href="mailto:[email protected]">James R. Perkins</a> | ||
*/ | ||
@SuppressWarnings({"Convert2Lambda", "SynchronizationOnLocalVariableOrMethodParameter"}) | ||
class JBossLoggerContext implements LoggerContext { | ||
private static final Logger.AttachmentKey<ExtendedLogger> LOGGER_KEY = new Logger.AttachmentKey<>(); | ||
private static final Logger.AttachmentKey<Map<Key, ExtendedLogger>> LOGGER_KEY = new Logger.AttachmentKey<>(); | ||
private final LogContext logContext; | ||
private final Object externalContext; | ||
|
||
|
@@ -54,32 +59,12 @@ public Object getExternalContext() { | |
|
||
@Override | ||
public ExtendedLogger getLogger(final String name) { | ||
final LogContext context = logContext; | ||
ExtendedLogger result = context.getAttachment(name, LOGGER_KEY); | ||
if (result == null) { | ||
final Logger lmLogger = context.getLogger(name); | ||
result = new JBossLogger(lmLogger); | ||
final ExtendedLogger appearing = lmLogger.attachIfAbsent(LOGGER_KEY, result); | ||
if (appearing != null) { | ||
result = appearing; | ||
} | ||
} | ||
return result; | ||
return getOrCreateLogger(new Key(name, null)); | ||
} | ||
|
||
@Override | ||
public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) { | ||
final LogContext context = logContext; | ||
ExtendedLogger result = context.getAttachment(name, LOGGER_KEY); | ||
if (result == null) { | ||
final Logger lmLogger = context.getLogger(name); | ||
result = new JBossLogger(lmLogger, messageFactory); | ||
final ExtendedLogger appearing = lmLogger.attachIfAbsent(LOGGER_KEY, result); | ||
if (appearing != null) { | ||
result = appearing; | ||
} | ||
} | ||
return result; | ||
return getOrCreateLogger(new Key(name, messageFactory)); | ||
} | ||
|
||
@Override | ||
|
@@ -89,14 +74,101 @@ public boolean hasLogger(final String name) { | |
|
||
@Override | ||
public boolean hasLogger(final String name, final MessageFactory messageFactory) { | ||
return messageFactory == null ? | ||
hasLogger(name, AbstractLogger.DEFAULT_MESSAGE_FACTORY_CLASS) : | ||
hasLogger(name, messageFactory.getClass()); | ||
return hasLogger(name, messageFactory == null ? null : messageFactory.getClass()); | ||
} | ||
|
||
@Override | ||
public boolean hasLogger(final String name, final Class<? extends MessageFactory> messageFactoryClass) { | ||
final ExtendedLogger logger = logContext.getAttachment(name, LOGGER_KEY); | ||
return logger != null && logger.getMessageFactory().getClass() == messageFactoryClass; | ||
final Map<Key, ExtendedLogger> loggers = logContext.getAttachment(name, LOGGER_KEY); | ||
if (loggers == null) { | ||
return false; | ||
} | ||
synchronized (loggers) { | ||
for (Key key : loggers.keySet()) { | ||
if (messageFactoryClass == null && key.messageFactory == null) { | ||
return true; | ||
} else if (key.messageFactory != null && key.messageFactory.getClass() == messageFactoryClass) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private ExtendedLogger getOrCreateLogger(final Key key) { | ||
final Map<Key, ExtendedLogger> loggers = getLoggers(logContext, key.name); | ||
synchronized (loggers) { | ||
return loggers.computeIfAbsent(key, new Function<Key, ExtendedLogger>() { | ||
@Override | ||
public ExtendedLogger apply(final Key key) { | ||
if (key.messageFactory == null) { | ||
return new JBossLogger(logContext.getLogger(key.name)); | ||
} | ||
return new JBossLogger(logContext.getLogger(key.name), key.messageFactory); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(logContext, externalContext); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object obj) { | ||
if (obj == this) { | ||
return true; | ||
} | ||
if (!(obj instanceof JBossLoggerContext)) { | ||
return false; | ||
} | ||
final JBossLoggerContext other = (JBossLoggerContext) obj; | ||
return Objects.equals(logContext, other.logContext) && Objects.equals(externalContext, other.externalContext); | ||
} | ||
|
||
private static Map<Key, ExtendedLogger> getLoggers(final LogContext context, final String name) { | ||
Map<Key, ExtendedLogger> result = context.getAttachment(name, LOGGER_KEY); | ||
if (result == null) { | ||
final Logger lmLogger = context.getLogger(name); | ||
result = new HashMap<>(); | ||
final Map<Key, ExtendedLogger> appearing = lmLogger.attachIfAbsent(LOGGER_KEY, result); | ||
if (appearing != null) { | ||
result = appearing; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private static class Key { | ||
final String name; | ||
final MessageFactory messageFactory; | ||
|
||
private Key(final String name, final MessageFactory messageFactory) { | ||
this.name = name; | ||
this.messageFactory = messageFactory; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name, messageFactory); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object obj) { | ||
if (obj == this) { | ||
return true; | ||
} | ||
if (!(obj instanceof Key)) { | ||
return false; | ||
} | ||
final Key other = (Key) obj; | ||
return Objects.equals(name, other.name) && Objects.equals(messageFactory, other.messageFactory); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Key(name=" + name + ", messageFactory=" + messageFactory + ")"; | ||
} | ||
} | ||
} |
Oops, something went wrong.