Search in sources :

Example 1 with Filter

use of com.cosylab.logging.engine.Filter in project ACS by ACS-Community.

the class FilterTest method testMinMaxFilterType.

/**
	 * Check if the filter works for MinMax value 
	 */
public void testMinMaxFilterType() throws Exception {
    // Check the line
    Integer maxLine = Integer.valueOf(120);
    Integer minLine = Integer.valueOf(98);
    Filter minMaxFilter = new MinMaxFilter(LogField.LINE, lethal, minLine, maxLine, false);
    assertTrue("MinMax should have accepted this line value", minMaxFilter.applyTo(log1, lethal));
    assertFalse("MinMax should have rejected this line value", minMaxFilter.applyTo(log2, lethal));
    assertFalse("MinMax should have rejected this line value", minMaxFilter.applyTo(log3, lethal));
    // Check the time
    Date maxDate = df.parseIsoTimestamp("2013-08-07T15:10:10.512");
    Date minDate = df.parseIsoTimestamp("2013-08-02T15:10:10.512");
    Filter minMaxDateFilter = new MinMaxFilter(LogField.TIMESTAMP, lethal, minDate, maxDate, false);
    assertTrue("MinMax should have accepted this timestamp", minMaxDateFilter.applyTo(log1, lethal));
    assertFalse("MinMax should have rejected this timestamp", minMaxDateFilter.applyTo(log2, lethal));
    assertFalse("MinMax should have rejected this timestamp", minMaxDateFilter.applyTo(log3, lethal));
    // Log type
    LogTypeHelper maxType = LogTypeHelper.NOTICE;
    LogTypeHelper minType = LogTypeHelper.DEBUG;
    Filter minMaxTypeFilter = new MinMaxFilter(LogField.ENTRYTYPE, lethal, minType, maxType, false);
    assertTrue("MinMax should have accepted this log type", minMaxTypeFilter.applyTo(log1, lethal));
    assertFalse("MinMax should have rejected this log type", minMaxTypeFilter.applyTo(log2, lethal));
    assertFalse("MinMax should have rejected this log type", minMaxTypeFilter.applyTo(log3, lethal));
}
Also used : MinMaxFilter(com.cosylab.logging.engine.MinMaxFilter) RegExpFilter(com.cosylab.logging.engine.RegExpFilter) ExactFilter(com.cosylab.logging.engine.ExactFilter) MinMaxFilter(com.cosylab.logging.engine.MinMaxFilter) Filter(com.cosylab.logging.engine.Filter) LogTypeHelper(com.cosylab.logging.engine.log.LogTypeHelper) Date(java.util.Date)

Example 2 with Filter

use of com.cosylab.logging.engine.Filter in project ACS by ACS-Community.

the class FiltersVectorTest method testHasActiveFilters.

/**
	 * Test {@link FiltersVector#hasActiveFilters()}
	 */
public void testHasActiveFilters() throws Exception {
    //No active filters yet
    assertFalse("The vector should be empty at startup", filters.hasActiveFilters());
    Filter exactLogMessageFilter = new ExactFilter(LogField.LOGMESSAGE, lethal, "A message", false);
    filters.addFilter(exactLogMessageFilter, false);
    assertFalse("The vector should contain one filter but not active", filters.hasActiveFilters());
    Filter stackLvlFilter = new ExactFilter(LogField.STACKLEVEL, lethal, Integer.valueOf(5), false);
    filters.addFilter(stackLvlFilter, true);
    assertTrue("The vector should contain one active filter", filters.hasActiveFilters());
    filters.clear();
    assertFalse("The empty vector has no active filters", filters.hasActiveFilters());
}
Also used : ExactFilter(com.cosylab.logging.engine.ExactFilter) RegExpFilter(com.cosylab.logging.engine.RegExpFilter) ExactFilter(com.cosylab.logging.engine.ExactFilter) MinMaxFilter(com.cosylab.logging.engine.MinMaxFilter) Filter(com.cosylab.logging.engine.Filter)

Example 3 with Filter

use of com.cosylab.logging.engine.Filter in project ACS by ACS-Community.

the class EngineFilteringTest method testFiltering.

/**
	 * Set a filter and checks if the logs received are those passing a filters.
	 * <P>
	 * In this case we do not need to check the correctness of the logs received 
	 * in the listeners but only if they are received or not because it means that
	 * the engine is using the filters.
	 * <P>
	 * For this example, the test defines 1 filter based on the type of the logs.
	 * <P>
	 * The correctness of the filtering is also tested in another test because the engine 
	 * uses the same filters used by the table.
	 * 
	 * @throws Exception
	 */
public void testFiltering() throws Exception {
    // Randomly generate the logs
    Collection<ILogEntry> logs = CacheUtils.generateLogs(NUMBER_OF_LOGS);
    assertNotNull(logs);
    assertEquals(NUMBER_OF_LOGS, logs.size());
    Collection<ILogEntry> flushLogs = CacheUtils.generateLogsType(100, LogTypeHelper.NOTICE);
    // Create a filter for the type INFO
    Filter f = new ExactFilter(LogField.ENTRYTYPE, false, LogTypeHelper.INFO, false);
    assertNotNull(f);
    // And a filter for the source name
    Filter nameFilter = new ExactFilter(LogField.ROUTINE, false, getName(), false);
    assertNotNull(nameFilter);
    // No filters exists in the engine
    assertNull(engine.getFilters());
    // Add the filters
    engine.addFilter(f);
    engine.addFilter(nameFilter);
    assertNotNull(engine.getFilters());
    assertEquals("Size differ", 2, engine.getFilters().size());
    // connect the engine
    engine.connect();
    // wait until the engine is connected
    int iterCount = 0;
    while (iterCount < TIMEOUT && !engine.isConnected()) {
        iterCount++;
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
        }
    }
    assertTrue(engine.isConnected());
    // publish all the logs and count the number of INFO logs
    int infos = 0;
    for (ILogEntry log : logs) {
        AcsLogLevel level = AcsLogLevel.fromAcsCoreLevel(log.getType().acsCoreLevel);
        m_logger.log(level, (String) log.getField(LogField.LOGMESSAGE));
        if (log.getType() == LogTypeHelper.INFO) {
            infos++;
        }
    }
    // If it is possible then it is better this second option then sending logs..
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }
    System.out.println("Flushing");
    for (ILogEntry log : flushLogs) {
        AcsLogLevel level = AcsLogLevel.fromAcsCoreLevel(log.getType().acsCoreLevel);
        m_logger.log(level, (String) log.getField(LogField.LOGMESSAGE));
    }
    // wait till all the logs are received
    //
    // It additionally waits for TIMEOUT seconds after the last log has been 
    // received to be sure there are no further logs delayed because of caching
    // or similar problems
    // The number of seconds after the last log has been received
    int elapsed = 0;
    // The number of logs at the previous iteration
    int count = 0;
    while (elapsed < TIMEOUT) {
        synchronized (receivedLogs) {
            if (receivedLogs.size() != count) {
                count = receivedLogs.size();
                elapsed = 0;
                continue;
            }
        }
        // No new logs received
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
        }
        elapsed++;
    }
    // If all the logs have been received then xmlLogsCount must be greater then NUMBER_OF_LOGS
    // Not equal because the xml logs are not filtered and there can be logs generated
    // outside of this object
    assertTrue("Received only " + xmlLogsCount.get() + " xml logs, when there should have been at least " + NUMBER_OF_LOGS, xmlLogsCount.get() >= NUMBER_OF_LOGS);
    // Check if the number of received logs is as expected
    assertEquals(infos, receivedLogs.size());
}
Also used : ExactFilter(com.cosylab.logging.engine.ExactFilter) ILogEntry(com.cosylab.logging.engine.log.ILogEntry) ExactFilter(com.cosylab.logging.engine.ExactFilter) Filter(com.cosylab.logging.engine.Filter) AcsLogLevel(alma.acs.logging.AcsLogLevel)

Example 4 with Filter

use of com.cosylab.logging.engine.Filter in project ACS by ACS-Community.

the class EngineFilteringTest method testAddFilter.

/**
	 * Test the adding and clearing filters
	 * 
	 * @throws Exception
	 */
public void testAddFilter() throws Exception {
    // No filters defined: getFilters() return null
    assertNull(engine.getFilters());
    // Create a filter
    Filter f = new ExactFilter(LogField.ENTRYTYPE, false, LogTypeHelper.INFO, false);
    assertNotNull(f);
    // Add the filter
    engine.addFilter(f);
    assertNotNull(engine.getFilters());
    assertEquals("Sizes differ", 1, engine.getFilters().size());
    // The filters has been added as active
    assertTrue(engine.getFilters().hasActiveFilters());
    // Create and add another filter
    Filter f2 = new ExactFilter(LogField.ENTRYTYPE, false, LogTypeHelper.DEBUG, false);
    assertNotNull(f2);
    assertEquals("Sizes differ", 1, engine.getFilters().size());
    // Clear the filters
    engine.clearFilters();
    assertNull(engine.getFilters());
}
Also used : ExactFilter(com.cosylab.logging.engine.ExactFilter) ExactFilter(com.cosylab.logging.engine.ExactFilter) Filter(com.cosylab.logging.engine.Filter)

Example 5 with Filter

use of com.cosylab.logging.engine.Filter in project ACS by ACS-Community.

the class FilterTest method testMaxFilterType.

/**
	 * Check if the filter works for max value 
	 */
public void testMaxFilterType() throws Exception {
    // Check the line
    Integer maxLine = Integer.valueOf(120);
    Filter maxFilter = new MinMaxFilter(LogField.LINE, lethal, null, maxLine, false);
    assertTrue("Max should have accepted this line value", maxFilter.applyTo(log1, lethal));
    Integer noMaxLine = Integer.valueOf(98);
    Filter noMaxFilter = new MinMaxFilter(LogField.LINE, lethal, null, noMaxLine, false);
    assertFalse("Min should have rejected this line value", noMaxFilter.applyTo(log1, lethal));
    // Check the time
    Date date = df.parseIsoTimestamp("2013-09-01T15:10:10.512");
    Filter maxDateFilter = new MinMaxFilter(LogField.TIMESTAMP, lethal, null, date, false);
    assertTrue("Max should have accepted this timestamp", maxDateFilter.applyTo(log1, lethal));
    Date noDate = df.parseIsoTimestamp("2013-07-01T15:10:10.512");
    Filter noMaxDateFilter = new MinMaxFilter(LogField.TIMESTAMP, lethal, null, noDate, false);
    assertFalse("Max should have accepted this timestamp", noMaxDateFilter.applyTo(log1, lethal));
    // Log type
    LogTypeHelper maxType = LogTypeHelper.CRITICAL;
    Filter maxTypeFilter = new MinMaxFilter(LogField.ENTRYTYPE, lethal, null, maxType, false);
    assertTrue("Min should have accepted this log type", maxTypeFilter.applyTo(log1, lethal));
    LogTypeHelper noMaxType = LogTypeHelper.TRACE;
    Filter noMaxTypeFilter = new MinMaxFilter(LogField.ENTRYTYPE, lethal, null, noMaxType, false);
    assertFalse("Min should have accepted this log type", noMaxTypeFilter.applyTo(log1, lethal));
}
Also used : MinMaxFilter(com.cosylab.logging.engine.MinMaxFilter) RegExpFilter(com.cosylab.logging.engine.RegExpFilter) ExactFilter(com.cosylab.logging.engine.ExactFilter) MinMaxFilter(com.cosylab.logging.engine.MinMaxFilter) Filter(com.cosylab.logging.engine.Filter) LogTypeHelper(com.cosylab.logging.engine.log.LogTypeHelper) Date(java.util.Date)

Aggregations

Filter (com.cosylab.logging.engine.Filter)14 ExactFilter (com.cosylab.logging.engine.ExactFilter)12 MinMaxFilter (com.cosylab.logging.engine.MinMaxFilter)10 RegExpFilter (com.cosylab.logging.engine.RegExpFilter)9 Date (java.util.Date)5 LogTypeHelper (com.cosylab.logging.engine.log.LogTypeHelper)3 FiltersVector (com.cosylab.logging.engine.FiltersVector)2 InvalidFilterConstraintException (com.cosylab.logging.engine.InvalidFilterConstraintException)2 AcsLogLevel (alma.acs.logging.AcsLogLevel)1 ILogEntry (com.cosylab.logging.engine.log.ILogEntry)1 File (java.io.File)1