@Test public void flowTracingString_SupplierOfObjectMessages() { final EntryMessage msg = logger.traceEntry("doFoo(a={}, b={})", new Supplier<Message>() { @Override public Message get() { return new ObjectMessage(1); } }, new Supplier<Message>() { @Override public Message get() { return new ObjectMessage(2); } }); logger.traceExit(msg, 3); assertEquals(2, results.size()); assertThat("Incorrect Entry", results.get(0), startsWith("ENTER[ FLOW ] TRACE Enter")); assertThat("Missing entry data", results.get(0), containsString("doFoo(a=1, b=2)")); assertThat("Incorrect Exit", results.get(1), startsWith("EXIT[ FLOW ] TRACE Exit")); assertThat("Missing exit data", results.get(1), containsString("doFoo(a=1, b=2): 3")); }
private String prepareJSONForObjectMessageAsJsonObjectTests(final int value, final boolean objectMessageAsJsonObject) { final TestClass testClass = new TestClass(); testClass.setValue(value); // @formatter:off final Log4jLogEvent expected = Log4jLogEvent.newBuilder() .setLoggerName("a.B") .setLoggerFqcn("f.q.c.n") .setLevel(Level.DEBUG) .setMessage(new ObjectMessage(testClass)) .setThreadName("threadName") .setTimeMillis(1).build(); // @formatter:off final AbstractJacksonLayout layout = JsonLayout.newBuilder() .setCompact(true) .setObjectMessageAsJsonObject(objectMessageAsJsonObject) .build(); // @formatter:off return layout.toSerializable(expected); }
@Test public void testAppend() throws ServiceException { LogEvent event = Log4jLogEvent.createEvent( "com.c2w.client.log4j2.Corp2WorldAppenderTest", null, "com.c2w.client.log4j2.Corp2WorldAppenderTest", Level.INFO, new ObjectMessage("Test INFO Message"), null, null, null, null, Thread.currentThread().getName(), null, System.currentTimeMillis()); appender.start(); appender.append(event); try { Thread.sleep(1000); } catch (InterruptedException e) { } appender.stop(); new Verifications() {{ service.start(); times=1; service.send(withInstanceOf(com.c2w.client.core.message.Message.class)); times=1; service.stop(); times = 1; }}; }
/** * Tests Category.forcedLog. */ @Test @SuppressWarnings("deprecation") public void testForcedLog() { final MockCategory category = new MockCategory("org.example.foo"); category.setAdditivity(false); category.getLogger().addAppender(appender); category.info("Hello, World"); final List<LogEvent> list = appender.getEvents(); int events = list.size(); assertTrue("Number of events should be 1, was " + events, events == 1); LogEvent event = list.get(0); Message msg = event.getMessage(); assertNotNull("No message", msg); assertTrue("Incorrect Message type", msg instanceof ObjectMessage); Object[] objects = msg.getParameters(); assertTrue("Incorrect Object type", objects[0] instanceof String); appender.clear(); category.log(Priority.INFO, "Hello, World"); events = list.size(); assertTrue("Number of events should be 1, was " + events, events == 1); event = list.get(0); msg = event.getMessage(); assertNotNull("No message", msg); assertTrue("Incorrect Message type", msg instanceof ObjectMessage); objects = msg.getParameters(); assertTrue("Incorrect Object type", objects[0] instanceof String); appender.clear(); }
@Override public void format(LogEvent logEvent, StringBuilder stringBuilder) { if (logEvent.getMessage() instanceof ObjectMessage && logEvent.getMessage().getParameters() != null && logEvent.getMessage().getParameters().length > 0 && logEvent.getMessage().getParameters()[0] != null && logEvent.getMessage().getParameters()[0] instanceof MuleMessage) { stringBuilder.append(formatResult(doConvert((MuleMessage) logEvent.getMessage().getParameters()[0]))); } else { notifyError(String.format("Got log message '%s', but expected a message of type MuleMessage.", logEvent.getMessage())); stringBuilder.append(formatResult(null)); } }
@Test public void flowTracing_SupplierOfObjectMessage() { logger.traceEntry(new Supplier<ObjectMessage>() { @Override public ObjectMessage get() { return new ObjectMessage(1234567890); } }); assertEquals(1, results.size()); assertThat("Incorrect Entry", results.get(0), startsWith("ENTER[ FLOW ] TRACE Enter")); assertThat("Missing entry data", results.get(0), containsString("(1234567890)")); assertThat("Bad toString()", results.get(0), not(containsString("ObjectMessage"))); }
void initialize(final SimpleModule simpleModule, final boolean objectMessageAsJsonObject) { // Workaround because mix-ins do not work for classes that already have a built-in deserializer. // See Jackson issue 429. simpleModule.addDeserializer(StackTraceElement.class, new Log4jStackTraceElementDeserializer()); simpleModule.addDeserializer(ContextStack.class, new MutableThreadContextStackDeserializer()); if (objectMessageAsJsonObject) { simpleModule.addSerializer(ObjectMessage.class, new ObjectMessageSerializer()); } simpleModule.addSerializer(Message.class, new MessageSerializer()); }
@Override public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg, final Throwable t) { final SimpleBindings bindings = new SimpleBindings(); bindings.put("logger", logger); bindings.put("level", level); bindings.put("marker", marker); bindings.put("message", msg instanceof String ? new SimpleMessage((String)msg) : new ObjectMessage(msg)); bindings.put("parameters", null); bindings.put("throwable", t); bindings.putAll(configuration.getProperties()); bindings.put("substitutor", configuration.getStrSubstitutor()); final Object object = configuration.getScriptManager().execute(script.getName(), bindings); return object == null || !Boolean.TRUE.equals(object) ? onMismatch : onMatch; }
@Override protected boolean isEnabled(final Level level, final Marker marker, final Object data, final Throwable t) { return isEnabled(level, marker, new ObjectMessage(data), t); }
public void forcedLog(final String fqcn, final Priority level, final Object message, final Throwable t) { final org.apache.logging.log4j.Level lvl = org.apache.logging.log4j.Level.toLevel(level.toString()); final Message msg = message instanceof Message ? (Message) message : new ObjectMessage(message); logger.log(null, fqcn, lvl, msg, t); }
public void log(final Priority priority, final Object message, final Throwable t) { if (isEnabledFor(priority)) { final Message msg = new ObjectMessage(message); forcedLog(FQCN, priority, msg, t); } }
public void log(final Priority priority, final Object message) { if (isEnabledFor(priority)) { final Message msg = new ObjectMessage(message); forcedLog(FQCN, priority, msg, null); } }
public void log(final String fqcn, final Priority priority, final Object message, final Throwable t) { if (isEnabledFor(priority)) { final Message msg = new ObjectMessage(message); forcedLog(fqcn, priority, msg, t); } }
private void maybeLog(final String fqcn, final org.apache.logging.log4j.Level level, final Object message, final Throwable throwable) { if (logger.isEnabled(level, null, message, throwable)) { logger.log(null, FQCN, level, new ObjectMessage(message), throwable); } }
@Override public boolean isEnabled(final Level level, final Marker marker, final Object data, final Throwable t) { objectCount++; return isEnabled(level, marker, new ObjectMessage(data), t); }
public void forcedLog(final String fqcn, final Priority level, final Object message, final Throwable t) { final org.apache.logging.log4j.Level lvl = org.apache.logging.log4j.Level.toLevel(level.toString()); final Message msg = message instanceof Message ? (Message) message : new ObjectMessage(message); logger.logMessage(fqcn, lvl, null, msg, t); }
private void maybeLog(final String fqcn, final org.apache.logging.log4j.Level level, final Object message, final Throwable throwable) { if (logger.isEnabled(level, null, message, throwable)) { logger.logMessage(FQCN, level, null, new ObjectMessage(message), throwable); } }
ObjectMessageSerializer() { super(ObjectMessage.class); }
@Override public void serialize(final ObjectMessage value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonGenerationException { jgen.writeObject(value.getParameter()); }
@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); logger.log(level, msg); // Message logger.log(level, 123); // Object logger.log(level, name); // String logger.log(level, marker, msg); // Marker, Message logger.log(level, marker, 123); // Marker, Object logger.log(level, marker, name); // marker, String logger.log(level, msg, t); // Message, Throwable logger.log(level, 123, t); // Object, Throwable logger.log(level, name, "param1", "param2"); // String, Object... logger.log(level, name, t); // String, Throwable logger.log(level, marker, msg, t); // Marker, Message, Throwable logger.log(level, marker, 123, t); // Marker, Object, Throwable logger.log(level, marker, name, "param1", "param2"); // Marker, String, Object... logger.log(level, marker, name, t); // Marker, String, Throwable } } // 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); } }