Search in sources :

Example 51 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class ThrowablePatternConverterTest method testShortLocalizedMessage.

@Test
public void testShortLocalizedMessage() {
    final String[] options = { "short.localizedMessage" };
    final ThrowablePatternConverter converter = ThrowablePatternConverter.newInstance(null, options);
    final Throwable parent = new LocalizedException();
    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 messages should be same", "I am localized.", result);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 52 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class VariablesNotEmptyReplacementConverterTest method testReplacement.

private void testReplacement(final String tag, final String expectedValue) {
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    VariablesNotEmptyReplacementConverterTest.class.getName()).setLevel(//
    Level.DEBUG).setMessage(//
    new SimpleMessage("This is a test")).build();
    final StringBuilder sb = new StringBuilder();
    final LoggerContext ctx = LoggerContext.getContext();
    final String[] options = new String[] { "[" + tag + "]" };
    final VariablesNotEmptyReplacementConverter converter = VariablesNotEmptyReplacementConverter.newInstance(ctx.getConfiguration(), options);
    converter.format(event, sb);
    assertEquals(expectedValue, sb.toString());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 53 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class JmsAppenderIT method testLogObjectMessageToQueue.

@Test
public void testLogObjectMessageToQueue() throws Exception {
    setUp(SerializedLayout.createLayout());
    final int messageCount = 100;
    final MessageConsumer messageConsumer = jmsManager.createMessageConsumer();
    final JmsQueueConsumer consumer = new JmsQueueConsumer(messageCount, ObjectMessage.class);
    messageConsumer.setMessageListener(consumer);
    final String messageText = "Hello, World!";
    final String loggerName = this.getClass().getName();
    for (int i = 0; i < messageCount; i++) {
        final LogEvent event = //
        Log4jLogEvent.newBuilder().setLoggerName(loggerName).setLoggerFqcn(loggerName).setLevel(//
        Level.INFO).setMessage(new SimpleMessage(messageText)).setThreadName(//
        Thread.currentThread().getName()).setTimeMillis(System.currentTimeMillis()).build();
        appender.append(event);
    }
    consumer.awaitAndAssertAllMessagesConsumed();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 54 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class Logger method logMessage.

@Override
public void logMessage(final String fqcn, final Level level, final Marker marker, final Message message, final Throwable t) {
    final Message msg = message == null ? new SimpleMessage(Strings.EMPTY) : message;
    final ReliabilityStrategy strategy = privateConfig.loggerConfig.getReliabilityStrategy();
    strategy.log(this, getName(), fqcn, marker, level, msg, t);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ReliabilityStrategy(org.apache.logging.log4j.core.config.ReliabilityStrategy)

Example 55 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class ConsoleAppenderTest method testConsoleStreamManagerDoesNotClose.

private void testConsoleStreamManagerDoesNotClose(final PrintStream ps, final Target targetName, final SystemSetter systemSetter) {
    try {
        systemSetter.systemSet(psMock);
        final Layout<String> layout = PatternLayout.newBuilder().withAlwaysWriteExceptions(true).build();
        final ConsoleAppender app = ConsoleAppender.newBuilder().withLayout(layout).setTarget(targetName).withName("Console").withIgnoreExceptions(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();
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage)

Aggregations

SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)130 Test (org.junit.Test)98 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)96 LogEvent (org.apache.logging.log4j.core.LogEvent)93 Message (org.apache.logging.log4j.message.Message)34 Marker (org.apache.logging.log4j.Marker)19 Level (org.apache.logging.log4j.Level)11 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)10 IOException (java.io.IOException)7 LoggerContext (org.apache.logging.log4j.core.LoggerContext)7 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)6 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)6 HashMap (java.util.HashMap)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectInputStream (java.io.ObjectInputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)4