Skip to content

Commit

Permalink
Added forwarding of parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
SlyngDK committed Aug 28, 2020
1 parent 27e02fa commit 1204d43
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
64 changes: 36 additions & 28 deletions src/main/java/org/jboss/slf4j/JBossLoggerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ public void log(Marker marker, String fqcn, int level, String message, Object[]
FormattingTuple result = MessageFormatter.arrayFormat(message, argArray);
switch (level) {
case LocationAwareLogger.TRACE_INT:
logger.trace(fqcn, result.getMessage(), t);
logger.trace(fqcn, result.getMessage(), argArray, t);
break;

case LocationAwareLogger.DEBUG_INT:
logger.debug(fqcn, result.getMessage(), t);
logger.debug(fqcn, result.getMessage(), argArray, t);
break;

case LocationAwareLogger.INFO_INT:
logger.info(fqcn, result.getMessage(), t);
logger.info(fqcn, result.getMessage(), argArray, t);
break;

case LocationAwareLogger.WARN_INT:
logger.warn(fqcn, result.getMessage(), t);
logger.warn(fqcn, result.getMessage(), argArray, t);
break;

case LocationAwareLogger.ERROR_INT:
logger.error(fqcn, result.getMessage(), t);
logger.error(fqcn, result.getMessage(), argArray, t);
break;

default:
Expand All @@ -88,30 +88,30 @@ public boolean isTraceEnabled() {

@Override
public void trace(final String msg) {
log(Level.TRACE, LOGGER_FQCN, msg, null);
log(Level.TRACE, LOGGER_FQCN, msg);
}

@Override
public void trace(final String format, final Object arg) {
if (logger.isTraceEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.format(format, arg);
log(Level.TRACE, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.TRACE, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arg);
}
}

@Override
public void trace(final String format, final Object arg1, final Object arg2) {
if (logger.isTraceEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.format(format, arg1, arg2);
log(Level.TRACE, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.TRACE, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arg1, arg2);
}
}

@Override
public void trace(final String format, final Object... arguments) {
if (logger.isTraceEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.arrayFormat(format, arguments);
log(Level.TRACE, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.TRACE, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arguments);
}
}

Expand All @@ -129,32 +129,32 @@ public boolean isDebugEnabled() {

@Override
public void debug(final String msg) {
if (logger.isDebugEnabled()) {
log(Level.DEBUG, LOGGER_FQCN, msg, null);
}
if (logger.isDebugEnabled()) {
log(Level.DEBUG, LOGGER_FQCN, msg);
}
}

@Override
public void debug(final String format, final Object arg) {
if (logger.isDebugEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.format(format, arg);
log(Level.DEBUG, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.DEBUG, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arg);
}
}

@Override
public void debug(final String format, final Object arg1, final Object arg2) {
if (logger.isDebugEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.format(format, arg1, arg2);
log(Level.DEBUG, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.DEBUG, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arg1, arg2);
}
}

@Override
public void debug(final String format, final Object... arguments) {
if (logger.isDebugEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.arrayFormat(format, arguments);
log(Level.DEBUG, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.DEBUG, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arguments);
}
}

Expand All @@ -173,31 +173,31 @@ public boolean isInfoEnabled() {
@Override
public void info(final String msg) {
if (logger.isInfoEnabled()) {
log(Level.INFO, LOGGER_FQCN, msg, null);
log(Level.INFO, LOGGER_FQCN, msg);
}
}

@Override
public void info(final String format, final Object arg) {
if (logger.isInfoEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.format(format, arg);
log(Level.INFO, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.INFO, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arg);
}
}

@Override
public void info(final String format, final Object arg1, final Object arg2) {
if (logger.isInfoEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.format(format, arg1, arg2);
log(Level.INFO, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.INFO, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arg1, arg2);
}
}

@Override
public void info(final String format, final Object... arguments) {
if (logger.isInfoEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.arrayFormat(format, arguments);
log(Level.INFO, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.INFO, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arguments);
}
}

Expand All @@ -216,31 +216,31 @@ public boolean isWarnEnabled() {
@Override
public void warn(final String msg) {
if (isWarnEnabled()) {
log(Level.WARN, LOGGER_FQCN, msg, null);
log(Level.WARN, LOGGER_FQCN, msg);
}
}

@Override
public void warn(final String format, final Object arg) {
if (isWarnEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.format(format, arg);
log(Level.WARN, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.WARN, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arg);
}
}

@Override
public void warn(final String format, final Object... arguments) {
if (isWarnEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.arrayFormat(format, arguments);
log(Level.WARN, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.WARN, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arguments);
}
}

@Override
public void warn(final String format, final Object arg1, final Object arg2) {
if (isWarnEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.format(format, arg1, arg2);
log(Level.WARN, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.WARN, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arg1, arg2);
}
}

Expand All @@ -259,31 +259,31 @@ public boolean isErrorEnabled() {
@Override
public void error(final String msg) {
if (isErrorEnabled()) {
log(Level.ERROR, LOGGER_FQCN, msg, null);
log(Level.ERROR, LOGGER_FQCN, msg);
}
}

@Override
public void error(final String format, final Object arg) {
if (isErrorEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.format(format, arg);
log(Level.ERROR, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.ERROR, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arg);
}
}

@Override
public void error(final String format, final Object arg1, final Object arg2) {
if (isErrorEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.format(format, arg1, arg2);
log(Level.ERROR, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.ERROR, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arg1, arg2);
}
}

@Override
public void error(final String format, final Object... arguments) {
if (isErrorEnabled()) {
final FormattingTuple formattingTuple = MessageFormatter.arrayFormat(format, arguments);
log(Level.ERROR, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable());
log(Level.ERROR, LOGGER_FQCN, formattingTuple.getMessage(), formattingTuple.getThrowable(), arguments);
}
}

Expand All @@ -294,7 +294,15 @@ public void error(final String msg, final Throwable t) {
}
}

private void log(final org.jboss.logging.Logger.Level level, final String fqcn, final Object message) {
log(level, fqcn, message, null);
}

private void log(final org.jboss.logging.Logger.Level level, final String fqcn, final Object message, final Throwable t) {
logger.log(level, fqcn, message, t);
logger.log(fqcn, level, message, null, t);
}

private void log(final org.jboss.logging.Logger.Level level, final String fqcn, final Object message, final Throwable t, Object... args) {
logger.log(fqcn, level, message, args, t);
}
}
4 changes: 4 additions & 0 deletions src/test/java/org/slf4j/LoggerTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNull;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -87,12 +89,14 @@ public void testLogger() {
LogRecord record = HANDLER.messages.poll();
assertNotNull(record);
assertEquals("Expected message not found.", testMsg, record.getMessage());
assertNull("Expected parameters to be null, when no args.", record.getParameters());

// Test a formatted message
logger.info("This is a test formatted {}", "message");
record = HANDLER.messages.poll();
assertNotNull(record);
assertEquals("Expected formatted message not found.", "This is a test formatted message", record.getMessage());
assertArrayEquals("Expected parameter not found.", new Object[]{"message"}, record.getParameters());
}

@Test
Expand Down

0 comments on commit 1204d43

Please sign in to comment.