Search in sources :

Example 16 with Appender

use of ch.qos.logback.core.Appender in project ddf by codice.

the class AllApplicationsCompleterTest method testAllApplicationsCompleterNullAppService.

/**
     * Tests the {@link AllApplicationsCompleter#complete(String, int, List)} method
     * for the case where the ApplicationService given to it is null
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testAllApplicationsCompleterNullAppService() {
    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    final Appender mockAppender = mock(Appender.class);
    when(mockAppender.getName()).thenReturn("MOCK");
    root.addAppender(mockAppender);
    root.setLevel(Level.ALL);
    ApplicationService testAppService = null;
    AllApplicationsCompleter allApplicationsCompleter = new AllApplicationsCompleter();
    allApplicationsCompleter.setApplicationService(testAppService);
    allApplicationsCompleter.complete(null, commandLine, new ArrayList<>());
    verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {

        @Override
        public boolean matches(final Object argument) {
            return ((LoggingEvent) argument).getFormattedMessage().contains(NO_APP_SERV);
        }
    }));
}
Also used : Appender(ch.qos.logback.core.Appender) ArgumentMatcher(org.mockito.ArgumentMatcher) Logger(org.slf4j.Logger) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Test(org.junit.Test)

Example 17 with Appender

use of ch.qos.logback.core.Appender in project ddf by codice.

the class ApplicationServiceBeanTest method testAddApplicationsASE.

/**
     * Tests the {@link ApplicationServiceBean#addApplications(List)} method
     * for the case where an ApplicationServiceException is thrown
     *
     * @throws Exception
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testAddApplicationsASE() throws Exception {
    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    final Appender mockAppender = mock(Appender.class);
    when(mockAppender.getName()).thenReturn("MOCK");
    root.addAppender(mockAppender);
    root.setLevel(Level.ALL);
    ApplicationServiceBean serviceBean = new ApplicationServiceBean(testAppService, testConfigAdminExt, mBeanServer);
    List<Map<String, Object>> testURLList = new ArrayList<>();
    Map<String, Object> testURLMap1 = mock(HashMap.class);
    when(testURLMap1.get("value")).thenReturn(TEST_URL);
    Map<String, Object> testURLMap2 = mock(HashMap.class);
    when(testURLMap2.get("value")).thenReturn(TEST_URL);
    testURLList.add(testURLMap1);
    testURLList.add(testURLMap2);
    doThrow(new ApplicationServiceException()).when(testAppService).addApplication(any(URI.class));
    serviceBean.addApplications(testURLList);
    verify(mockAppender, times(2)).doAppend(argThat(new ArgumentMatcher() {

        @Override
        public boolean matches(final Object argument) {
            return ((LoggingEvent) argument).getFormattedMessage().contains(ADD_APP_ASE);
        }
    }));
}
Also used : Appender(ch.qos.logback.core.Appender) ArrayList(java.util.ArrayList) Logger(org.slf4j.Logger) URI(java.net.URI) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) ArgumentMatcher(org.mockito.ArgumentMatcher) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 18 with Appender

use of ch.qos.logback.core.Appender in project ddf by codice.

the class ApplicationServiceBeanTest method testGetServicesASE.

/**
     * Tests the {@link ApplicationServiceBean#getServices(String)} method
     * for the case where an ApplicationServiceException is thrown
     *
     * @throws Exception
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testGetServicesASE() throws Exception {
    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    final Appender mockAppender = mock(Appender.class);
    when(mockAppender.getName()).thenReturn("MOCK");
    root.addAppender(mockAppender);
    root.setLevel(Level.ALL);
    ApplicationServiceBean serviceBean = new ApplicationServiceBean(testAppService, testConfigAdminExt, mBeanServer) {

        @Override
        protected BundleContext getContext() {
            return bundleContext;
        }
    };
    Bundle testBundle = mock(Bundle.class);
    Bundle[] bundles = { testBundle };
    when(bundleContext.getBundles()).thenReturn(bundles);
    List<Map<String, Object>> services = new ArrayList<>();
    Map<String, Object> testService1 = new HashMap<>();
    services.add(testService1);
    doThrow(new ApplicationServiceException()).when(testApp).getBundles();
    when(testAppService.getApplication(TEST_APP_NAME)).thenReturn(testApp);
    when(testConfigAdminExt.listServices(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(services);
    serviceBean.getServices(TEST_APP_NAME);
    verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {

        @Override
        public boolean matches(final Object argument) {
            return ((LoggingEvent) argument).getFormattedMessage().contains(GET_SERV_ASE);
        }
    }));
}
Also used : Appender(ch.qos.logback.core.Appender) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) Logger(org.slf4j.Logger) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) ArgumentMatcher(org.mockito.ArgumentMatcher) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 19 with Appender

use of ch.qos.logback.core.Appender in project ddf by codice.

the class ApplicationConfigInstallerTest method testRunConfigFileNotExist.

/**
     * Tests the {@link ApplicationConfigInstaller#run()} method for the case
     * where the configFile doesn't exist
     *
     * @throws Exception
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testRunConfigFileNotExist() throws Exception {
    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    final Appender mockAppender = mock(Appender.class);
    when(mockAppender.getName()).thenReturn("MOCK");
    root.addAppender(mockAppender);
    root.setLevel(Level.ALL);
    ApplicationConfigInstaller configInstaller = getApplicationConfigInstaller(BAD_FILE, null, null, START_FEATURE, STOP_FEATURE);
    configInstaller.run();
    verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {

        @Override
        public boolean matches(final Object argument) {
            return ((LoggingEvent) argument).getFormattedMessage().contains(RUN_NO_CONFIG);
        }
    }));
}
Also used : Appender(ch.qos.logback.core.Appender) ArgumentMatcher(org.mockito.ArgumentMatcher) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 20 with Appender

use of ch.qos.logback.core.Appender in project uplace.es by Uplace.

the class LoggingConfiguration method setMetricsMarkerLogbackFilter.

// Configure a log filter to remove "metrics" logs from all appenders except the "LOGSTASH" appender
private void setMetricsMarkerLogbackFilter(LoggerContext context) {
    log.info("Filtering metrics logs from all appenders except the {} appender", LOGSTASH_APPENDER_NAME);
    OnMarkerEvaluator onMarkerMetricsEvaluator = new OnMarkerEvaluator();
    onMarkerMetricsEvaluator.setContext(context);
    onMarkerMetricsEvaluator.addMarker("metrics");
    onMarkerMetricsEvaluator.start();
    EvaluatorFilter<ILoggingEvent> metricsFilter = new EvaluatorFilter<>();
    metricsFilter.setContext(context);
    metricsFilter.setEvaluator(onMarkerMetricsEvaluator);
    metricsFilter.setOnMatch(FilterReply.DENY);
    metricsFilter.start();
    for (ch.qos.logback.classic.Logger logger : context.getLoggerList()) {
        for (Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders(); it.hasNext(); ) {
            Appender<ILoggingEvent> appender = it.next();
            if (!appender.getName().equals(ASYNC_LOGSTASH_APPENDER_NAME)) {
                log.debug("Filter metrics logs from the {} appender", appender.getName());
                appender.setContext(context);
                appender.addFilter(metricsFilter);
                appender.start();
            }
        }
    }
}
Also used : LogstashTcpSocketAppender(net.logstash.logback.appender.LogstashTcpSocketAppender) Appender(ch.qos.logback.core.Appender) AsyncAppender(ch.qos.logback.classic.AsyncAppender) OnMarkerEvaluator(ch.qos.logback.classic.boolex.OnMarkerEvaluator) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) EvaluatorFilter(ch.qos.logback.core.filter.EvaluatorFilter)

Aggregations

Appender (ch.qos.logback.core.Appender)50 Test (org.junit.Test)36 Logger (org.slf4j.Logger)26 ArgumentMatcher (org.mockito.ArgumentMatcher)23 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)15 Logger (ch.qos.logback.classic.Logger)14 FileAppender (ch.qos.logback.core.FileAppender)14 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)13 LoggerContext (ch.qos.logback.classic.LoggerContext)12 FeaturesService (org.apache.karaf.features.FeaturesService)10 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)9 HashSet (java.util.HashSet)9 Repository (org.apache.karaf.features.Repository)9 File (java.io.File)8 ApplicationServiceException (org.codice.ddf.admin.application.service.ApplicationServiceException)8 ArrayList (java.util.ArrayList)7 Application (org.codice.ddf.admin.application.service.Application)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 AsyncAppender (ch.qos.logback.classic.AsyncAppender)2