Search in sources :

Example 81 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project platformlayer by platformlayer.

the class LogbackHook method append.

@Override
protected void append(E e) {
    OpsContext opsContext = OpsContext.get();
    if (opsContext != null) {
        ILoggingEvent event = (ILoggingEvent) e;
        // Note that we can get the unformatted message in getMessage(), presumably along with the parameters...
        String message = event.getFormattedMessage();
        Level level = event.getLevel();
        int levelInt = level.toInt();
        List<String[]> exceptionStacks = null;
        IThrowableProxy throwableInformation = event.getThrowableProxy();
        while (throwableInformation != null) {
            String[] exceptionStackTrace = null;
            StackTraceElementProxy[] trace = throwableInformation.getStackTraceElementProxyArray();
            String exceptionMessage = throwableInformation.getMessage();
            String exceptionClass = throwableInformation.getClassName();
            if (trace != null) {
                exceptionStackTrace = new String[1 + trace.length];
                exceptionStackTrace[0] = exceptionClass + ": " + exceptionMessage;
                for (int i = 0; i < trace.length; i++) {
                    exceptionStackTrace[1 + i] = trace[i].getSTEAsString();
                }
            } else {
                exceptionStackTrace = new String[1];
                exceptionStackTrace[0] = exceptionClass + ": " + exceptionMessage;
            }
            if (exceptionStacks == null) {
                exceptionStacks = Lists.newArrayList();
            }
            exceptionStacks.add(exceptionStackTrace);
            throwableInformation = throwableInformation.getCause();
        }
        if (message != null || exceptionStacks != null) {
            opsContext.getJobLogger().logMessage(message, exceptionStacks, levelInt);
            if (levelInt >= Level.ERROR_INT) {
                // String key = "warn-" + OpsSystem.buildSimpleTimeString() + "-" + (System.nanoTime() % 1000);
                if (opsContext != null) {
                    // && opsContext.getOperation() != null) {
                    if (exceptionStacks != null && !exceptionStacks.isEmpty()) {
                        String[] exceptionStack = exceptionStacks.get(0);
                        if (exceptionStack != null && exceptionStack.length > 0) {
                            message += "; " + exceptionStack[0];
                        }
                    }
                    opsContext.addWarning(null, message);
                }
            }
        }
    }
}
Also used : IThrowableProxy(ch.qos.logback.classic.spi.IThrowableProxy) Level(ch.qos.logback.classic.Level) OpsContext(org.platformlayer.ops.OpsContext) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) StackTraceElementProxy(ch.qos.logback.classic.spi.StackTraceElementProxy)

Example 82 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project spring-boot by spring-projects.

the class LogbackConfigurationTests method filePatternCanBeOverridden.

@Test
public void filePatternCanBeOverridden() throws JoranException {
    JoranConfigurator configurator = new JoranConfigurator();
    LoggerContext context = new LoggerContext();
    configurator.setContext(context);
    configurator.doConfigure(new File("src/test/resources/custom-file-log-pattern.xml"));
    Appender<ILoggingEvent> appender = context.getLogger("ROOT").getAppender("FILE");
    assertThat(appender).isInstanceOf(FileAppender.class);
    Encoder<?> encoder = ((FileAppender<?>) appender).getEncoder();
    assertThat(encoder).isInstanceOf(PatternLayoutEncoder.class);
    assertThat(((PatternLayoutEncoder) encoder).getPattern()).isEqualTo("bar");
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) File(java.io.File) Test(org.junit.Test)

Example 83 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project sonarqube by SonarSource.

the class CeProcessLoggingTest method startup_logger_prints_to_only_to_system_out.

@Test
public void startup_logger_prints_to_only_to_system_out() {
    LoggerContext ctx = underTest.configure(props);
    Logger startup = ctx.getLogger("startup");
    assertThat(startup.isAdditive()).isFalse();
    Appender appender = startup.getAppender("CONSOLE");
    assertThat(appender).isInstanceOf(ConsoleAppender.class);
    ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) appender;
    assertThat(consoleAppender.getTarget()).isEqualTo("System.out");
    assertThat(consoleAppender.getEncoder()).isInstanceOf(PatternLayoutEncoder.class);
    PatternLayoutEncoder patternEncoder = (PatternLayoutEncoder) consoleAppender.getEncoder();
    assertThat(patternEncoder.getPattern()).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level app[][%logger{20}] %msg%n");
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) FileAppender(ch.qos.logback.core.FileAppender) Appender(ch.qos.logback.core.Appender) ConsoleAppender(ch.qos.logback.core.ConsoleAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.Test)

Example 84 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project sonarqube by SonarSource.

the class CeProcessLoggingTest method log_to_ce_file.

@Test
public void log_to_ce_file() {
    LoggerContext ctx = underTest.configure(props);
    Logger root = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
    Appender<ILoggingEvent> appender = root.getAppender("file_ce");
    assertThat(appender).isInstanceOf(FileAppender.class);
    FileAppender fileAppender = (FileAppender) appender;
    assertThat(fileAppender.getFile()).isEqualTo(new File(logDir, "ce.log").getAbsolutePath());
    assertThat(fileAppender.getEncoder()).isInstanceOf(PatternLayoutEncoder.class);
    PatternLayoutEncoder encoder = (PatternLayoutEncoder) fileAppender.getEncoder();
    assertThat(encoder.getPattern()).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level ce[%X{ceTaskUuid}][%logger{20}] %msg%n");
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) File(java.io.File) Test(org.junit.Test)

Example 85 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project sonarqube by SonarSource.

the class AppLogging method configureConsole.

/**
   * Creates a non additive logger dedicated to printing message as is (ie. assuming they are already formatted).
   *
   * It creates a dedicated appender to the System.out which applies no formatting the logs it receives.
   */
private void configureConsole(LoggerContext loggerContext) {
    ConsoleAppender<ILoggingEvent> consoleAppender = helper.newConsoleAppender(loggerContext, CONSOLE_PLAIN_APPENDER, "%msg%n");
    Logger consoleLogger = loggerContext.getLogger(CONSOLE_LOGGER);
    consoleLogger.setAdditive(false);
    consoleLogger.addAppender(consoleAppender);
}
Also used : ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Logger(ch.qos.logback.classic.Logger)

Aggregations

ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)121 Test (org.junit.Test)53 Logger (ch.qos.logback.classic.Logger)44 LoggerContext (ch.qos.logback.classic.LoggerContext)41 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)23 FileAppender (ch.qos.logback.core.FileAppender)21 AsyncLoggingEventAppenderFactory (io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory)18 File (java.io.File)18 DropwizardLayoutFactory (io.dropwizard.logging.layout.DropwizardLayoutFactory)17 Appender (ch.qos.logback.core.Appender)15 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)14 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)12 AsyncAppender (ch.qos.logback.classic.AsyncAppender)11 NullLevelFilterFactory (io.dropwizard.logging.filter.NullLevelFilterFactory)10 ArrayList (java.util.ArrayList)10 Logger (org.slf4j.Logger)10 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)7 LogMessage (co.cask.cdap.logging.appender.LogMessage)7 IOException (java.io.IOException)6 Query (org.apache.lucene.search.Query)6