Search in sources :

Example 1 with Message

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

the class StringBuilderPool method createLog4j2Event.

private static LogEvent createLog4j2Event() {
    final Marker marker = null;
    final String fqcn = "com.mycom.myproject.mypackage.MyClass";
    final Level level = Level.DEBUG;
    // length=32
    final String STR = "AB!(%087936DZYXQWEIOP$#^~-=/><nb";
    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 2 with Message

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

the class MessagePatternConverter method format.

/**
     * {@inheritDoc}
     */
@Override
public void format(final LogEvent event, final StringBuilder toAppendTo) {
    final Message msg = event.getMessage();
    if (msg instanceof StringBuilderFormattable) {
        final boolean doRender = textRenderer != null;
        final StringBuilder workingBuilder = doRender ? new StringBuilder(80) : toAppendTo;
        final StringBuilderFormattable stringBuilderFormattable = (StringBuilderFormattable) msg;
        final int offset = workingBuilder.length();
        stringBuilderFormattable.formatTo(workingBuilder);
        // TODO can we optimize this?
        if (config != null && !noLookups) {
            for (int i = offset; i < workingBuilder.length() - 1; i++) {
                if (workingBuilder.charAt(i) == '$' && workingBuilder.charAt(i + 1) == '{') {
                    final String value = workingBuilder.substring(offset, workingBuilder.length());
                    workingBuilder.setLength(offset);
                    workingBuilder.append(config.getStrSubstitutor().replace(event, value));
                }
            }
        }
        if (doRender) {
            textRenderer.render(workingBuilder, toAppendTo);
        }
        return;
    }
    if (msg != null) {
        String result;
        if (msg instanceof MultiformatMessage) {
            result = ((MultiformatMessage) msg).getFormattedMessage(formats);
        } else {
            result = msg.getFormattedMessage();
        }
        if (result != null) {
            toAppendTo.append(config != null && result.contains("${") ? config.getStrSubstitutor().replace(event, result) : result);
        } else {
            toAppendTo.append("null");
        }
    }
}
Also used : MultiformatMessage(org.apache.logging.log4j.message.MultiformatMessage) Message(org.apache.logging.log4j.message.Message) StringBuilderFormattable(org.apache.logging.log4j.util.StringBuilderFormattable) MultiformatMessage(org.apache.logging.log4j.message.MultiformatMessage)

Example 3 with Message

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

the class CsvParameterLayout method toSerializable.

@Override
public String toSerializable(final LogEvent event) {
    final Message message = event.getMessage();
    final Object[] parameters = message.getParameters();
    final StringBuilder buffer = getStringBuilder();
    try {
        getFormat().printRecord(buffer, parameters);
        return buffer.toString();
    } catch (final IOException e) {
        StatusLogger.getLogger().error(message, e);
        return getFormat().getCommentMarker() + " " + e;
    }
}
Also used : Message(org.apache.logging.log4j.message.Message) IOException(java.io.IOException)

Example 4 with Message

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

the class LevelTest method testLevelLogging.

@Test
public void testLevelLogging() {
    final Marker marker = MarkerManager.getMarker("marker");
    final Message msg = new ObjectMessage("msg");
    final Throwable t = new Throwable("test");
    final Level[] levels = new Level[] { Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL };
    final String[] names = new String[] { "levelTest", "levelTest.Trace", "levelTest.Debug", "levelTest.Info", "levelTest.Warn", "levelTest.Error", "levelTest.Fatal" };
    for (final Level level : levels) {
        for (final String name : names) {
            final Logger logger = context.getLogger(name);
            // Message
            logger.log(level, msg);
            // Object
            logger.log(level, 123);
            // String
            logger.log(level, name);
            // Marker, Message
            logger.log(level, marker, msg);
            // Marker, Object
            logger.log(level, marker, 123);
            // marker, String
            logger.log(level, marker, name);
            // Message, Throwable
            logger.log(level, msg, t);
            // Object, Throwable
            logger.log(level, 123, t);
            // String, Object...
            logger.log(level, name, "param1", "param2");
            // String, Throwable
            logger.log(level, name, t);
            // Marker, Message, Throwable
            logger.log(level, marker, msg, t);
            // Marker, Object, Throwable
            logger.log(level, marker, 123, t);
            // Marker, String, Object...
            logger.log(level, marker, name, "param1", "param2");
            // Marker, String, Throwable
            logger.log(level, marker, name, t);
        }
    }
    // Logger "levelTest" will not receive same events as "levelTest.Trace"
    int levelCount = names.length - 1;
    final int UNIT = 14;
    final Expected[] expectedResults = new Expected[] { //
    new Expected(listAll, UNIT * levelCount, "TRACE", "All"), //
    new Expected(listTrace, UNIT * levelCount--, "TRACE", "Trace"), //
    new Expected(listDebug, UNIT * levelCount--, "DEBUG", "Debug"), //
    new Expected(listInfo, UNIT * levelCount--, "INFO", "Info"), //
    new Expected(listWarn, UNIT * levelCount--, "WARN", "Warn"), //
    new Expected(listError, UNIT * levelCount--, "ERROR", "Error"), //
    new Expected(listFatal, UNIT * levelCount--, "FATAL", "Fatal") };
    for (final Expected expected : expectedResults) {
        final String description = expected.description;
        final List<LogEvent> events = expected.appender.getEvents();
        assertNotNull(description + ": No events", events);
        assertThat(events, hasSize(expected.expectedEventCount));
        final LogEvent event = events.get(0);
        assertEquals(description + ": Expected level " + expected.expectedInitialEventLevel + ", got" + event.getLevel(), event.getLevel().name(), expected.expectedInitialEventLevel);
    }
}
Also used : ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Message(org.apache.logging.log4j.message.Message) Marker(org.apache.logging.log4j.Marker) Logger(org.apache.logging.log4j.Logger) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Level(org.apache.logging.log4j.Level) Test(org.junit.Test)

Example 5 with Message

use of org.apache.logging.log4j.message.Message in project syncope by apache.

the class MemoryAppender method append.

@Override
public void append(final LogEvent event) {
    LogStatementTO statement = new LogStatementTO();
    statement.setLevel(LoggerLevel.fromLevel(event.getLevel()));
    statement.setLoggerName(event.getLoggerName());
    Message msg = event.getMessage();
    statement.setMessage((msg instanceof ReusableMessage ? ((ReusableMessage) msg).memento() : msg).getFormattedMessage());
    statement.setTimeMillis(event.getTimeMillis());
    if (event.getThrown() != null) {
        statement.setStackTrace(ExceptionUtils2.getFullStackTrace(event.getThrown()));
    }
    statement.setThreadId(event.getThreadId());
    statement.setThreadName(event.getThreadName());
    statement.setThreadPriority(event.getThreadPriority());
    this.statements.add(statement);
}
Also used : ReusableMessage(org.apache.logging.log4j.message.ReusableMessage) Message(org.apache.logging.log4j.message.Message) ReusableMessage(org.apache.logging.log4j.message.ReusableMessage) LogStatementTO(org.apache.syncope.common.lib.log.LogStatementTO)

Aggregations

Message (org.apache.logging.log4j.message.Message)125 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)86 LogEvent (org.apache.logging.log4j.core.LogEvent)54 Test (org.junit.jupiter.api.Test)50 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)45 Marker (org.apache.logging.log4j.Marker)26 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)26 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)24 Level (org.apache.logging.log4j.Level)23 Test (org.junit.Test)22 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)19 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)18 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)17 MapMessage (org.apache.logging.log4j.message.MapMessage)15 StringMap (org.apache.logging.log4j.util.StringMap)15 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)9 EntryMessage (org.apache.logging.log4j.message.EntryMessage)9 Map (java.util.Map)8 StringFormattedMessage (org.apache.logging.log4j.message.StringFormattedMessage)8 IOException (java.io.IOException)7