/** * Validate the raw HTTP data * * @param item * The validation item * @return TRUE if matches; FALSE otherwise */ private String validateHTTP(ValidationData original, ValidationData item, Variables variables, int httpCode, String httpMsg, String uniqueName) { String actualValue = null; if (item.getKey().equalsIgnoreCase("HTTPRESPONSECODE")) { actualValue = "" + httpCode; } else if (item.getKey().equalsIgnoreCase("HTTPRESPONSEMESSAGE")) { actualValue = httpMsg; } boolean result = this.evaluateResult(actualValue, item.getValue(), item.getCondition(), variables); if (result) { return TankConstants.HTTP_CASE_PASS; } String msg = "Failed http validation: value = " + actualValue; LOG.error( new MapMessage(LogUtil.getLogMessage(item.toString() + " " + msg, LogEventType.Validation))); tsc.addError(new ErrorContainer("HTTP_CODE", original, item, msg)); return TankConstants.HTTP_CASE_FAIL; }
/** * Validate an item against the response body * * @param item * The validation item * @return TRUE if matches; FALSE otherwise */ private String validateBody(ValidationData original, ValidationData item, Variables variables, BaseResponse reqResponse, String uniqueName) { String actualValue = reqResponse.getValue(item.getKey()); LOG.debug("Body compare actual value: " + actualValue); boolean result = evaluateResult(actualValue, item.getValue(), item.getCondition(), variables); if (result) { return TankConstants.HTTP_CASE_PASS; } String msg = "Failed body validation: body value = " + actualValue; LOG.error( new MapMessage(LogUtil.getLogMessage("Validation Failed: " + item.toString() + " " + msg, LogEventType.Validation, LoggingProfile.VERBOSE))); tsc.addError(new ErrorContainer("BODY", original, item, msg)); return TankConstants.HTTP_CASE_FAIL; }
@Override public String lookup(final LogEvent event, final String key) { if (map == null && !(event.getMessage() instanceof MapMessage)) { return null; } if (map != null && map.containsKey(key)) { final String obj = map.get(key); if (obj != null) { return obj; } } if (event.getMessage() instanceof MapMessage) { return ((MapMessage) event.getMessage()).get(key); } return null; }
@Test public void testConverter() { final MapMessage msg = new MapMessage(); msg.put("subject", "I"); msg.put("verb", "love"); msg.put("object", "Log4j"); final MapPatternConverter converter = MapPatternConverter.newInstance(null); final LogEvent event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String str = sb.toString(); String expected = "subject=I"; assertTrue("Missing or incorrect subject. Expected " + expected + ", actual " + str, str.contains(expected)); expected = "verb=love"; assertTrue("Missing or incorrect verb", str.contains(expected)); expected = "object=Log4j"; assertTrue("Missing or incorrect object", str.contains(expected)); }
@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(); }
@Test public void addTest() { final MapRewritePolicy addPolicy = MapRewritePolicy.createPolicy("Add", rewrite); LogEvent rewritten = addPolicy.rewrite(logEvent0); compareLogEvents(logEvent0, rewritten); Assert.assertEquals("Simple log message changed", logEvent0.getMessage(), rewritten.getMessage()); rewritten = addPolicy.rewrite(logEvent1); compareLogEvents(logEvent1, rewritten); checkAdded(((MapMessage)rewritten.getMessage()).getData()); rewritten = addPolicy.rewrite(logEvent2); compareLogEvents(logEvent2, rewritten); checkAdded(((MapMessage)rewritten.getMessage()).getData()); rewritten = addPolicy.rewrite(logEvent3); compareLogEvents(logEvent3, rewritten); checkAdded(((MapMessage)rewritten.getMessage()).getData()); }
@Test public void updateTest() { final MapRewritePolicy updatePolicy = MapRewritePolicy.createPolicy("Update", rewrite); LogEvent rewritten = updatePolicy.rewrite(logEvent0); compareLogEvents(logEvent0, rewritten); Assert.assertEquals("Simple log message changed", logEvent0.getMessage(), rewritten.getMessage()); rewritten = updatePolicy.rewrite(logEvent1); compareLogEvents(logEvent1, rewritten); checkUpdated(((MapMessage)rewritten.getMessage()).getData()); rewritten = updatePolicy.rewrite(logEvent2); compareLogEvents(logEvent2, rewritten); checkUpdated(((MapMessage)rewritten.getMessage()).getData()); rewritten = updatePolicy.rewrite(logEvent3); compareLogEvents(logEvent3, rewritten); checkUpdated(((MapMessage)rewritten.getMessage()).getData()); }
@Test public void defaultIsAdd() { final MapRewritePolicy addPolicy = MapRewritePolicy.createPolicy(null, rewrite); LogEvent rewritten = addPolicy.rewrite(logEvent0); compareLogEvents(logEvent0, rewritten); Assert.assertEquals("Simple log message changed", logEvent0.getMessage(), rewritten.getMessage()); rewritten = addPolicy.rewrite(logEvent1); compareLogEvents(logEvent1, rewritten); checkAdded(((MapMessage)rewritten.getMessage()).getData()); rewritten = addPolicy.rewrite(logEvent2); compareLogEvents(logEvent2, rewritten); checkAdded(((MapMessage)rewritten.getMessage()).getData()); rewritten = addPolicy.rewrite(logEvent3); compareLogEvents(logEvent3, rewritten); checkAdded(((MapMessage)rewritten.getMessage()).getData()); }
@Override protected void writeInternal(final LogEvent event, final Serializable serializable) { if (!this.isRunning() || this.connection == null || this.connection.isClosed()) { throw new AppenderLoggingException( "Cannot write logging event; NoSQL manager not connected to the database."); } final NoSqlObject<W> entity = this.connection.createObject(); if (serializable instanceof MapMessage) { setFields((MapMessage<?, ?>) serializable, entity); } else { setFields(event, entity); } this.connection.insertObject(entity); }
@Test public void test() { final Logger logger = LogManager.getLogger(); final MapMessage mapMessage = new MapMessage(); mapMessage.with("SomeName", "SomeValue"); mapMessage.with("SomeInt", 1); logger.info(mapMessage); // try (final MongoClient mongoClient = mongoDbTestRule.getMongoClient()) { final MongoDatabase database = mongoClient.getDatabase("test"); Assert.assertNotNull(database); final MongoCollection<Document> collection = database.getCollection("applog"); Assert.assertNotNull(collection); final Document first = collection.find().first(); Assert.assertNotNull(first); final String firstJson = first.toJson(); Assert.assertEquals(firstJson, "SomeValue", first.getString("SomeName")); Assert.assertEquals(firstJson, Integer.valueOf(1), first.getInteger("SomeInt")); } }
private void setFields(final MapMessage<?, ?> mapMessage) throws SQLException { final IndexedReadOnlyStringMap map = mapMessage.getIndexedReadOnlyStringMap(); final String simpleName = statement.getClass().getName(); int i = 1; // JDBC indices start at 1 for (final ColumnMapping mapping : this.columnMappings) { final String source = mapping.getSource(); final String key = Strings.isEmpty(source) ? mapping.getName() : source; final Object value = map.getValue(key); if (logger().isTraceEnabled()) { final String valueStr = value instanceof String ? "\"" + value + "\"" : Objects.toString(value, null); logger().trace("{} setObject({}, {}) for key '{}' and mapping '{}'", simpleName, i, valueStr, key, mapping.getName()); } statement.setObject(i++, value); } }
@Test public void test() { final Logger logger = LogManager.getLogger(); final MapMessage mapMessage = new MapMessage(); mapMessage.with("SomeName", "SomeValue"); mapMessage.with("SomeInt", 1); logger.info(mapMessage); // final MongoClient mongoClient = mongoDbTestRule.getMongoClient(); try { final DB database = mongoClient.getDB("test"); Assert.assertNotNull(database); final DBCollection collection = database.getCollection("applog"); Assert.assertNotNull(collection); final DBObject first = collection.find().next(); Assert.assertNotNull(first); final String firstMapString = first.toMap().toString(); Assert.assertEquals(firstMapString, "SomeValue", first.get("SomeName")); Assert.assertEquals(firstMapString, Integer.valueOf(1), first.get("SomeInt")); } finally { mongoClient.close(); } }
/** * Validate an item against the response header * * @param item * The validation item * @return TRUE if matches; FALSE otherwise */ private String validateHeader(ValidationData original, ValidationData item, Variables variables, BaseResponse reqResponse, String uniqueName) { String actualValue = reqResponse.getHttpHeader(item.getKey()); boolean result = this.evaluateResult(actualValue, item.getValue(), item.getCondition(), variables); if (result) { return TankConstants.HTTP_CASE_PASS; } String msg = "Failed header validation: header value = " + actualValue; LOG.error( new MapMessage(LogUtil.getLogMessage(item.toString() + " " + msg, LogEventType.Validation))); tsc.addError(new ErrorContainer("HEADER", original, item, msg)); return TankConstants.HTTP_CASE_FAIL; }
/** * Validate an item against the cookies * * @param item * The validation item * @param item * @return TRUE if matches; FALSE otherwise */ private String validateCookie(ValidationData original, ValidationData item, Variables variables, BaseResponse reqResponse, String uniqueName) { String actualValue = reqResponse.getCookie(item.getKey()); LOG.debug("Cookie compare actual value: " + actualValue); boolean result = evaluateResult(actualValue, item.getValue(), item.getCondition(), variables); if (result) { return TankConstants.HTTP_CASE_PASS; } String msg = "Failed cookie validation: cookie value = " + actualValue; LOG.error( new MapMessage(LogUtil.getLogMessage(item.toString() + " " + msg, LogEventType.Validation))); tsc.addError(new ErrorContainer("COOKIE", original, item, msg)); return TankConstants.HTTP_CASE_FAIL; }
/** * Rewrite the event. * @param source a logging event that may be returned or * used to create a new logging event. * @return The LogEvent after rewriting. */ @Override public LogEvent rewrite(final LogEvent source) { final Message msg = source.getMessage(); if (msg == null || !(msg instanceof MapMessage)) { return source; } final Map<String, String> newMap = new HashMap<String, String>(((MapMessage) msg).getData()); switch (mode) { case Add: { newMap.putAll(map); break; } default: { for (final Map.Entry<String, String> entry : map.entrySet()) { if (newMap.containsKey(entry.getKey())) { newMap.put(entry.getKey(), entry.getValue()); } } } } final MapMessage message = ((MapMessage) msg).newInstance(newMap); if (source instanceof Log4jLogEvent) { Log4jLogEvent event = (Log4jLogEvent) source; return Log4jLogEvent.createEvent(event.getLoggerName(), event.getMarker(), event.getFQCN(), event.getLevel(), message, event.getThrownProxy(), event.getContextMap(), event.getContextStack(), event.getThreadName(), event.getSource(), event.getMillis()); } return new Log4jLogEvent(source.getLoggerName(), source.getMarker(), source.getFQCN(), source.getLevel(), message, source.getThrown(), source.getContextMap(), source.getContextStack(), source.getThreadName(), source.getSource(), source.getMillis()); }
@Override public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg, final Throwable t) { if (msg instanceof MapMessage) { return filter(((MapMessage) msg).getData()) ? onMatch : onMismatch; } return Result.NEUTRAL; }
@Override public Result filter(final LogEvent event) { final Message msg = event.getMessage(); if (msg instanceof MapMessage) { return filter(((MapMessage) msg).getData()) ? onMatch : onMismatch; } return Result.NEUTRAL; }
@Test public void testConverterWithKey() { final MapMessage msg = new MapMessage(); msg.put("subject", "I"); msg.put("verb", "love"); msg.put("object", "Log4j"); final MapPatternConverter converter = MapPatternConverter.newInstance(new String[] {"object"}); final LogEvent event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String str = sb.toString(); final String expected = "Log4j"; assertEquals(expected, str); }
@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")}; }
@Test public void testSimpleMap() { final Logger logger = LogManager.getLogger(CollectionLoggingTest.class.getName()); logger.error(System.getProperties()); final Map<String, String> map = new HashMap<String, String>(); map.put("MyKey1", "MyValue1"); map.put("MyKey2", "MyValue2"); logger.error(new MapMessage(map)); logger.error(map); }
@Test public void testConfig() { final LoggerContext ctx = Configurator.initialize("Test1", "target/test-classes/log4j2-mapfilter.xml"); final Configuration config = ctx.getConfiguration(); final Filter filter = config.getFilter(); assertNotNull("No MapFilter", filter); assertTrue("Not a MapFilter", filter instanceof MapFilter); final MapFilter mapFilter = (MapFilter) filter; assertFalse("Should not be And filter", mapFilter.isAnd()); final Map<String, List<String>> map = mapFilter.getMap(); assertNotNull("No Map", map); assertTrue("No elements in Map", map.size() != 0); assertTrue("Incorrect number of elements in Map", map.size() == 1); assertTrue("Map does not contain key eventId", map.containsKey("eventId")); assertTrue("List does not contain 2 elements", map.get("eventId").size() == 2); final Logger logger = LogManager.getLogger(MapFilterTest.class); final Map<String, String> eventMap = new HashMap<String, String>(); eventMap.put("eventId", "Login"); logger.debug(new MapMessage(eventMap)); final Map<String,Appender> appenders = config.getAppenders(); final Appender app = appenders.get("LIST"); assertNotNull("No List appender", app); final List<String> msgs = ((ListAppender) app).getMessages(); assertNotNull("No messages", msgs); assertTrue("No messages", msgs.size() > 0); }
private void setFields(final MapMessage<?, ?> mapMessage, final NoSqlObject<W> noSqlObject) { // Map without calling org.apache.logging.log4j.message.MapMessage#getData() which makes a copy of the map. mapMessage.forEach(new BiConsumer<String, Object>() { @Override public void accept(final String key, final Object value) { noSqlObject.set(key, value); } }); }
/** * Rewrite the event. * @param source a logging event that may be returned or * used to create a new logging event. * @return The LogEvent after rewriting. */ @Override public LogEvent rewrite(final LogEvent source) { final Message msg = source.getMessage(); if (msg == null || !(msg instanceof MapMessage)) { return source; } @SuppressWarnings("unchecked") final MapMessage<?, Object> mapMsg = (MapMessage<?, Object>) msg; final Map<String, Object> newMap = new HashMap<>(mapMsg.getData()); switch (mode) { case Add: { newMap.putAll(map); break; } default: { for (final Map.Entry<String, Object> entry : map.entrySet()) { if (newMap.containsKey(entry.getKey())) { newMap.put(entry.getKey(), entry.getValue()); } } } } final Message message = mapMsg.newInstance(newMap); return new Log4jLogEvent.Builder(source).setMessage(message).build(); }
@Override public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg, final Throwable t) { if (msg instanceof MapMessage) { return filter((MapMessage<?, ?>) msg) ? onMatch : onMismatch; } return Result.NEUTRAL; }
@Override public Result filter(final LogEvent event) { final Message msg = event.getMessage(); if (msg instanceof MapMessage) { return filter((MapMessage<?, ?>) msg) ? onMatch : onMismatch; } return Result.NEUTRAL; }
protected boolean filter(final MapMessage<?, ?> mapMessage) { boolean match = false; for (int i = 0; i < map.size(); i++) { final String toMatch = mapMessage.get(map.getKeyAt(i)); match = toMatch != null && ((List<String>) map.getValueAt(i)).contains(toMatch); if ((!isAnd && match) || (isAnd && !match)) { break; } } return match; }
@Test public void testDataSourceConfig() throws Exception { try (final Connection connection = jdbcRule.getConnectionSource().getConnection()) { final Error exception = new Error("Final error massage is fatal!"); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final PrintWriter writer = new PrintWriter(outputStream); exception.printStackTrace(writer); writer.close(); final Logger logger = LogManager.getLogger(this.getClass().getName() + ".testDataSourceConfig"); MapMessage mapMessage = new MapMessage(); mapMessage.with("Id", 1); mapMessage.with("ColumnA", "ValueA"); mapMessage.with("ColumnB", "ValueB"); logger.info(mapMessage); try (final Statement statement = connection.createStatement(); final ResultSet resultSet = statement .executeQuery("SELECT Id, ColumnA, ColumnB FROM dsLogEntry ORDER BY Id")) { assertTrue("There should be at least one row.", resultSet.next()); Assert.assertEquals(1, resultSet.getInt("Id")); assertFalse("There should not be two rows.", resultSet.next()); } } }
@Override public String execute() { String validation = TankConstants.HTTP_CASE_PASS; if (APITestHarness.getInstance().getAgentRunData().getActiveProfile() == LoggingProfile.TRACE) { LOG.info( new MapMessage(LogUtil.getLogMessage("Executing step..."))); } if (checkPreValidations()) { if (LOG.isDebugEnabled() || APITestHarness.getInstance().getAgentRunData().getActiveProfile() == LoggingProfile.VERBOSE || APITestHarness.getInstance().getAgentRunData().getActiveProfile() == LoggingProfile.TRACE) { LOG.info( new MapMessage(LogUtil.getLogMessage("Skipping request because of Pre-Validation."))); } return TankConstants.HTTP_CASE_PASS; } try { if (method.equalsIgnoreCase("GET")) { baseRequest.doGet(baseResponse); } else if (method.equalsIgnoreCase("POST")) { baseRequest.doPost(baseResponse); } else if (method.equalsIgnoreCase("PUT")) { baseRequest.doPut(baseResponse); } else if (method.equalsIgnoreCase("DELETE")) { baseRequest.doDelete(baseResponse); } else if (method.equalsIgnoreCase("OPTIONS")) { baseRequest.doOptions(baseResponse); } } catch (Throwable e) { LOG.error( new MapMessage( LogUtil.getLogMessage("Unexpected Exception executing request: " + e.toString(), LogEventType.IO)), e); } if (APITestHarness.getInstance().getTPMonitor().isEnabled()) { if (!StringUtils.isEmpty(loggingKey)) { APITestHarness.getInstance().getTPMonitor().addToMap(loggingKey, baseRequest); } } validation = processValidations(variables, "", baseResponse); LogUtil.getLogEvent().setValidationStatus(validation); if (APITestHarness.getInstance().getAgentRunData().getActiveProfile() == LoggingProfile.VERBOSE) { LOG.info( new MapMessage(LogUtil.getLogMessage("Made Call ..."))); } processVariables(variables, baseResponse); if (APITestHarness.getInstance().getAgentRunData().getActiveProfile() == LoggingProfile.TRACE) { LOG.info( new MapMessage(LogUtil.getLogMessage("Variables Processed. Exiting ..."))); } if (!APITestHarness.getInstance().isDebug()) { processPerfResponse(validation, uniqueName, Integer.valueOf(variables.getVariable("THREAD_ID")), baseRequest, baseResponse); } tsc.setRequest(baseRequest); tsc.setResponse(baseResponse); return validation; }
private void init(CFMLogFields header) { mapMessage = new MapMessage(header.getKeyMap()); }
protected void addMapData(final String prefix, final Map<String, String> fields, final MapMessage msg) { final Map<String, String> data = msg.getData(); for (final Map.Entry<String, String> entry : data.entrySet()) { fields.put(prefix + entry.getKey(), entry.getValue()); } }
protected void addMapData(final String prefix, final Map<String, String> fields, final MapMessage<?, String> msg) { final Map<String, String> data = msg.getData(); for (final Map.Entry<String, String> entry : data.entrySet()) { fields.put(prefix + entry.getKey(), entry.getValue()); } }
@Override protected void writeInternal(final LogEvent event, final Serializable serializable) { StringReader reader = null; try { if (!this.isRunning() || this.connection == null || this.connection.isClosed() || this.statement == null || this.statement.isClosed()) { throw new AppenderLoggingException( "Cannot write logging event; JDBC manager not connected to the database."); } if (serializable instanceof MapMessage) { setFields((MapMessage<?, ?>) serializable); } int i = 1; // JDBC indices start at 1 for (final ColumnMapping mapping : this.columnMappings) { if (ThreadContextMap.class.isAssignableFrom(mapping.getType()) || ReadOnlyStringMap.class.isAssignableFrom(mapping.getType())) { this.statement.setObject(i++, event.getContextData().toMap()); } else if (ThreadContextStack.class.isAssignableFrom(mapping.getType())) { this.statement.setObject(i++, event.getContextStack().asList()); } else if (Date.class.isAssignableFrom(mapping.getType())) { this.statement.setObject(i++, DateTypeConverter.fromMillis(event.getTimeMillis(), mapping.getType().asSubclass(Date.class))); } else { StringLayout layout = mapping.getLayout(); if (layout != null) { if (Clob.class.isAssignableFrom(mapping.getType())) { this.statement.setClob(i++, new StringReader(layout.toSerializable(event))); } else if (NClob.class.isAssignableFrom(mapping.getType())) { this.statement.setNClob(i++, new StringReader(layout.toSerializable(event))); } else { final Object value = TypeConverters.convert(layout.toSerializable(event), mapping.getType(), null); if (value == null) { this.statement.setNull(i++, Types.NULL); } else { this.statement.setObject(i++, value); } } } } } for (final ColumnConfig column : this.columnConfigs) { if (column.isEventTimestamp()) { this.statement.setTimestamp(i++, new Timestamp(event.getTimeMillis())); } else if (column.isClob()) { reader = new StringReader(column.getLayout().toSerializable(event)); if (column.isUnicode()) { this.statement.setNClob(i++, reader); } else { this.statement.setClob(i++, reader); } } else if (column.isUnicode()) { this.statement.setNString(i++, column.getLayout().toSerializable(event)); } else { this.statement.setString(i++, column.getLayout().toSerializable(event)); } } if (this.isBatchSupported) { this.statement.addBatch(); } else if (this.statement.executeUpdate() == 0) { throw new AppenderLoggingException( "No records inserted in database table for log event in JDBC manager."); } } catch (final SQLException e) { throw new AppenderLoggingException("Failed to insert record for log event in JDBC manager: " + e.getMessage(), e); } finally { Closer.closeSilently(reader); } }