Search in sources :

Example 86 with SimpleMessage

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

the class PatternLayoutComparisonBenchmark method createLog4j2Event.

private static LogEvent createLog4j2Event() {
    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 Map<String, String> 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).setContextMap(//
    mdc).setContextStack(//
    ndc).setThreadName(//
    threadName).setSource(//
    location).setTimeMillis(//
    timestamp).build();
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Level(org.apache.logging.log4j.Level) Marker(org.apache.logging.log4j.Marker) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack)

Example 87 with SimpleMessage

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

the class AsyncLoggerConfigUseAfterShutdownTest method testNoErrorIfLogAfterShutdown.

@Test
public void testNoErrorIfLogAfterShutdown() throws Exception {
    final Logger log = LogManager.getLogger("com.foo.Bar");
    log.info("some message");
    // stop async thread
    CoreLoggerContexts.stopLoggerContext();
    // call the #logMessage() method to bypass the isEnabled check: 
    // before the LOG4J2-639 fix this would throw a NPE
    ((AbstractLogger) log).logMessage("com.foo.Bar", Level.INFO, null, new SimpleMessage("msg"), null);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Logger(org.apache.logging.log4j.Logger) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Example 88 with SimpleMessage

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

the class AsyncLoggerUseAfterShutdownTest method testNoErrorIfLogAfterShutdown.

@Test
public void testNoErrorIfLogAfterShutdown() throws Exception {
    final Logger log = LogManager.getLogger("com.foo.Bar");
    final String msg = "Async logger msg";
    log.info(msg, new InternalError("this is not a real error"));
    // stop async thread
    CoreLoggerContexts.stopLoggerContext();
    // call the #logMessage() method to bypass the isEnabled check: 
    // before the LOG4J2-639 fix this would throw a NPE
    ((AbstractLogger) log).logMessage("com.foo.Bar", Level.INFO, null, new SimpleMessage("msg"), null);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Logger(org.apache.logging.log4j.Logger) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Example 89 with SimpleMessage

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

the class Log4jLogger method log.

@Override
public void log(final Marker marker, final String fqcn, final int level, final String message, final Object[] params, Throwable throwable) {
    final Level log4jLevel = getLevel(level);
    final org.apache.logging.log4j.Marker log4jMarker = getMarker(marker);
    if (!logger.isEnabled(log4jLevel, log4jMarker, message, params)) {
        return;
    }
    final Message msg;
    if (eventLogger && marker != null && marker.contains(EVENT_MARKER) && converter != null) {
        msg = converter.convertEvent(message, params, throwable);
    } else if (params == null) {
        msg = new SimpleMessage(message);
    } else {
        msg = new ParameterizedMessage(message, params, throwable);
        if (throwable != null) {
            throwable = msg.getThrowable();
        }
    }
    logger.logMessage(fqcn, log4jLevel, log4jMarker, msg, throwable);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Level(org.apache.logging.log4j.Level) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 90 with SimpleMessage

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

the class AbstractLogger method entryMsg.

protected EntryMessage entryMsg(final String format, final Object... params) {
    final int count = params == null ? 0 : params.length;
    if (count == 0) {
        if (Strings.isEmpty(format)) {
            return flowMessageFactory.newEntryMessage(null);
        }
        return flowMessageFactory.newEntryMessage(new SimpleMessage(format));
    }
    if (format != null) {
        return flowMessageFactory.newEntryMessage(new ParameterizedMessage(format, params));
    }
    final StringBuilder sb = new StringBuilder();
    sb.append("params(");
    for (int i = 0; i < count; i++) {
        if (i > 0) {
            sb.append(", ");
        }
        final Object parm = params[i];
        sb.append(parm instanceof Message ? ((Message) parm).getFormattedMessage() : String.valueOf(parm));
    }
    sb.append(')');
    return flowMessageFactory.newEntryMessage(new SimpleMessage(sb));
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) EntryMessage(org.apache.logging.log4j.message.EntryMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) StringFormattedMessage(org.apache.logging.log4j.message.StringFormattedMessage) Message(org.apache.logging.log4j.message.Message) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

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