Search in sources :

Example 6 with Appender

use of ch.qos.logback.core.Appender in project cdap by caskdata.

the class LogStageInjector method start.

/**
   * Hijacks the appenders for the root logger and replaces them with a {@link LogStageAppender} that will insert
   * the ETL stage name at the start of each message if the stage name is set. Uses {@link org.slf4j.MDC} to look up
   * the current stage name.
   */
public static void start() {
    if (!initialized.compareAndSet(false, true)) {
        return;
    }
    ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
    if (!(loggerFactory instanceof LoggerContext)) {
        LOG.warn("LoggerFactory is not a logback LoggerContext. Stage names will not be injected into log messages.");
        return;
    }
    LoggerContext loggerContext = (LoggerContext) loggerFactory;
    ch.qos.logback.classic.Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
    List<Appender<ILoggingEvent>> appenders = new ArrayList<>();
    Iterator<Appender<ILoggingEvent>> appenderIterator = rootLogger.iteratorForAppenders();
    while (appenderIterator.hasNext()) {
        Appender<ILoggingEvent> appender = appenderIterator.next();
        // appender will have a different classloader than LogStageAppender...
        if (appender.getClass().getName().equals(LogStageAppender.class.getName())) {
            return;
        }
        appenders.add(appender);
    }
    Appender<ILoggingEvent> stageAppender = new LogStageAppender(appenders);
    stageAppender.setContext(loggerContext);
    stageAppender.start();
    rootLogger.addAppender(stageAppender);
    // To avoid duplicate messages, need to detach the original appenders
    for (Appender<ILoggingEvent> appender : appenders) {
        rootLogger.detachAppender(appender);
    }
}
Also used : Appender(ch.qos.logback.core.Appender) ILoggerFactory(org.slf4j.ILoggerFactory) ArrayList(java.util.ArrayList) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 7 with Appender

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

the class ApplicationServiceImplTest method testRemoveApplicationUninstallAllFeaturesException.

/**
     * Tests the {@link ApplicationServiceImpl#removeApplication(Application)} method
     * for the case where an exception is thrown within uninstallAllFeatures(Application)
     *
     * @throws Exception
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testRemoveApplicationUninstallAllFeaturesException() 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);
    Set<Repository> activeRepos = new HashSet<Repository>(Arrays.asList(mainFeatureRepo, noMainFeatureRepo1, noMainFeatureRepo2));
    FeaturesService featuresService = createMockFeaturesService(activeRepos, null, null);
    when(bundleContext.getService(mockFeatureRef)).thenReturn(featuresService);
    ApplicationService appService = createPermittedApplicationServiceImpl();
    Application testApp = mock(ApplicationImpl.class);
    Feature testFeature1 = mock(Feature.class);
    Feature testFeature2 = mock(Feature.class);
    Set<Feature> featureSet = new HashSet<>();
    featureSet.add(testFeature1);
    featureSet.add(testFeature2);
    when(featuresService.isInstalled(any(Feature.class))).thenReturn(true);
    when(testApp.getFeatures()).thenReturn(featureSet);
    doThrow(new Exception()).when(featuresService).uninstallFeature(anyString(), anyString(), any(EnumSet.class));
    appService.removeApplication(testApp);
    verify(mockAppender, times(2)).doAppend(argThat(new ArgumentMatcher() {

        @Override
        public boolean matches(final Object argument) {
            return ((LoggingEvent) argument).getFormattedMessage().contains(UNINSTALL_FAIL);
        }
    }));
}
Also used : Appender(ch.qos.logback.core.Appender) EnumSet(java.util.EnumSet) Logger(org.slf4j.Logger) Feature(org.apache.karaf.features.Feature) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Repository(org.apache.karaf.features.Repository) ArgumentMatcher(org.mockito.ArgumentMatcher) FeaturesService(org.apache.karaf.features.FeaturesService) Application(org.codice.ddf.admin.application.service.Application) HashSet(java.util.HashSet) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Test(org.junit.Test)

Example 8 with Appender

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

the class ApplicationServiceImplTest method testFindFeatureExceptions.

/**
     * Tests the {@link ApplicationServiceImpl#findFeature(Feature)} method
     * for the case where exceptions are thrown inside findFeature(Feature, Set<Application>)
     *
     * @throws Exception
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix and un-ignore
@Test
@Ignore
public void testFindFeatureExceptions() 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);
    Application testApp = mock(ApplicationImpl.class);
    final Set<Application> applicationSet = new HashSet<>();
    applicationSet.add(testApp);
    Set<Repository> activeRepos = new HashSet<Repository>(Arrays.asList(mainFeatureRepo, noMainFeatureRepo1));
    FeaturesService featuresService = createMockFeaturesService(activeRepos, null, null);
    when(bundleContext.getService(mockFeatureRef)).thenReturn(featuresService);
    ApplicationService appService = new ApplicationServiceImpl(bundleStateServices) {

        @Override
        protected BundleContext getContext() {
            return bundleContext;
        }

        @Override
        public Set<Application> getApplications() {
            return applicationSet;
        }

        @Override
        public boolean isPermittedToViewFeature(String featureName) {
            return true;
        }
    };
    Feature testFeature = mock(Feature.class);
    doThrow(new NullPointerException()).when(testApp).getFeatures();
    appService.findFeature(testFeature);
    verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {

        @Override
        public boolean matches(final Object argument) {
            return ((LoggingEvent) argument).getFormattedMessage().contains(FIND_FEAT_EX);
        }
    }));
    verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {

        @Override
        public boolean matches(final Object argument) {
            return ((LoggingEvent) argument).getFormattedMessage().contains(FIND_FEAT_EX2);
        }
    }));
}
Also used : Appender(ch.qos.logback.core.Appender) Mockito.anyString(org.mockito.Mockito.anyString) Logger(org.slf4j.Logger) Feature(org.apache.karaf.features.Feature) Repository(org.apache.karaf.features.Repository) ArgumentMatcher(org.mockito.ArgumentMatcher) FeaturesService(org.apache.karaf.features.FeaturesService) Application(org.codice.ddf.admin.application.service.Application) HashSet(java.util.HashSet) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with Appender

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

the class ApplicationUploadEndpointTest method testApplicationUploadEndpointCreateEmptyFilename.

/**
     * Tests the {@link ApplicationUploadEndpoint#create(MultipartBody, UriInfo)} method
     * for the case where the filename is empty
     *
     * @throws Exception
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testApplicationUploadEndpointCreateEmptyFilename() 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);
    ApplicationUploadEndpoint applicationUploadEndpoint = new ApplicationUploadEndpoint(testAppService);
    testFile = new File(File.class.getResource(TEST_FILE_NAME).getPath());
    testIS = new FileInputStream(testFile);
    when(testDataHandler.getInputStream()).thenReturn(testIS);
    when(testDisp.getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME)).thenReturn(StringUtils.EMPTY);
    applicationUploadEndpoint.setDefaultFileLocation(TEST_FILE_LOCATION);
    applicationUploadEndpoint.create(testMultipartBody, testUriInfo);
    verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {

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

Example 10 with Appender

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

the class ApplicationUploadEndpointTest method testApplicationUploadEndpointCreateInvalidType.

/**
     * Tests the {@link ApplicationUploadEndpoint#create(MultipartBody, UriInfo)} method
     * for the case where the source file has an invalid type
     *
     * @throws Exception
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testApplicationUploadEndpointCreateInvalidType() 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);
    ApplicationUploadEndpoint applicationUploadEndpoint = new ApplicationUploadEndpoint(testAppService);
    testFile = new File(File.class.getResource(BAD_FILE_NAME).getPath());
    testIS = mock(InputStream.class);
    when(testIS.available()).thenReturn(1);
    when(testDataHandler.getInputStream()).thenReturn(testIS);
    when(testDisp.getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME)).thenReturn(BAD_FILE_NAME);
    applicationUploadEndpoint.setDefaultFileLocation(TEST_FILE_LOCATION);
    Response response = applicationUploadEndpoint.create(testMultipartBody, testUriInfo);
    Response expectedResponse = Response.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE_415).build();
    verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {

        @Override
        public boolean matches(final Object argument) {
            return ((LoggingEvent) argument).getFormattedMessage().contains(WRONG_FILE_TYPE);
        }
    }));
}
Also used : Appender(ch.qos.logback.core.Appender) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Logger(org.slf4j.Logger) Response(javax.ws.rs.core.Response) ArgumentMatcher(org.mockito.ArgumentMatcher) File(java.io.File) Test(org.junit.Test)

Aggregations

Appender (ch.qos.logback.core.Appender)46 Test (org.junit.Test)36 Logger (org.slf4j.Logger)26 ArgumentMatcher (org.mockito.ArgumentMatcher)23 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)13 Logger (ch.qos.logback.classic.Logger)12 LoggerContext (ch.qos.logback.classic.LoggerContext)12 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)12 FileAppender (ch.qos.logback.core.FileAppender)11 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 ApplicationServiceException (org.codice.ddf.admin.application.service.ApplicationServiceException)8 File (java.io.File)6 ArrayList (java.util.ArrayList)6 Application (org.codice.ddf.admin.application.service.Application)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)2