Skip to content

Commit

Permalink
Merge pull request #272 from jamezp/LOGMGR-259
Browse files Browse the repository at this point in the history
[LOGMGR-259] The current parsed token should be the name of the named…
  • Loading branch information
jamezp authored Aug 1, 2019
2 parents f131e68 + a859c5c commit c8a639f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -664,14 +664,13 @@ public Object getObject() {
expect(")", iterator);
return new SimpleObjectProducer(new SubstituteFilter(pattern, replacement, true));
} else {
final String name = expectName(iterator);
if (! filters.containsKey(name) || immediate && ! filterRefs.containsKey(name)) {
throw new IllegalArgumentException(String.format("No filter named \"%s\" is defined", name));
if (! filters.containsKey(token) || immediate && ! filterRefs.containsKey(token)) {
throw new IllegalArgumentException(String.format("No filter named \"%s\" is defined", token));
}
if (immediate) {
return new SimpleObjectProducer(filterRefs.get(name));
return new SimpleObjectProducer(filterRefs.get(token));
} else {
return new RefProducer(name, filterRefs);
return new RefProducer(token, filterRefs);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Filter;
import java.util.logging.LogRecord;

import org.jboss.logmanager.ExtLogRecord;
import org.jboss.logmanager.Level;
Expand Down Expand Up @@ -74,6 +75,51 @@ public void testFilterParsing() throws Exception {
}
}

@Test
public void testNamedFilter() {
final LogContext context = LogContext.create();
final LogContextConfiguration configuration = LogContextConfiguration.Factory.create(context);
final LoggerConfiguration loggerConfiguration = configuration.addLoggerConfiguration(LOGGER_NAME);
final Logger logger = context.getLogger(LOGGER_NAME);

final FilterDescription description = new FilterDescription("test", "test named filter",
"test named filter | filtered", true);

configuration.addFilterConfiguration(null, TestFilter.class.getName(), "test");
loggerConfiguration.setFilter(description.filterExpression);
configuration.commit();

final ExtLogRecord record = create(description.logMessage);

final Filter filter = logger.getFilter();
Assert.assertNotNull("Expected a filter on the logger, but one was not found: " + description, filter);
Assert.assertEquals("Filter.isLoggable() test failed: " + description, description.isLoggable, filter.isLoggable(record));
final String msg = record.getFormattedMessage();
Assert.assertEquals(String.format("Expected %s found %s: %n%s", description.expectedMessage, msg, description), description.expectedMessage, msg);
}

@Test
public void testEmbeddedNamedFilter() {
final LogContext context = LogContext.create();
final LogContextConfiguration configuration = LogContextConfiguration.Factory.create(context);
final LoggerConfiguration loggerConfiguration = configuration.addLoggerConfiguration(LOGGER_NAME);
final Logger logger = context.getLogger(LOGGER_NAME);

final FilterDescription description = new FilterDescription("all(test)", "test named filter",
"test named filter | filtered", true);
configuration.addFilterConfiguration(null, TestFilter.class.getName(), "test");
loggerConfiguration.setFilter(description.filterExpression);
configuration.commit();

final ExtLogRecord record = create(description.logMessage);

final Filter filter = logger.getFilter();
Assert.assertNotNull("Expected a filter on the logger, but one was not found: " + description, filter);
Assert.assertEquals("Filter.isLoggable() test failed: " + description, description.isLoggable, filter.isLoggable(record));
final String msg = record.getFormattedMessage();
Assert.assertEquals(String.format("Expected %s found %s: %n%s", description.expectedMessage, msg, description), description.expectedMessage, msg);
}

private static ExtLogRecord create(final String message) {
final ExtLogRecord record = new ExtLogRecord(Level.INFO, message, Logger.class.getName());
record.setLoggerName(LOGGER_NAME);
Expand Down Expand Up @@ -105,4 +151,13 @@ public String toString() {
", isLoggable=" + isLoggable + ")";
}
}

public static class TestFilter implements Filter {

@Override
public boolean isLoggable(final LogRecord record) {
record.setMessage(record.getMessage() + " | filtered");
return true;
}
}
}

0 comments on commit c8a639f

Please sign in to comment.