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();
}
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");
}
}
}
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;
}
}
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);
}
}
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);
}
Aggregations