org.apache.logging.log4j.core.impl.Log4jLogEvent Java Examples
The following examples show how to use
org.apache.logging.log4j.core.impl.Log4jLogEvent.
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: PatternSelectorTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testLevelPatternSelector() throws Exception { final PatternMatch[] patterns = new PatternMatch[1]; patterns[0] = new PatternMatch("TRACE", "%d %-5p [%t]: ====== %C{1}.%M:%L %m ======%n"); final PatternSelector selector = LevelPatternSelector.createSelector(patterns, "%d %-5p [%t]: %m%n", true, true, ctx.getConfiguration()); 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.PatternSelectorTest$FauxLogger") .setLevel(Level.TRACE) // .setIncludeLocation(true) .setMessage(new SimpleMessage("entry")).build(); final String result1 = new FauxLogger().formatEvent(event1, layout); final String expectSuffix1 = String.format("====== PatternSelectorTest.testLevelPatternSelector:85 entry ======%n"); assertTrue("Unexpected result: " + result1, result1.endsWith(expectSuffix1)); 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)); }
Example #2
Source File: MessagePatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testDisabledLookup() { final Configuration config = new DefaultConfigurationBuilder() .addProperty("foo", "bar") .build(true); final MessagePatternConverter converter = MessagePatternConverter.newInstance( config, new String[] {"nolookups"}); final Message msg = new ParameterizedMessage("${foo}"); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("MyLogger") // .setLevel(Level.DEBUG) // .setMessage(msg).build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); assertEquals("Expected the raw pattern string without lookup", "${foo}", sb.toString()); }
Example #3
Source File: ConsoleAppenderTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
private void testConsoleStreamManagerDoesNotClose(final PrintStream ps, final Target targetName, final SystemSetter systemSetter) { try { systemSetter.systemSet(psMock); final Layout<String> layout = PatternLayout.newBuilder().setAlwaysWriteExceptions(true).build(); final ConsoleAppender app = ConsoleAppender.newBuilder().setLayout(layout).setTarget(targetName) .setName("Console").setIgnoreExceptions(false).build(); app.start(); assertTrue("Appender did not start", app.isStarted()); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("TestLogger") // .setLoggerFqcn(ConsoleAppenderTest.class.getName()) // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Test")) // .build(); app.append(event); app.stop(); assertFalse("Appender did not stop", app.isStarted()); } finally { systemSetter.systemSet(ps); } then(psMock).should(atLeastOnce()).write(any(byte[].class), anyInt(), anyInt()); then(psMock).should(atLeastOnce()).flush(); }
Example #4
Source File: LogEventTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test @Ignore public void testEquals() { final LogEvent event1 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()) // .setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world!")) // .build(); 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 LogEvent event3 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()) // .setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world!")) // .build(); assertNotEquals("Events should not be equal", event1, event2); assertEquals("Events should be equal", event2, event3); }
Example #5
Source File: ExtendedObjectWriterTest.java From log4j2-elasticsearch with Apache License 2.0 | 6 votes |
@Test public void returnsNewSerProvNewWhenSerializersCached() throws IOException { // given int expectedCalls = new Random().nextInt(100) + 2; List<DefaultSerializerProvider> caught = new ArrayList<>(); ExtendedObjectMapper mapper = new ExtendedObjectMapper(new JsonFactory()); ExtendedObjectWriter writer = spy((ExtendedObjectWriter)mapper._newWriter(mapper.getSerializationConfig(), mapper.getTypeFactory().constructType(Log4jLogEvent.class), null)); when(writer._serializerProvider()).thenAnswer((Answer<DefaultSerializerProvider>) invocationOnMock -> { DefaultSerializerProvider result = (DefaultSerializerProvider) invocationOnMock.callRealMethod(); caught.add(result); return result; }); // when for (int i = 0; i < expectedCalls; i++) { writer.writeValue((OutputStream)new ReusableByteBufOutputStream(Mockito.mock(ByteBuf.class)), Log4jLogEvent.newBuilder().build()); } // then Assert.assertEquals(2, new HashSet<>(caught).size()); verify(writer, Mockito.times(expectedCalls))._serializerProvider(); }
Example #6
Source File: TimeFilterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void springForward() { final TimeFilter filter = new TimeFilter(LocalTime.of(2,0), LocalTime.of(3,0), ZoneId.of("America/Los_Angeles"), null, null, LocalDate.of(2020, 3, 8)); filter.start(); assertTrue(filter.isStarted()); ZonedDateTime date = ZonedDateTime.of(2020, 3, 8, 2, 6, 30, 0, ZoneId.of("America/Los_Angeles")); CLOCKTIME = date.toInstant().toEpochMilli(); LogEvent event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame("Time " + CLOCKTIME + " is not within range: " + filter.toString(), Filter.Result.NEUTRAL, filter.filter(event)); date = date.plusDays(1).withHour(2); CLOCKTIME = date.toInstant().toEpochMilli(); event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame("Time " + CLOCKTIME + " is not within range: " + filter.toString(), Filter.Result.NEUTRAL, filter.filter(event)); date = date.withHour(4); CLOCKTIME = date.toInstant().toEpochMilli(); event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame("Time " + CLOCKTIME + " is within range: " + filter.toString(), Filter.Result.DENY, filter.filter(event)); }
Example #7
Source File: YamlLayoutTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testMutableLogEvent() throws Exception { final AbstractJacksonLayout layout = YamlLayout.newBuilder() .setLocationInfo(false) .setProperties(false) .setIncludeStacktrace(false) .setAdditionalFields(new KeyValuePair[] { new KeyValuePair("KEY1", "VALUE1"), new KeyValuePair("KEY2", "${java:runtime}"), }) .setCharset(StandardCharsets.UTF_8) .setConfiguration(ctx.getConfiguration()) .build(); Log4jLogEvent logEvent = LogEventFixtures.createLogEvent(); final MutableLogEvent mutableEvent = new MutableLogEvent(); mutableEvent.initFrom(logEvent); final String strLogEvent = layout.toSerializable(logEvent); final String strMutableEvent = layout.toSerializable(mutableEvent); assertEquals(strMutableEvent, strLogEvent, strMutableEvent); }
Example #8
Source File: LevelPatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testLevelMapWithLength() { final Message msg = new SimpleMessage("Hello"); LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("MyLogger") // .setLevel(Level.DEBUG) // .setMessage(msg).build(); final StringBuilder sb = new StringBuilder(); LevelPatternConverter converter = LevelPatternConverter.newInstance(null); converter.format(event, sb); assertEquals(Level.DEBUG.toString(), sb.toString()); final String[] opts = new String[] { "WARN=Warning, length=2" }; converter = LevelPatternConverter.newInstance(opts); sb.setLength(0); converter.format(event, sb); assertEquals("DE", sb.toString()); event = Log4jLogEvent.newBuilder() // .setLoggerName("MyLogger") // .setLevel(Level.WARN) // .setMessage(msg).build(); sb.setLength(0); converter.format(event, sb); assertEquals("Warning", sb.toString()); }
Example #9
Source File: TimeFilterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testTime() { // https://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/ final TimeFilter filter = TimeFilter.createFilter("02:00:00", "03:00:00", "America/Los_Angeles", null, null); filter.start(); assertTrue(filter.isStarted()); final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles")); cal.set(Calendar.HOUR_OF_DAY, 2); CLOCKTIME = cal.getTimeInMillis(); LogEvent event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); //assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null)); assertSame("Time " + CLOCKTIME + " is not within range: " + filter.toString(), Filter.Result.NEUTRAL, filter.filter(event)); cal.roll(Calendar.DAY_OF_MONTH, true); cal.set(Calendar.HOUR_OF_DAY, 2); CLOCKTIME = cal.getTimeInMillis(); event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame("Time " + CLOCKTIME + " is not within range: " + filter.toString(), Filter.Result.NEUTRAL, filter.filter(event)); //assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null)); cal.set(Calendar.HOUR_OF_DAY, 4); CLOCKTIME = cal.getTimeInMillis(); event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); //assertSame(Filter.Result.DENY, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null)); assertSame("Time " + CLOCKTIME + " is within range: " + filter.toString(), Filter.Result.DENY, filter.filter(event)); }
Example #10
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 #11
Source File: ThrowablePatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testShortClassName() { final String packageName = "org.apache.logging.log4j.core.pattern."; final String[] options = { "short.className" }; final ThrowablePatternConverter converter = ThrowablePatternConverter.newInstance(null, options); final Throwable cause = new NullPointerException("null pointer"); final Throwable parent = new IllegalArgumentException("IllegalArgument", cause); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("testLogger") // .setLoggerFqcn(this.getClass().getName()) // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("test exception")) // .setThrown(parent).build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String result = sb.toString(); assertEquals("The class names should be same", packageName + "ThrowablePatternConverterTest", result); }
Example #12
Source File: PatternLayoutTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@SuppressWarnings("unused") private void testUnixTime(final String pattern) throws Exception { final PatternLayout layout = PatternLayout.newBuilder().setPattern(pattern + " %m") .setConfiguration(ctx.getConfiguration()).build(); final LogEvent event1 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world 1!")).build(); final byte[] result1 = layout.toByteArray(event1); assertEquals(event1.getTimeMillis() + " Hello, world 1!", new String(result1)); // System.out.println("event1=" + event1.getMillis()); final LogEvent event2 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world 2!")).build(); final byte[] result2 = layout.toByteArray(event2); assertEquals(event2.getTimeMillis() + " Hello, world 2!", new String(result2)); // System.out.println("event2=" + event2.getMillis()); }
Example #13
Source File: RootThrowablePatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testFull1() { final RootThrowablePatternConverter converter = RootThrowablePatternConverter.newInstance(null, null); final Throwable cause = new NullPointerException("null pointer"); final Throwable parent = new IllegalArgumentException("IllegalArgument", cause); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("testLogger") // .setLoggerFqcn(this.getClass().getName()) // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("test exception")) // .setThrown(parent).build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String result = sb.toString(); // System.out.print(result); assertTrue("Missing Exception", result.contains("Wrapped by: java.lang.IllegalArgumentException: IllegalArgument")); assertTrue("Incorrect start of msg", result.startsWith("java.lang.NullPointerException: null pointer")); }
Example #14
Source File: LogEventTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testNanoTimeIsNotSerialized1() throws Exception { final LogEvent event1 = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()) // .setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world!")) // .setThreadName("this must be initialized or the test fails") // .setNanoTime(12345678L) // .build(); final LogEvent copy = new Log4jLogEvent.Builder(event1).build(); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(event1); final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); final ObjectInputStream ois = new FilteredObjectInputStream(bais); final LogEvent actual = (LogEvent) ois.readObject(); assertNotEquals("Different event: nanoTime", copy, actual); assertNotEquals("Different nanoTime", copy.getNanoTime(), actual.getNanoTime()); assertEquals("deserialized nanoTime is zero", 0, actual.getNanoTime()); }
Example #15
Source File: MdcPatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testConverterWithKeys() { final Message msg = new SimpleMessage("Hello"); final String [] options = new String[] {"object, subject"}; final MdcPatternConverter converter = MdcPatternConverter.newInstance(options); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("MyLogger") // .setLevel(Level.DEBUG) // .setMessage(msg) // .build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String str = sb.toString(); final String expected = "{object=Log4j, subject=I}"; assertEquals(expected, str); }
Example #16
Source File: ThrowablePatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testShortMethodName() { final String[] options = { "short.methodName" }; final ThrowablePatternConverter converter = ThrowablePatternConverter.newInstance(null, options); final Throwable cause = new NullPointerException("null pointer"); final Throwable parent = new IllegalArgumentException("IllegalArgument", cause); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("testLogger") // .setLoggerFqcn(this.getClass().getName()) // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("test exception")) // .setThrown(parent).build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String result = sb.toString(); assertEquals("The method names should be same", "testShortMethodName", result); }
Example #17
Source File: JsonLayoutTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testLayoutMessageWithCurlyBraces() throws Exception { final boolean propertiesAsList = false; final AbstractJacksonLayout layout = JsonLayout.newBuilder() .setLocationInfo(false) .setProperties(false) .setPropertiesAsList(propertiesAsList) .setComplete(false) .setCompact(true) .setEventEol(false) .setCharset(StandardCharsets.UTF_8) .setIncludeStacktrace(true) .build(); final Log4jLogEvent expected = Log4jLogEvent.newBuilder() .setLoggerName("a.B") .setLoggerFqcn("f.q.c.n") .setLevel(Level.DEBUG) .setMessage(new ParameterizedMessage("Testing {}", new TestObj())) .setThreadName("threadName") .setTimeMillis(1).build(); final String str = layout.toSerializable(expected); final String expectedMessage = "Testing " + TestObj.TO_STRING_VALUE; assertTrue(str, str.contains("\"message\":\"" + expectedMessage + '"')); final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class); assertEquals(expectedMessage, actual.getMessage().getFormattedMessage()); }
Example #18
Source File: TimeFilterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void overnightFallback() { final TimeFilter filter = new TimeFilter(LocalTime.of(23,0), LocalTime.of(2,0), ZoneId.of("America/Los_Angeles"), null, null, LocalDate.of(2020, 10, 31)); filter.start(); assertTrue(filter.isStarted()); ZonedDateTime date = ZonedDateTime.of(2020, 10, 31, 23, 30, 30, 0, ZoneId.of("America/Los_Angeles")).withEarlierOffsetAtOverlap(); CLOCKTIME = date.toInstant().toEpochMilli(); LogEvent event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame("Time " + CLOCKTIME + " is not within range: " + filter.toString(), Filter.Result.NEUTRAL, filter.filter(event)); date = date.plusHours(1); CLOCKTIME = date.toInstant().toEpochMilli(); event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame("Time " + CLOCKTIME + " is not within range: " + filter.toString(), Filter.Result.NEUTRAL, filter.filter(event)); date = date.plusHours(2); CLOCKTIME = date.toInstant().toEpochMilli(); event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame("Time " + CLOCKTIME + " is within range: " + filter.toString(), Filter.Result.DENY, filter.filter(event)); date = date.plusDays(1).withHour(0); CLOCKTIME = date.toInstant().toEpochMilli(); event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame("Time " + CLOCKTIME + " is not within range: " + filter.toString(), Filter.Result.NEUTRAL, filter.filter(event)); }
Example #19
Source File: ExtendedThrowablePatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testSuffix() { final String suffix = "suffix(test suffix)"; final String[] options = {suffix}; final ExtendedThrowablePatternConverter converter = ExtendedThrowablePatternConverter.newInstance(null, options); final Throwable cause = new NullPointerException("null pointer"); final Throwable parent = new IllegalArgumentException("IllegalArgument", cause); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("testLogger") // .setLoggerFqcn(this.getClass().getName()) // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("test exception")) // .setThrown(parent).build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String result = sb.toString(); assertTrue("No suffix", result.contains("test suffix")); }
Example #20
Source File: PatternLayoutBenchmark.java From logging-log4j2 with Apache License 2.0 | 6 votes |
private static LogEvent createLogEvent() { final Marker marker = null; final String fqcn = "com.mycom.myproject.mypackage.MyClass"; final Level level = Level.DEBUG; final Message message = new SimpleMessage(STR); final Throwable t = null; final StringMap mdc = null; final ContextStack ndc = null; final String threadName = null; final StackTraceElement location = null; final long timestamp = 12345678; return Log4jLogEvent.newBuilder() // .setLoggerName("name(ignored)") // .setMarker(marker) // .setLoggerFqcn(fqcn) // .setLevel(level) // .setMessage(message) // .setThrown(t) // .setContextData(mdc) // .setContextStack(ndc) // .setThreadName(threadName) // .setSource(location) // .setTimeMillis(timestamp) // .build(); }
Example #21
Source File: ThrowablePatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testShortLineNumber() { final String[] options = { "short.lineNumber" }; final ThrowablePatternConverter converter = ThrowablePatternConverter.newInstance(null, options); final Throwable cause = new NullPointerException("null pointer"); final Throwable parent = new IllegalArgumentException("IllegalArgument", cause); final StackTraceElement top = parent.getStackTrace()[0]; final int expectedLineNumber = top.getLineNumber(); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("testLogger") // .setLoggerFqcn(this.getClass().getName()) // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("test exception")) // .setThrown(parent).build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String result = sb.toString(); assertTrue("The line numbers should be same", expectedLineNumber == Integer.parseInt(result)); }
Example #22
Source File: MapPatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testConverterWithJavaFormat() { final StringMapMessage msg = new StringMapMessage(); msg.put("subject", "I"); msg.put("verb", "love"); msg.put("object", "Log4j"); final MapPatternConverter converter = MapPatternConverter.newInstance(null, MapMessage.MapFormat.JAVA); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("MyLogger") // .setLevel(Level.DEBUG) // .setMessage(msg) // .build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String str = sb.toString(); String expected = "subject=\"I\""; assertTrue("Missing or incorrect subject. Expected " + expected + ", actual " + str, str.contains(expected)); expected = "verb=\"love\""; assertTrue("Missing or incorrect verb", str.contains(expected)); expected = "object=\"Log4j\""; assertTrue("Missing or incorrect object", str.contains(expected)); assertEquals("{object=\"Log4j\", subject=\"I\", verb=\"love\"}", str); }
Example #23
Source File: EncodingPatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testReplacement() { final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName(EncodingPatternConverterTest.class.getName()) // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("Test \r\n<div class=\"test\">this</div> & <div class='test'>that</div>")) .build(); final StringBuilder sb = new StringBuilder(); final LoggerContext ctx = LoggerContext.getContext(); final String[] options = new String[]{"%msg"}; final EncodingPatternConverter converter = EncodingPatternConverter .newInstance(ctx.getConfiguration(), options); assertNotNull("Error creating converter", converter); converter.format(event, sb); assertEquals( "Test \\r\\n<div class="test">this</div> & <div class='test'>that</div>", sb.toString()); }
Example #24
Source File: EncodingPatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testCrlfEscaping() { final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName(EncodingPatternConverterTest.class.getName()) // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("Test \r\n<div class=\"test\">this\r</div> & \n<div class='test'>that</div>")) .build(); final StringBuilder sb = new StringBuilder(); final LoggerContext ctx = LoggerContext.getContext(); final String[] options = new String[]{"%msg", "CRLF"}; final EncodingPatternConverter converter = EncodingPatternConverter .newInstance(ctx.getConfiguration(), options); assertNotNull("Error creating converter", converter); converter.format(event, sb); assertEquals( "Test \\r\\n<div class=\"test\">this\\r</div> & \\n<div class='test'>that</div>", sb.toString()); }
Example #25
Source File: GelfLayoutBenchmark.java From logging-log4j2 with Apache License 2.0 | 6 votes |
private static LogEvent createLogEvent() { final Marker marker = null; final String fqcn = "com.mycom.myproject.mypackage.MyClass"; final org.apache.logging.log4j.Level level = org.apache.logging.log4j.Level.DEBUG; final Message message = new SimpleMessage(MESSAGE); final Throwable t = null; final StringMap mdc = null; final ThreadContext.ContextStack ndc = null; final String threadName = null; final StackTraceElement location = null; final long timestamp = 12345678; return Log4jLogEvent.newBuilder() // .setLoggerName("name(ignored)") // .setMarker(marker) // .setLoggerFqcn(fqcn) // .setLevel(level) // .setMessage(message) // .setThrown(t) // .setContextData(mdc) // .setContextStack(ndc) // .setThreadName(threadName) // .setSource(location) // .setTimeMillis(timestamp) // .build(); }
Example #26
Source File: RootThrowablePatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testSuffix() { final String suffix = "suffix(test suffix)"; final String[] options = {suffix}; final RootThrowablePatternConverter converter = RootThrowablePatternConverter.newInstance(null, options); final Throwable cause = new NullPointerException("null pointer"); final Throwable parent = new IllegalArgumentException("IllegalArgument", cause); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("testLogger") // .setLoggerFqcn(this.getClass().getName()) // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("test exception")) // .setThrown(parent).build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String result = sb.toString(); assertTrue("No suffix", result.contains("test suffix")); }
Example #27
Source File: MdcPatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testConverterWithKey() { final Message msg = new SimpleMessage("Hello"); final String [] options = new String[] {"object"}; final MdcPatternConverter converter = MdcPatternConverter.newInstance(options); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("MyLogger") // .setLevel(Level.DEBUG) // .setMessage(msg) // .build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String str = sb.toString(); final String expected = "Log4j"; assertEquals(expected, str); }
Example #28
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 #29
Source File: TimeFilterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void fallBack() { final TimeFilter filter = new TimeFilter(LocalTime.of(1,0), LocalTime.of(2,0), ZoneId.of("America/Los_Angeles"), null, null, LocalDate.of(2020, 11, 1)); filter.start(); assertTrue(filter.isStarted()); ZonedDateTime date = ZonedDateTime.of(2020, 11, 1, 1, 6, 30, 0, ZoneId.of("America/Los_Angeles")).withEarlierOffsetAtOverlap(); CLOCKTIME = date.toInstant().toEpochMilli(); LogEvent event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame("Time " + CLOCKTIME + " is not within range: " + filter.toString(), Filter.Result.NEUTRAL, filter.filter(event)); date = ZonedDateTime.of(2020, 11, 1, 1, 6, 30, 0, ZoneId.of("America/Los_Angeles")).withLaterOffsetAtOverlap(); CLOCKTIME = date.toInstant().toEpochMilli(); event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame("Time " + CLOCKTIME + " is within range: " + filter.toString(), Filter.Result.DENY, filter.filter(event)); date = date.plusDays(1).withHour(1).withMinute(30); CLOCKTIME = date.toInstant().toEpochMilli(); event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame("Time " + CLOCKTIME + " is not within range: " + filter.toString(), Filter.Result.NEUTRAL, filter.filter(event)); date = date.withHour(4); CLOCKTIME = date.toInstant().toEpochMilli(); event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build(); assertSame("Time " + CLOCKTIME + " is within range: " + filter.toString(), Filter.Result.DENY, filter.filter(event)); }
Example #30
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); }