|
22 | 22 | import java.util.ArrayList;
|
23 | 23 | import java.util.Collection;
|
24 | 24 | import java.util.logging.Filter;
|
| 25 | +import java.util.logging.LogRecord; |
25 | 26 |
|
26 | 27 | import org.jboss.logmanager.ExtLogRecord;
|
27 | 28 | import org.jboss.logmanager.Level;
|
@@ -74,6 +75,51 @@ public void testFilterParsing() throws Exception {
|
74 | 75 | }
|
75 | 76 | }
|
76 | 77 |
|
| 78 | + @Test |
| 79 | + public void testNamedFilter() { |
| 80 | + final LogContext context = LogContext.create(); |
| 81 | + final LogContextConfiguration configuration = LogContextConfiguration.Factory.create(context); |
| 82 | + final LoggerConfiguration loggerConfiguration = configuration.addLoggerConfiguration(LOGGER_NAME); |
| 83 | + final Logger logger = context.getLogger(LOGGER_NAME); |
| 84 | + |
| 85 | + final FilterDescription description = new FilterDescription("test", "test named filter", |
| 86 | + "test named filter | filtered", true); |
| 87 | + |
| 88 | + configuration.addFilterConfiguration(null, TestFilter.class.getName(), "test"); |
| 89 | + loggerConfiguration.setFilter(description.filterExpression); |
| 90 | + configuration.commit(); |
| 91 | + |
| 92 | + final ExtLogRecord record = create(description.logMessage); |
| 93 | + |
| 94 | + final Filter filter = logger.getFilter(); |
| 95 | + Assert.assertNotNull("Expected a filter on the logger, but one was not found: " + description, filter); |
| 96 | + Assert.assertEquals("Filter.isLoggable() test failed: " + description, description.isLoggable, filter.isLoggable(record)); |
| 97 | + final String msg = record.getFormattedMessage(); |
| 98 | + Assert.assertEquals(String.format("Expected %s found %s: %n%s", description.expectedMessage, msg, description), description.expectedMessage, msg); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testEmbeddedNamedFilter() { |
| 103 | + final LogContext context = LogContext.create(); |
| 104 | + final LogContextConfiguration configuration = LogContextConfiguration.Factory.create(context); |
| 105 | + final LoggerConfiguration loggerConfiguration = configuration.addLoggerConfiguration(LOGGER_NAME); |
| 106 | + final Logger logger = context.getLogger(LOGGER_NAME); |
| 107 | + |
| 108 | + final FilterDescription description = new FilterDescription("all(test)", "test named filter", |
| 109 | + "test named filter | filtered", true); |
| 110 | + configuration.addFilterConfiguration(null, TestFilter.class.getName(), "test"); |
| 111 | + loggerConfiguration.setFilter(description.filterExpression); |
| 112 | + configuration.commit(); |
| 113 | + |
| 114 | + final ExtLogRecord record = create(description.logMessage); |
| 115 | + |
| 116 | + final Filter filter = logger.getFilter(); |
| 117 | + Assert.assertNotNull("Expected a filter on the logger, but one was not found: " + description, filter); |
| 118 | + Assert.assertEquals("Filter.isLoggable() test failed: " + description, description.isLoggable, filter.isLoggable(record)); |
| 119 | + final String msg = record.getFormattedMessage(); |
| 120 | + Assert.assertEquals(String.format("Expected %s found %s: %n%s", description.expectedMessage, msg, description), description.expectedMessage, msg); |
| 121 | + } |
| 122 | + |
77 | 123 | private static ExtLogRecord create(final String message) {
|
78 | 124 | final ExtLogRecord record = new ExtLogRecord(Level.INFO, message, Logger.class.getName());
|
79 | 125 | record.setLoggerName(LOGGER_NAME);
|
@@ -105,4 +151,13 @@ public String toString() {
|
105 | 151 | ", isLoggable=" + isLoggable + ")";
|
106 | 152 | }
|
107 | 153 | }
|
| 154 | + |
| 155 | + public static class TestFilter implements Filter { |
| 156 | + |
| 157 | + @Override |
| 158 | + public boolean isLoggable(final LogRecord record) { |
| 159 | + record.setMessage(record.getMessage() + " | filtered"); |
| 160 | + return true; |
| 161 | + } |
| 162 | + } |
108 | 163 | }
|
0 commit comments