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);
}
}
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);
}
}));
}
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);
}
}));
}
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);
}
}));
}
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);
}
}));
}
Aggregations