org.apache.logging.log4j.MarkerManager Java Examples
The following examples show how to use
org.apache.logging.log4j.MarkerManager.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MarkerTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testAddMarker() { final String childMakerName = CHILD_MAKER_NAME + "-AM"; final String parentMarkerName = PARENT_MARKER_NAME + "-AM"; final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMakerName); final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMarkerName); slf4jMarker.add(slf4jParent); final Marker log4jParent = MarkerManager.getMarker(parentMarkerName); final Marker log4jMarker = MarkerManager.getMarker(childMakerName); assertTrue("Incorrect Marker class", slf4jMarker instanceof Log4jMarker); assertTrue(String.format("%s (log4jMarker=%s) is not an instance of %s (log4jParent=%s) in Log4j", childMakerName, parentMarkerName, log4jMarker, log4jParent), log4jMarker.isInstanceOf(log4jParent)); assertTrue(String.format("%s (slf4jMarker=%s) is not an instance of %s (log4jParent=%s) in SLF4J", childMakerName, parentMarkerName, slf4jMarker, slf4jParent), slf4jMarker.contains(slf4jParent)); }
Example #2
Source File: MarkerAttributeConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testConvert02() { Marker marker = MarkerManager.getMarker("anotherConvert02").setParents(MarkerManager.getMarker("finalConvert03")); marker = MarkerManager.getMarker("testConvert02").setParents(marker); final String converted = this.converter.convertToDatabaseColumn(marker); assertNotNull("The converted value should not be null.", converted); assertEquals("The converted value is not correct.", "testConvert02[ anotherConvert02[ finalConvert03 ] ]", converted); final Marker reversed = this.converter.convertToEntityAttribute(converted); assertNotNull("The reversed value should not be null.", reversed); assertEquals("The reversed value is not correct.", "testConvert02", marker.getName()); final Marker[] parents = marker.getParents(); assertNotNull("The first parent should not be null.", parents); assertNotNull("The first parent should not be null.", parents[0]); assertEquals("The first parent is not correct.", "anotherConvert02", parents[0].getName()); assertNotNull("The second parent should not be null.", parents[0].getParents()); assertNotNull("The second parent should not be null.", parents[0].getParents()[0]); assertEquals("The second parent is not correct.", "finalConvert03", parents[0].getParents()[0].getName()); }
Example #3
Source File: MarkerAttributeConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testConvert01() { final Marker marker = MarkerManager.getMarker("testConvert01"); final String converted = this.converter.convertToDatabaseColumn(marker); assertNotNull("The converted value should not be null.", converted); assertEquals("The converted value is not correct.", "testConvert01", converted); final Marker reversed = this.converter.convertToEntityAttribute(converted); assertNotNull("The reversed value should not be null.", reversed); assertEquals("The reversed value is not correct.", "testConvert01", marker.getName()); }
Example #4
Source File: PatternParserTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
private void testNestedPatternHighlight(final Level level, final String expectedStart) { final List<PatternFormatter> formatters = parser.parse(nestedPatternHighlight); assertNotNull(formatters); final Throwable t = new Throwable(); t.getStackTrace(); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("org.apache.logging.log4j.PatternParserTest") // .setMarker(MarkerManager.getMarker("TEST")) // .setLoggerFqcn(Logger.class.getName()) // .setLevel(level) // .setMessage(new SimpleMessage("Hello, world")) // .setThreadName("Thread1") // .setSource(/*stackTraceElement[0]*/ null) // .setTimeMillis(System.currentTimeMillis()) // .build(); final StringBuilder buf = new StringBuilder(); for (final PatternFormatter formatter : formatters) { formatter.format(event, buf); } final String str = buf.toString(); final String expectedEnd = String.format("] %-5s: Hello, world%s\u001B[m", level, Strings.LINE_SEPARATOR); assertTrue("Expected to start with: " + expectedStart + ". Actual: " + str, str.startsWith(expectedStart)); assertTrue("Expected to end with: \"" + expectedEnd + "\". Actual: \"" + str, str.endsWith(expectedEnd)); }
Example #5
Source File: PatternLayoutTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testEqualsEmptyMarker() throws Exception { // replace "[]" with the empty string final PatternLayout layout = PatternLayout.newBuilder().setPattern("[%logger]%equals{[%marker]}{[]}{} %msg") .setConfiguration(ctx.getConfiguration()).build(); // Not empty marker final LogEvent event1 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMarker(MarkerManager.getMarker("TestMarker")) // .setMessage(new SimpleMessage("Hello, world!")).build(); assertToByteArray("[org.apache.logging.log4j.core.layout.PatternLayoutTest][TestMarker] Hello, world!", layout, event1); assertEncode("[org.apache.logging.log4j.core.layout.PatternLayoutTest][TestMarker] Hello, world!", layout, event1); // empty marker final LogEvent event2 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world!")).build(); assertToByteArray("[org.apache.logging.log4j.core.layout.PatternLayoutTest] Hello, world!", layout, event2); assertEncode("[org.apache.logging.log4j.core.layout.PatternLayoutTest] Hello, world!", layout, event2); }
Example #6
Source File: MarkerTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testEquals() { final String childMarkerName = CHILD_MAKER_NAME + "-ASM"; final String parentMakerName = PARENT_MARKER_NAME + "-ASM"; final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName); final org.slf4j.Marker slf4jMarker2 = org.slf4j.MarkerFactory.getMarker(childMarkerName); final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); slf4jMarker.add(slf4jParent); final Marker log4jParent = MarkerManager.getMarker(parentMakerName); final Marker log4jMarker = MarkerManager.getMarker(childMarkerName); final Marker log4jMarker2 = MarkerManager.getMarker(childMarkerName); assertEquals(log4jParent, log4jParent); assertEquals(log4jMarker, log4jMarker); assertEquals(log4jMarker, log4jMarker2); assertEquals(slf4jMarker, slf4jMarker2); assertNotEquals(log4jParent, log4jMarker); assertNotEquals(log4jMarker, log4jParent); }
Example #7
Source File: PatternLayoutTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testEqualsMarkerWithMessageSubstitution() throws Exception { // replace "[]" with the empty string final PatternLayout layout = PatternLayout.newBuilder().setPattern("[%logger]%equals{[%marker]}{[]}{[%msg]}") .setConfiguration(ctx.getConfiguration()).build(); // Not empty marker final LogEvent event1 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMarker(MarkerManager.getMarker("TestMarker")) .setMessage(new SimpleMessage("Hello, world!")).build(); final byte[] result1 = layout.toByteArray(event1); assertEquals("[org.apache.logging.log4j.core.layout.PatternLayoutTest][TestMarker]", new String(result1)); // empty marker final LogEvent event2 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) .setMessage(new SimpleMessage("Hello, world!")).build(); final byte[] result2 = layout.toByteArray(event2); assertEquals("[org.apache.logging.log4j.core.layout.PatternLayoutTest][Hello, world!]", new String(result2)); }
Example #8
Source File: MarkerTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testAddMarker() { final String childMakerName = CHILD_MAKER_NAME + "-AM"; final String parentMarkerName = PARENT_MARKER_NAME + "-AM"; final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMakerName); final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMarkerName); slf4jMarker.add(slf4jParent); final Marker log4jParent = MarkerManager.getMarker(parentMarkerName); final Marker log4jMarker = MarkerManager.getMarker(childMakerName); assertTrue("Incorrect Marker class", slf4jMarker instanceof Log4jMarker); assertTrue(String.format("%s (log4jMarker=%s) is not an instance of %s (log4jParent=%s) in Log4j", childMakerName, parentMarkerName, log4jMarker, log4jParent), log4jMarker.isInstanceOf(log4jParent)); assertTrue(String.format("%s (slf4jMarker=%s) is not an instance of %s (log4jParent=%s) in SLF4J", childMakerName, parentMarkerName, slf4jMarker, slf4jParent), slf4jMarker.contains(slf4jParent)); }
Example #9
Source File: Log4jMarkerFactory.java From logging-log4j2 with Apache License 2.0 | 6 votes |
private static org.apache.logging.log4j.Marker convertMarker(final Marker original, final Collection<Marker> visited) { final org.apache.logging.log4j.Marker marker = MarkerManager.getMarker(original.getName()); if (original.hasReferences()) { final Iterator<Marker> it = original.iterator(); while (it.hasNext()) { final Marker next = it.next(); if (visited.contains(next)) { LOGGER.warn("Found a cycle in Marker [{}]. Cycle will be broken.", next.getName()); } else { visited.add(next); marker.addParents(convertMarker(next, visited)); } } } return marker; }
Example #10
Source File: PatternLayoutTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testRegexEmptyMarker() throws Exception { // replace "[]" with the empty string final PatternLayout layout = PatternLayout.newBuilder().setPattern("[%logger]%replace{[%marker]}{\\[\\]}{} %msg") .setConfiguration(ctx.getConfiguration()).build(); // Not empty marker final LogEvent event1 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMarker(MarkerManager.getMarker("TestMarker")) // .setMessage(new SimpleMessage("Hello, world!")).build(); assertToByteArray("[org.apache.logging.log4j.core.layout.PatternLayoutTest][TestMarker] Hello, world!", layout, event1); assertEncode("[org.apache.logging.log4j.core.layout.PatternLayoutTest][TestMarker] Hello, world!", layout, event1); // empty marker final LogEvent event2 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world!")).build(); assertToByteArray("[org.apache.logging.log4j.core.layout.PatternLayoutTest] Hello, world!", layout, event2); assertEncode("[org.apache.logging.log4j.core.layout.PatternLayoutTest] Hello, world!", layout, event2); }
Example #11
Source File: Log4jMarkerFactory.java From logging-log4j2 with Apache License 2.0 | 6 votes |
private static org.apache.logging.log4j.Marker convertMarker(final Marker original, final Collection<Marker> visited) { final org.apache.logging.log4j.Marker marker = MarkerManager.getMarker(original.getName()); if (original.hasReferences()) { final Iterator<Marker> it = original.iterator(); while (it.hasNext()) { final Marker next = it.next(); if (visited.contains(next)) { LOGGER.warn("Found a cycle in Marker [{}]. Cycle will be broken.", next.getName()); } else { visited.add(next); marker.addParents(convertMarker(next, visited)); } } } return marker; }
Example #12
Source File: MarkerTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testEquals() { final String childMarkerName = CHILD_MAKER_NAME + "-ASM"; final String parentMakerName = PARENT_MARKER_NAME + "-ASM"; final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName); final org.slf4j.Marker slf4jMarker2 = org.slf4j.MarkerFactory.getMarker(childMarkerName); final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); slf4jMarker.add(slf4jParent); final Marker log4jParent = MarkerManager.getMarker(parentMakerName); final Marker log4jMarker = MarkerManager.getMarker(childMarkerName); final Marker log4jMarker2 = MarkerManager.getMarker(childMarkerName); assertEquals(log4jParent, log4jParent); assertEquals(log4jMarker, log4jMarker); assertEquals(log4jMarker, log4jMarker2); assertEquals(slf4jMarker, slf4jMarker2); assertNotEquals(log4jParent, log4jMarker); assertNotEquals(log4jMarker, log4jParent); }
Example #13
Source File: NoMarkerFilterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testMarkers() { final Marker sampleMarker = MarkerManager.getMarker("SampleMarker"); NoMarkerFilter filter = NoMarkerFilter.newBuilder().build(); filter.start(); assertTrue(filter.isStarted()); assertSame(Filter.Result.DENY, filter.filter(null, null, sampleMarker, (Object) null, (Throwable) null)); assertSame(Filter.Result.NEUTRAL, filter.filter(null, null, null, (Object) null, (Throwable) null)); filter.stop(); LogEvent event = Log4jLogEvent.newBuilder() // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("Hello, world!")).build(); assertSame(Filter.Result.NEUTRAL, filter.filter(event)); filter.start(); assertSame(Filter.Result.NEUTRAL, filter.filter(event)); event = Log4jLogEvent.newBuilder() // .setMarker(sampleMarker) // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("Hello, world!")).build(); assertSame(Filter.Result.DENY, filter.filter(event)); filter.stop(); }
Example #14
Source File: MutableLogEventTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testInitFromReusableCopiesFormatString() { Message message = ReusableMessageFactory.INSTANCE.newMessage("msg in a {}", "bottle"); final Log4jLogEvent source = Log4jLogEvent.newBuilder() // .setContextData(CONTEXT_DATA) // .setContextStack(STACK) // .setEndOfBatch(true) // .setIncludeLocation(true) // .setLevel(Level.FATAL) // .setLoggerFqcn("a.b.c.d.e") // .setLoggerName("my name is Logger") // .setMarker(MarkerManager.getMarker("on your marks")) // .setMessage(message) // .setNanoTime(1234567) // .setSource(new StackTraceElement("myclass", "mymethod", "myfile", 123)) // .setThreadId(100).setThreadName("threadname").setThreadPriority(10) // .setThrown(new RuntimeException("run")) // .setTimeMillis(987654321) .build(); final MutableLogEvent mutable = new MutableLogEvent(); mutable.initFrom(source); assertEquals("format", "msg in a {}", mutable.getFormat()); assertEquals("formatted", "msg in a bottle", mutable.getFormattedMessage()); assertEquals("parameters", new String[] {"bottle"}, mutable.getParameters()); Message memento = mutable.memento(); assertEquals("format", "msg in a {}", memento.getFormat()); assertEquals("formatted", "msg in a bottle", memento.getFormattedMessage()); assertEquals("parameters", new String[] {"bottle"}, memento.getParameters()); Message eventMementoMessage = mutable.createMemento().getMessage(); assertEquals("format", "msg in a {}", eventMementoMessage.getFormat()); assertEquals("formatted", "msg in a bottle", eventMementoMessage.getFormattedMessage()); assertEquals("parameters", new String[] {"bottle"}, eventMementoMessage.getParameters()); Message log4JLogEventMessage = new Log4jLogEvent.Builder(mutable).build().getMessage(); assertEquals("format", "msg in a {}", log4JLogEventMessage.getFormat()); assertEquals("formatted", "msg in a bottle", log4JLogEventMessage.getFormattedMessage()); assertEquals("parameters", new String[] {"bottle"}, log4JLogEventMessage.getParameters()); }
Example #15
Source File: FlumePersistentAppenderTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testLogInterrupted() { final ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(() -> { executor.shutdownNow(); final Logger logger = LogManager.getLogger("EventLogger"); final Marker marker = MarkerManager.getMarker("EVENT"); logger.info(marker, "This is a test message"); Assert.assertTrue("Interruption status not preserved", Thread.currentThread().isInterrupted()); }); }
Example #16
Source File: RingBufferLogEventTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@SuppressWarnings("deprecation") @Test public void testCreateMementoReturnsCopy() { final RingBufferLogEvent evt = new RingBufferLogEvent(); final String loggerName = "logger.name"; final Marker marker = MarkerManager.getMarker("marked man"); final String fqcn = "f.q.c.n"; final Level level = Level.TRACE; final Message data = new SimpleMessage("message"); final Throwable t = new InternalError("not a real error"); final ContextStack contextStack = new MutableThreadContextStack(Arrays.asList("a", "b")); final String threadName = "main"; final StackTraceElement location = null; evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, new FixedPreciseClock(12345, 678), new DummyNanoClock(1)); ((StringMap) evt.getContextData()).putValue("key", "value"); final LogEvent actual = evt.createMemento(); assertEquals(evt.getLoggerName(), actual.getLoggerName()); assertEquals(evt.getMarker(), actual.getMarker()); assertEquals(evt.getLoggerFqcn(), actual.getLoggerFqcn()); assertEquals(evt.getLevel(), actual.getLevel()); assertEquals(evt.getMessage(), actual.getMessage()); assertEquals(evt.getThrown(), actual.getThrown()); assertEquals(evt.getContextData(), actual.getContextData()); assertEquals(evt.getContextStack(), actual.getContextStack()); assertEquals(evt.getThreadName(), actual.getThreadName()); assertEquals(evt.getTimeMillis(), actual.getTimeMillis()); assertEquals(evt.getInstant().getNanoOfMillisecond(), actual.getInstant().getNanoOfMillisecond()); assertEquals(evt.getSource(), actual.getSource()); assertEquals(evt.getThrownProxy(), actual.getThrownProxy()); }
Example #17
Source File: ScriptAppenderSelectorTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private void logAndCheck() { final Marker marker = MarkerManager.getMarker("HEXDUMP"); final Logger logger = loggerContextRule.getLogger(ScriptAppenderSelectorTest.class); logger.error("Hello"); final ListAppender listAppender = getListAppender(); assertEquals("Incorrect number of events", 1, listAppender.getEvents().size()); logger.error("World"); assertEquals("Incorrect number of events", 2, listAppender.getEvents().size()); logger.error(marker, "DEADBEEF"); assertEquals("Incorrect number of events", 3, listAppender.getEvents().size()); }
Example #18
Source File: MapRewritePolicyTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@BeforeClass public static void setupClass() { stringMap.putValue("test1", "one"); stringMap.putValue("test2", "two"); map = stringMap.toMap(); logEvent0 = Log4jLogEvent.newBuilder() // .setLoggerName("test") // .setContextData(stringMap) // .setLoggerFqcn("MapRewritePolicyTest.setupClass()") // .setLevel(Level.ERROR) // .setMessage(new SimpleMessage("Test")) // .setThrown(new RuntimeException("test")) // .setThreadName("none") .setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 28)) .setTimeMillis(2).build(); logEvent1 = ((Log4jLogEvent) logEvent0).asBuilder() // .setMessage(new StringMapMessage(map)) // .setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 29)) // .build(); final ThreadContextStack stack = new MutableThreadContextStack(new ArrayList<>(map.values())); logEvent2 = ((Log4jLogEvent) logEvent0).asBuilder() // .setContextStack(stack) // .setMarker(MarkerManager.getMarker("test")) // .setLevel(Level.TRACE) // .setMessage(new StructuredDataMessage("test", "Nothing", "test", map)) // .setTimeMillis(20000000) // .setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 30)) // .build(); logEvent3 = ((Log4jLogEvent) logEvent0).asBuilder() // .setContextStack(stack) // .setLevel(Level.ALL) // .setMessage(new StringMapMessage(map)) // .setTimeMillis(Long.MAX_VALUE) // .setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 31)) // .build(); rewrite = new KeyValuePair[]{new KeyValuePair("test2", "2"), new KeyValuePair("test3", "three")}; }
Example #19
Source File: SyslogAppenderTestBase.java From logging-log4j2 with Apache License 2.0 | 5 votes |
protected void sendInfoStructuredMessage() { ThreadContext.put("loginId", "JohnDoe"); ThreadContext.put("ipAddress", "192.168.0.120"); ThreadContext.put("locale", Locale.US.getDisplayName()); final StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit"); msg.put("ToAccount", "123456"); msg.put("FromAccount", "123457"); msg.put("Amount", "200.00"); // the msg.toString() doesn't contain the parameters of the ThreadContext, so we must use the line1 string final String str = msg.asString(null, null); sentMessages.add(str); root.info(MarkerManager.getMarker("EVENT"), msg); }
Example #20
Source File: MutableLogEventTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testInitFromReusableObjectCopiesParameter() { Object param = new Object(); Message message = ReusableMessageFactory.INSTANCE.newMessage(param); final Log4jLogEvent source = Log4jLogEvent.newBuilder() .setContextData(CONTEXT_DATA) .setContextStack(STACK) .setEndOfBatch(true) .setIncludeLocation(true) .setLevel(Level.FATAL) .setLoggerFqcn("a.b.c.d.e") .setLoggerName("my name is Logger") .setMarker(MarkerManager.getMarker("on your marks")) .setMessage(message) .setNanoTime(1234567) .setSource(new StackTraceElement("myclass", "mymethod", "myfile", 123)) .setThreadId(100).setThreadName("threadname") .setThreadPriority(10) .setThrown(new RuntimeException("run")) .setTimeMillis(987654321) .build(); final MutableLogEvent mutable = new MutableLogEvent(); mutable.initFrom(source); assertNull("format", mutable.getFormat()); assertEquals("formatted", param.toString(), mutable.getFormattedMessage()); assertEquals("parameters", new Object[] {param}, mutable.getParameters()); Message memento = mutable.memento(); assertNull("format", memento.getFormat()); assertEquals("formatted", param.toString(), memento.getFormattedMessage()); assertEquals("parameters", new Object[] {param}, memento.getParameters()); }
Example #21
Source File: DefaultRouteScriptAppenderTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private void logAndCheck() { final Marker marker = MarkerManager.getMarker("HEXDUMP"); final Logger logger = loggerContextRule.getLogger(DefaultRouteScriptAppenderTest.class); logger.error("Hello"); final ListAppender listAppender = getListAppender(); assertEquals("Incorrect number of events", 1, listAppender.getEvents().size()); logger.error("World"); assertEquals("Incorrect number of events", 2, listAppender.getEvents().size()); logger.error(marker, "DEADBEEF"); assertEquals("Incorrect number of events", 3, listAppender.getEvents().size()); }
Example #22
Source File: MarkerPatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testLookup() { final Message msg = new StructuredDataMessage("Test", "This is a test", "Audit"); final Marker eventMarker = MarkerManager.getMarker("EVENT"); final Marker auditMarker = MarkerManager.getMarker("AUDIT").setParents(eventMarker); final LogEvent event = Log4jLogEvent.newBuilder().setLoggerName("MyLogger").setMarker(auditMarker) .setLevel(Level.DEBUG).setMessage(msg).build(); final StringBuilder sb = new StringBuilder(); final MarkerPatternConverter converter = MarkerPatternConverter.newInstance(null); converter.format(event, sb); assertEquals(auditMarker.toString(), sb.toString()); }
Example #23
Source File: AbstractLog4j2EcsLayoutTest.java From ecs-logging-java with Apache License 2.0 | 5 votes |
@Test void testMarker() throws Exception { Marker parent = MarkerManager.getMarker("parent"); Marker child = MarkerManager.getMarker("child").setParents(parent); Marker grandchild = MarkerManager.getMarker("grandchild").setParents(child); root.debug(grandchild, "test"); assertThat(getLastLogLine().get("tags")).contains( TextNode.valueOf("parent"), TextNode.valueOf("child"), TextNode.valueOf("grandchild")); }
Example #24
Source File: AbstractMarkerMixInTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testTwoParents() throws IOException { final Marker expected = MarkerManager.getMarker("A"); final Marker parent1 = MarkerManager.getMarker("PARENT_MARKER1"); final Marker parent2 = MarkerManager.getMarker("PARENT_MARKER2"); expected.addParents(parent1); expected.addParents(parent2); final String str = writeValueAsString(expected); Assert.assertTrue(str.contains("PARENT_MARKER1")); Assert.assertTrue(str.contains("PARENT_MARKER2")); final Marker actual = reader.readValue(str); Assert.assertEquals(expected, actual); }
Example #25
Source File: MarkerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testRemoveNullMarker() { final String childMakerName = CHILD_MAKER_NAME + "-CM"; final String parentMakerName = PARENT_MARKER_NAME + "-CM"; final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMakerName); final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); slf4jMarker.add(slf4jParent); final Marker log4jParent = MarkerManager.getMarker(parentMakerName); final Marker log4jMarker = MarkerManager.getMarker(childMakerName); final Log4jMarker log4jSlf4jParent = new Log4jMarker(log4jParent); final Log4jMarker log4jSlf4jMarker = new Log4jMarker(log4jMarker); final org.slf4j.Marker nullMarker = null; Assert.assertFalse(log4jSlf4jParent.remove(nullMarker)); Assert.assertFalse(log4jSlf4jMarker.remove(nullMarker)); }
Example #26
Source File: Log4jMarkerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testEquals() { final Marker markerA = MarkerManager.getMarker(Log4jMarkerTest.class.getName() + "-A"); final Marker markerB = MarkerManager.getMarker(Log4jMarkerTest.class.getName() + "-B"); final Log4jMarker marker1 = new Log4jMarker(markerA); final Log4jMarker marker2 = new Log4jMarker(markerA); final Log4jMarker marker3 = new Log4jMarker(markerB); Assert.assertEquals(marker1, marker2); Assert.assertNotEquals(marker1, null); Assert.assertNotEquals(null, marker1); Assert.assertNotEquals(marker1, marker3); }
Example #27
Source File: AbstractMarkerMixInTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testOneParent() throws IOException { final Marker expected = MarkerManager.getMarker("A"); final Marker parent = MarkerManager.getMarker("PARENT_MARKER"); expected.addParents(parent); final String str = writeValueAsString(expected); Assert.assertTrue(str.contains("PARENT_MARKER")); final Marker actual = reader.readValue(str); Assert.assertEquals(expected, actual); }
Example #28
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void structuredData() { ThreadContext.put("loginId", "JohnDoe"); ThreadContext.put("ipAddress", "192.168.0.120"); ThreadContext.put("locale", Locale.US.getDisplayName()); final StructuredDataMessage msg = new StructuredDataMessage("Audit@18060", "Transfer Complete", "Transfer"); msg.put("ToAccount", "123456"); msg.put("FromAccount", "123457"); msg.put("Amount", "200.00"); logger.info(MarkerManager.getMarker("EVENT"), msg); ThreadContext.clearMap(); final List<LogEvent> events = app.getEvents(); assertEventCount(events, 1); }
Example #29
Source File: FlumePersistentAppenderTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testSingle() throws IOException { final Logger logger = LogManager.getLogger("EventLogger"); final Marker marker = MarkerManager.getMarker("EVENT"); logger.info(marker, "This is a test message"); final Event event = primary.poll(); Assert.assertNotNull(event); final String body = getBody(event); Assert.assertTrue("Channel contained event, but not expected message. Received: " + body, body.endsWith("This is a test message")); }
Example #30
Source File: PatternLayoutTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testPatternSelector() throws Exception { final PatternMatch[] patterns = new PatternMatch[1]; patterns[0] = new PatternMatch("FLOW", "%d %-5p [%t]: ====== %C{1}.%M:%L %m ======%n"); // @formatter:off final PatternSelector selector = MarkerPatternSelector.newBuilder() .setProperties(patterns) .setDefaultPattern("%d %-5p [%t]: %m%n") .setAlwaysWriteExceptions(true) .setNoConsoleNoAnsi(true) .setConfiguration(ctx.getConfiguration()) .build(); // @formatter:on final PatternLayout layout = PatternLayout.newBuilder().setPatternSelector(selector) .setConfiguration(ctx.getConfiguration()).build(); final LogEvent event1 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.layout.PatternLayoutTest$FauxLogger") .setMarker(MarkerManager.getMarker("FLOW")) .setLevel(Level.TRACE) // .setIncludeLocation(true) .setMessage(new SimpleMessage("entry")).build(); final String result1 = new FauxLogger().formatEvent(event1, layout); final String expectPattern1 = String.format(".*====== PatternLayoutTest.testPatternSelector:\\d+ entry ======%n"); assertTrue("Unexpected result: " + result1, result1.matches(expectPattern1)); final LogEvent event2 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world 1!")).build(); final String result2 = new String(layout.toByteArray(event2)); final String expectSuffix2 = String.format("Hello, world 1!%n"); assertTrue("Unexpected result: " + result2, result2.endsWith(expectSuffix2)); }