public Message convertEvent(final String message, final Object[] objects, final Throwable throwable) { try { final EventData data = (objects != null && objects[0] instanceof EventData) ? (EventData) objects[0] : new EventData(message); final StructuredDataMessage msg = new StructuredDataMessage(data.getEventId(), data.getMessage(), data.getEventType()); for (final Map.Entry<String, Object> entry : data.getEventMap().entrySet()) { final String key = entry.getKey(); if (EventData.EVENT_TYPE.equals(key) || EventData.EVENT_ID.equals(key) || EventData.EVENT_MESSAGE.equals(key)) { continue; } msg.put(key, String.valueOf(entry.getValue())); } return msg; } catch (final Exception ex) { return new ParameterizedMessage(message, objects, throwable); } }
@Test public void testMultiple() throws InterruptedException, IOException { for (int i = 0; i < 10; ++i) { final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Multiple " + i, "Test"); EventLogger.logEvent(msg); } for (int i = 0; i < 10; ++i) { final Event event = primary.poll(); Assert.assertNotNull(event); final String body = getBody(event); final String expected = "Test Multiple " + i; Assert.assertTrue("Channel contained event, but not expected message. Received: " + body, body.endsWith(expected)); } }
@Test public void testMultiple() throws InterruptedException, IOException { for (int i = 0; i < 10; ++i) { final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Multiple " + i, "Test"); msg.put("counter", Integer.toString(i)); EventLogger.logEvent(msg); } final boolean[] fields = new boolean[10]; for (int i = 0; i < 10; ++i) { final Event event = primary.poll(); Assert.assertNotNull("Received " + i + " events. Event " + (i + 1) + " is null", event); final String value = event.getHeaders().get("counter"); Assert.assertNotNull("Missing counter", value); final int counter = Integer.parseInt(value); if (fields[counter]) { Assert.fail("Duplicate event"); } else { fields[counter] = true; } } for (int i = 0; i < 10; ++i) { Assert.assertTrue("Channel contained event, but not expected message " + i, fields[i]); } }
@Test public void rewriteTest() { final StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a test", "Service"); msg.put("Key1", "Value1"); msg.put("Key2", "Value2"); EventLogger.logEvent(msg); final List<LogEvent> list = app.getEvents(); assertNotNull("No events generated", list); assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1); final LogEvent event = list.get(0); final Message m = event.getMessage(); assertTrue("Message is not a MapMessage", m instanceof MapMessage); final MapMessage message = (MapMessage) m; final Map<String, String> map = message.getData(); assertNotNull("No Map", map); assertTrue("Incorrect number of map entries, expected 3 got " + map.size(), map.size() == 3); final String value = map.get("Key1"); assertEquals("Apache", value); app.clear(); }
public Message convertEvent(final String message, final Object[] objects, final Throwable throwable) { try { final EventData data = objects != null && objects[0] instanceof EventData ? (EventData) objects[0] : new EventData(message); final StructuredDataMessage msg = new StructuredDataMessage(data.getEventId(), data.getMessage(), data.getEventType()); for (final Map.Entry<String, Object> entry : data.getEventMap().entrySet()) { final String key = entry.getKey(); if (EventData.EVENT_TYPE.equals(key) || EventData.EVENT_ID.equals(key) || EventData.EVENT_MESSAGE.equals(key)) { continue; } msg.put(key, String.valueOf(entry.getValue())); } return msg; } catch (final Exception ex) { return new ParameterizedMessage(message, objects, throwable); } }
@Test public void testMultiple() throws IOException { for (int i = 0; i < 10; ++i) { final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Multiple " + i, "Test"); EventLogger.logEvent(msg); } for (int i = 0; i < 10; ++i) { final Event event = primary.poll(); Assert.assertNotNull(event); final String body = getBody(event); final String expected = "Test Multiple " + i; Assert.assertTrue("Channel contained event, but not expected message. Received: " + body, body.endsWith(expected)); } }
@Test public void testMultiple() throws IOException { for (int i = 0; i < 10; ++i) { final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Multiple " + i, "Test"); EventLogger.logEvent(msg); } for (int i = 0; i < 10; ++i) { final Event event = primary.poll(); Assert.assertNotNull("Event should not be null", event); final String body = getBody(event); final String expected = "Test Multiple " + i; Assert.assertTrue("Channel contained event, but not expected message. Received: " + body, body.endsWith(expected)); } }
@Test public void testMultiple() { for (int i = 0; i < 10; ++i) { final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Multiple " + i, "Test"); msg.put("counter", Integer.toString(i)); EventLogger.logEvent(msg); } final boolean[] fields = new boolean[10]; for (int i = 0; i < 10; ++i) { final Event event = primary.poll(); Assert.assertNotNull("Received " + i + " events. Event " + (i + 1) + " is null", event); final String value = event.getHeaders().get("counter"); Assert.assertNotNull("Missing 'counter' in map " + event.getHeaders() + ", i = " + i, value); final int counter = Integer.parseInt(value); if (fields[counter]) { Assert.fail("Duplicate event"); } else { fields[counter] = true; } } for (int i = 0; i < 10; ++i) { Assert.assertTrue("Channel contained event, but not expected message " + i, fields[i]); } }
protected Result filter(final StructuredDataMessage message) { boolean match = false; final IndexedReadOnlyStringMap map = getStringMap(); for (int i = 0; i < map.size(); i++) { final StringBuilder toMatch = getValue(message, map.getKeyAt(i)); if (toMatch != null) { match = listContainsValue((List<String>) map.getValueAt(i), toMatch); } else { match = false; } if ((!isAnd() && match) || (isAnd() && !match)) { break; } } return match ? onMatch : onMismatch; }
private StringBuilder getValue(final StructuredDataMessage data, final String key) { final StringBuilder sb = getStringBuilder(); if (key.equalsIgnoreCase("id")) { data.getId().formatTo(sb); return sb; } else if (key.equalsIgnoreCase("id.name")) { return appendOrNull(data.getId().getName(), sb); } else if (key.equalsIgnoreCase("type")) { return appendOrNull(data.getType(), sb); } else if (key.equalsIgnoreCase("message")) { data.formatTo(sb); return sb; } else { return appendOrNull(data.get(key), sb); } }
private void appendMessage(final StringBuilder buffer, final LogEvent event) { final Message message = event.getMessage(); // This layout formats StructuredDataMessages instead of delegating to the Message itself. final String text = (message instanceof StructuredDataMessage || message instanceof MessageCollectionMessage) ? message.getFormat() : message.getFormattedMessage(); if (text != null && text.length() > 0) { buffer.append(' ').append(escapeNewlines(text, escapeNewLine)); } if (exceptionFormatters != null && event.getThrown() != null) { final StringBuilder exception = new StringBuilder(LF); for (final PatternFormatter formatter : exceptionFormatters) { formatter.format(event, exception); } buffer.append(escapeNewlines(exception.toString(), escapeNewLine)); } if (includeNewLine) { buffer.append(LF); } }
@Test public void rewriteTest() { final StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a test", "Service"); msg.put("Key1", "Value1"); msg.put("Key2", "Value2"); EventLogger.logEvent(msg); final List<LogEvent> list = app.getEvents(); assertNotNull("No events generated", list); assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1); final LogEvent event = list.get(0); final Message m = event.getMessage(); assertTrue("Message is not a StringMapMessage: " + m.getClass(), m instanceof StructuredDataMessage); final StructuredDataMessage message = (StructuredDataMessage) m; final Map<String, String> map = message.getData(); assertNotNull("No Map", map); assertTrue("Incorrect number of map entries, expected 3 got " + map.size(), map.size() == 3); final String value = map.get("Key1"); assertEquals("Apache", value); }
@Test public void addTest() { final MapRewritePolicy addPolicy = MapRewritePolicy.createPolicy("Add", rewrite); LogEvent rewritten = addPolicy.rewrite(logEvent0); compareLogEvents(logEvent0, rewritten); assertEquals("Simple log message changed", logEvent0.getMessage(), rewritten.getMessage()); rewritten = addPolicy.rewrite(logEvent1); compareLogEvents(logEvent1, rewritten); checkAdded(((StringMapMessage)rewritten.getMessage()).getData()); rewritten = addPolicy.rewrite(logEvent2); compareLogEvents(logEvent2, rewritten); checkAdded(((StructuredDataMessage)rewritten.getMessage()).getData()); rewritten = addPolicy.rewrite(logEvent3); compareLogEvents(logEvent3, rewritten); checkAdded(((StringMapMessage)rewritten.getMessage()).getData()); }
@Test public void updateTest() { final MapRewritePolicy updatePolicy = MapRewritePolicy.createPolicy("Update", rewrite); LogEvent rewritten = updatePolicy.rewrite(logEvent0); compareLogEvents(logEvent0, rewritten); assertEquals("Simple log message changed", logEvent0.getMessage(), rewritten.getMessage()); rewritten = updatePolicy.rewrite(logEvent1); compareLogEvents(logEvent1, rewritten); checkUpdated(((StringMapMessage)rewritten.getMessage()).getData()); rewritten = updatePolicy.rewrite(logEvent2); compareLogEvents(logEvent2, rewritten); checkUpdated(((StructuredDataMessage)rewritten.getMessage()).getData()); rewritten = updatePolicy.rewrite(logEvent3); compareLogEvents(logEvent3, rewritten); checkUpdated(((StringMapMessage)rewritten.getMessage()).getData()); }
@Test public void defaultIsAdd() { final MapRewritePolicy addPolicy = MapRewritePolicy.createPolicy(null, rewrite); LogEvent rewritten = addPolicy.rewrite(logEvent0); compareLogEvents(logEvent0, rewritten); assertEquals("Simple log message changed", logEvent0.getMessage(), rewritten.getMessage()); rewritten = addPolicy.rewrite(logEvent1); compareLogEvents(logEvent1, rewritten); checkAdded(((StringMapMessage)rewritten.getMessage()).getData()); rewritten = addPolicy.rewrite(logEvent2); compareLogEvents(logEvent2, rewritten); checkAdded(((StructuredDataMessage)rewritten.getMessage()).getData()); rewritten = addPolicy.rewrite(logEvent3); compareLogEvents(logEvent3, rewritten); checkAdded(((StringMapMessage)rewritten.getMessage()).getData()); }
/** * filter that populates the MDC on every request. */ public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { StructuredDataMessage msg = new StructuredDataMessage("", "", "request trace"); HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; msg.put("ipAddress", request.getRemoteAddr()); HttpSession session = request.getSession(false); TimeZone timeZone = null; if (session != null) { // This assumes there is some javascript on the user's page to create the cookie. if (session.getAttribute(TZ_NAME) == null) { if (request.getCookies() != null) { for (Cookie cookie : request.getCookies()) { if (TZ_NAME.equals(cookie.getName())) { int tzOffsetMinutes = Integer.parseInt(cookie.getValue()); timeZone = TimeZone.getTimeZone("GMT"); timeZone.setRawOffset((int)(tzOffsetMinutes * DateUtils.MILLIS_PER_MINUTE)); request.getSession().setAttribute(TZ_NAME, tzOffsetMinutes); cookie.setMaxAge(0); response.addCookie(cookie); } } } } } msg.put("hostname", servletRequest.getServerName()); msg.put("locale", servletRequest.getLocale().getDisplayName()); if (timeZone == null) { timeZone = TimeZone.getDefault(); } msg.put("timezone", timeZone.getDisplayName()); filterChain.doFilter(servletRequest, servletResponse); logger.trace(msg); }
@Test public void testStructured() throws InterruptedException, IOException { final Agent[] agents = new Agent[] { Agent.createAgent("localhost", testPort) }; final FlumeAppender avroAppender = FlumeAppender.createAppender(agents, null, "false", "Avro", null, "1000", "1000", "1", "1000", "avro", "false", null, null, null, "ReqCtx_", null, "true", "1", null, null, null, null); avroAppender.start(); final Logger eventLogger = (Logger) LogManager.getLogger("EventLogger"); Assert.assertNotNull(eventLogger); eventLogger.addAppender(avroAppender); eventLogger.setLevel(Level.ALL); final StructuredDataMessage msg = new StructuredDataMessage("Transfer", "Success", "Audit"); msg.put("memo", "This is a memo"); msg.put("acct", "12345"); msg.put("amount", "100.00"); ThreadContext.put("id", UUID.randomUUID().toString()); ThreadContext.put("memo", null); ThreadContext.put("test", "123"); EventLogger.logEvent(msg); final Transaction transaction = channel.getTransaction(); transaction.begin(); final Event event = channel.take(); Assert.assertNotNull(event); Assert.assertTrue("Channel contained event, but not expected message", getBody(event).endsWith("Success")); transaction.commit(); transaction.close(); eventSource.stop(); eventLogger.removeAppender(avroAppender); avroAppender.stop(); }
@Test public void testLog4Event() throws InterruptedException, IOException { final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Log4j", "Test"); EventLogger.logEvent(msg); final Event event = primary.poll(); Assert.assertNotNull(event); final String body = getBody(event); Assert.assertTrue("Channel contained event, but not expected message. Received: " + body, body.endsWith("Test Log4j")); }
@Test public void testPerformance() throws Exception { final long start = System.currentTimeMillis(); final int count = 10000; for (int i = 0; i < count; ++i) { final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Primary " + i, "Test"); msg.put("counter", Integer.toString(i)); EventLogger.logEvent(msg); } final long elapsed = System.currentTimeMillis() - start; System.out.println("Time to log " + count + " events " + elapsed + "ms"); }
@Test public void testHeaderAddedByInterceptor() throws InterruptedException, IOException { final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Log4j", "Test"); EventLogger.logEvent(msg); final Event event = primary.poll(); Assert.assertNotNull(event); final String environmentHeader = event.getHeaders().get("environment"); Assert.assertEquals("local", environmentHeader); }
public void run() { for (int i = start; i < stop; ++i) { final StructuredDataMessage msg = new StructuredDataMessage( "Test", "Test Multiple " + i, "Test"); msg.put("counter", Integer.toString(i)); EventLogger.logEvent(msg); } }
@SuppressWarnings("unchecked") public static <T extends AuditEvent> T getEvent(final Class<T> intrface) { final String eventId = NamingUtils.lowerFirst(intrface.getSimpleName()); final StructuredDataMessage msg = new StructuredDataMessage(eventId, null, "Audit"); return (T)Proxy.newProxyInstance(intrface .getClassLoader(), new Class<?>[]{intrface}, new AuditProxy(msg, intrface)); }
/** * Lookup the value for the key using the data in the LogEvent. * @param event The current LogEvent. * @param key the key to be looked up, may be null * @return The value associated with the key. */ @Override public String lookup(final LogEvent event, final String key) { if (event == null || !(event.getMessage() instanceof StructuredDataMessage)) { return null; } final StructuredDataMessage msg = (StructuredDataMessage) event.getMessage(); if (key.equalsIgnoreCase("id")) { return msg.getId().getName(); } else if (key.equalsIgnoreCase("type")) { return msg.getType(); } return msg.get(key); }
@Override public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg, final Throwable t) { if (msg instanceof StructuredDataMessage) { return filter((StructuredDataMessage) msg); } return Result.NEUTRAL; }
@Override public Result filter(final LogEvent event) { final Message msg = event.getMessage(); if (msg instanceof StructuredDataMessage) { return filter((StructuredDataMessage) msg); } return super.filter(event); }
protected Result filter(final StructuredDataMessage message) { boolean match = false; for (final Map.Entry<String, List<String>> entry : getMap().entrySet()) { final String toMatch = getValue(message, entry.getKey()); if (toMatch != null) { match = entry.getValue().contains(toMatch); } else { match = false; } if ((!isAnd() && match) || (isAnd() && !match)) { break; } } return match ? onMatch : onMismatch; }
private String getValue(final StructuredDataMessage data, final String key) { if (key.equalsIgnoreCase("id")) { return data.getId().toString(); } else if (key.equalsIgnoreCase("id.name")) { return data.getId().getName(); } else if (key.equalsIgnoreCase("type")) { return data.getType(); } else if (key.equalsIgnoreCase("message")) { return data.getFormattedMessage(); } else { return data.getData().get(key); } }
private void appendMessageId(final StringBuilder buffer, final Message message) { final boolean isStructured = message instanceof StructuredDataMessage; final String type = isStructured ? ((StructuredDataMessage) message).getType() : null; if (type != null) { buffer.append(type); } else if (messageId != null) { buffer.append(messageId); } else { buffer.append("-"); } }
@Test public void testLookup() { final Message msg = new StructuredDataMessage("Test", "This is a test", "Audit"); final Marker eventMarker = MarkerManager.getMarker("EVENT"); final Marker auditMarker = MarkerManager.getMarker("AUDIT", eventMarker); final LogEvent event = new Log4jLogEvent("MyLogger", auditMarker, null, Level.DEBUG, msg, null); final StringBuilder sb = new StringBuilder(); final MarkerPatternConverter converter = MarkerPatternConverter.newInstance(null); converter.format(event, sb); assertEquals(auditMarker.toString(), sb.toString()); }
@Test public void testLookup() { final Message msg = new StructuredDataMessage("Test", "This is a test", "Audit"); final LogEvent event = new Log4jLogEvent(null, null, null, Level.DEBUG, msg, null); final StrLookup lookup = new StructuredDataLookup(); String value = lookup.lookup(event, TESTKEY); assertEquals(TESTVAL, value); value = lookup.lookup("BadKey"); assertNull(value); }
@BeforeClass public static void setupClass() { map.put("test1", "one"); map.put("test2", "two"); logEvent0 = new Log4jLogEvent("test", null, "MapRewritePolicyTest.setupClass()", Level.ERROR, new SimpleMessage("Test"), new RuntimeException("test"), map, null, "none", new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 28), 2); logEvent1 = new Log4jLogEvent("test", null, "MapRewritePolicyTest.setupClass()", Level.ERROR, new MapMessage(map), null, map, null, "none", new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 29), 2); final ThreadContextStack stack = new MutableThreadContextStack(new ArrayList<String>(map.values())); logEvent2 = new Log4jLogEvent("test", MarkerManager.getMarker("test"), "MapRewritePolicyTest.setupClass()", Level.TRACE, new StructuredDataMessage("test", "Nothing", "test", map), new RuntimeException("test"), null, stack, "none", new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 30), 20000000); logEvent3 = new Log4jLogEvent("test", null, "MapRewritePolicyTest.setupClass()", Level.ALL, new MapMessage(map), null, map, stack, null, new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 31), Long.MAX_VALUE); rewrite = new KeyValuePair[] {new KeyValuePair("test2", "2"), new KeyValuePair("test3", "three")}; }