Search in sources :

Example 41 with Appender

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

the class ApplicationServiceImplTest method testGetAllFeaturesException.

/**
     * Tests the {@link ApplicationServiceImpl#getAllFeatures()} method
     * for the case where an exception is thrown by the featuresService
     *
     * @throws Exception
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testGetAllFeaturesException() 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();
    doThrow(new NullPointerException()).when(featuresService).listFeatures();
    appService.getAllFeatures();
    verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {

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

Example 42 with Appender

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

the class ApplicationServiceImplTest method testFindApplicationFeaturesGetRepoFeatException.

/**
     * Tests the {@link ApplicationServiceImpl#findApplicationFeatures(String)} method
     * for the case where an exception is thrown in getRepositoryFeatures
     *
     * @throws Exception
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testFindApplicationFeaturesGetRepoFeatException() 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 = new ApplicationServiceImpl(bundleStateServices) {

        @Override
        protected BundleContext getContext() {
            return bundleContext;
        }
    };
    Repository testRepo = mock(Repository.class);
    Repository[] repoList = { testRepo };
    when(featuresService.listRepositories()).thenReturn(repoList);
    when(testRepo.getName()).thenReturn(TEST_APP_NAME);
    doThrow(new Exception()).when(testRepo).getFeatures();
    appService.findApplicationFeatures(TEST_APP_NAME);
    verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {

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

Example 43 with Appender

use of ch.qos.logback.core.Appender in project ha-bridge by bwssytems.

the class LoggingUtil method getLogFileInfos.

/**
	 * Get the logfile information for the roor logger.
	 * 
	 * @return List of LogFileInfo obejcts
	 */
public static List<LogFileInfo> getLogFileInfos() {
    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final List<LogFileInfo> logFileInfos = new ArrayList<LogFileInfo>();
    final Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
    final Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders();
    while (it.hasNext()) {
        final Appender<ILoggingEvent> appender = it.next();
        if (appender instanceof FileAppender) {
            final FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) appender;
            final File logFile = new File(fileAppender.getFile());
            final LogFileInfo logFileInfo = new LogFileInfo();
            logFileInfo.setFileName(logFile.getName());
            logFileInfo.setFileLastChanged(new Date(logFile.lastModified()));
            logFileInfo.setFileSize(logFile.length());
            logFileInfos.add(logFileInfo);
        }
    }
    return logFileInfos;
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) Appender(ch.qos.logback.core.Appender) FileAppender(ch.qos.logback.core.FileAppender) ArrayList(java.util.ArrayList) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) LogFileInfo(com.bwssystems.logservices.LogFileInfo) Date(java.util.Date) File(java.io.File)

Example 44 with Appender

use of ch.qos.logback.core.Appender in project ha-bridge by bwssytems.

the class LoggingUtil method getLogFile.

/**
	 * Get the log file.
	 * 
	 * @param logFileName The name of the log file 
	 * @return The actual file
	 */
public static File getLogFile(final String logFileName) {
    if (logFileName == null) {
        throw new IllegalArgumentException("logFileName cannot be null.");
    }
    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
    final Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders();
    while (it.hasNext()) {
        final Appender<ILoggingEvent> appender = it.next();
        if (appender instanceof FileAppender) {
            final FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) appender;
            final File logFile = new File(fileAppender.getFile());
            if (logFile.getName().equalsIgnoreCase(logFileName)) {
                return logFile;
            }
        }
    }
    return null;
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) Appender(ch.qos.logback.core.Appender) FileAppender(ch.qos.logback.core.FileAppender) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) File(java.io.File)

Example 45 with Appender

use of ch.qos.logback.core.Appender in project sling by apache.

the class LogTailerWebConsolePlugin method getOptions.

private String getOptions() {
    Set<String> logFiles = new HashSet<String>();
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    for (ch.qos.logback.classic.Logger logger : context.getLoggerList()) {
        for (Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders(); index.hasNext(); ) {
            Appender<ILoggingEvent> appender = index.next();
            if (appender instanceof FileAppender) {
                FileAppender fileAppender = (FileAppender) appender;
                String logfilePath = fileAppender.getFile();
                logFiles.add(logfilePath);
            }
        }
    }
    String logFilesHtml = "<option value=\"\"> - Select file - </option>";
    for (String logFile : logFiles) {
        File file = new File(logFile);
        logFilesHtml += "<option value=\"" + getKey(file) + "\">" + file.getName() + "</option>";
    }
    return logFilesHtml;
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) Appender(ch.qos.logback.core.Appender) FileAppender(ch.qos.logback.core.FileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

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