|
| 1 | +package org.tron.common; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | + |
| 8 | +import ch.qos.logback.classic.Level; |
| 9 | +import ch.qos.logback.classic.Logger; |
| 10 | +import ch.qos.logback.classic.LoggerContext; |
| 11 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 12 | +import ch.qos.logback.classic.spi.LoggingEvent; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | +import org.tron.common.log.layout.MultiLayoutPattern; |
| 17 | + |
| 18 | +public class MultiLayoutPatternTest { |
| 19 | + |
| 20 | + private MultiLayoutPattern multiLayoutPattern; |
| 21 | + private LoggerContext context; |
| 22 | + |
| 23 | + @Before |
| 24 | + public void setUp() { |
| 25 | + context = new LoggerContext(); |
| 26 | + multiLayoutPattern = new MultiLayoutPattern(); |
| 27 | + multiLayoutPattern.setContext(context); |
| 28 | + |
| 29 | + MultiLayoutPattern.Rule rule1 = new MultiLayoutPattern.Rule(); |
| 30 | + rule1.setLogger("com.example.app1"); |
| 31 | + assertNotNull(rule1.getLogger()); |
| 32 | + rule1.setPattern("%date [%thread] %-5level %logger{36} - %msg%n"); |
| 33 | + assertNotNull(rule1.getPattern()); |
| 34 | + rule1.setOutputPatternAsHeader(true); |
| 35 | + assertTrue(rule1.isOutputPatternAsHeader()); |
| 36 | + multiLayoutPattern.addRule(rule1); |
| 37 | + |
| 38 | + MultiLayoutPattern.Rule rule2 = new MultiLayoutPattern.Rule(); |
| 39 | + rule2.setLogger("com.example.app2"); |
| 40 | + rule2.setPattern("%msg%n"); |
| 41 | + multiLayoutPattern.addRule(rule2); |
| 42 | + |
| 43 | + multiLayoutPattern.start(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testEncodeForSpecificLogger() { |
| 48 | + ILoggingEvent event1 = createLoggingEvent("com.example.app1", "Test message 1"); |
| 49 | + byte[] encoded1 = multiLayoutPattern.encode(event1); |
| 50 | + String result1 = new String(encoded1); |
| 51 | + assertTrue(result1.contains("Test message 1")); |
| 52 | + |
| 53 | + ILoggingEvent event2 = createLoggingEvent("com.example.app2", "Test message 2"); |
| 54 | + byte[] encoded2 = multiLayoutPattern.encode(event2); |
| 55 | + String result2 = new String(encoded2); |
| 56 | + assertEquals("Test message 2\n", result2); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testEncodeForRootLogger() { |
| 61 | + ILoggingEvent event = createLoggingEvent(Logger.ROOT_LOGGER_NAME, "Root logger message"); |
| 62 | + byte[] encoded = multiLayoutPattern.encode(event); |
| 63 | + String result = new String(encoded); |
| 64 | + assertFalse(result.contains("Root logger message")); |
| 65 | + } |
| 66 | + |
| 67 | + private ILoggingEvent createLoggingEvent(String loggerName, String message) { |
| 68 | + Logger logger = (Logger) LoggerFactory.getLogger(loggerName); |
| 69 | + return new LoggingEvent(loggerName, logger, Level.INFO, message, null, null); |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments