Search in sources :

Example 26 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender in project sonarqube by SonarSource.

the class WebServerProcessLoggingTest method log_for_cluster_changes_layout_in_file_and_console.

@Test
public void log_for_cluster_changes_layout_in_file_and_console() {
    props.set("sonar.cluster.enabled", "true");
    props.set("sonar.cluster.node.name", "my-node");
    LoggerContext ctx = underTest.configure(props);
    Logger root = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
    FileAppender fileAppender = (FileAppender) root.getAppender("file_web");
    PatternLayoutEncoder encoder = (PatternLayoutEncoder) fileAppender.getEncoder();
    assertThat(encoder.getPattern()).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level my-node web[%X{HTTP_REQUEST_ID}][%logger{20}] %msg%n");
    Logger startup = ctx.getLogger("startup");
    ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) startup.getAppender("CONSOLE");
    PatternLayoutEncoder patternEncoder = (PatternLayoutEncoder) consoleAppender.getEncoder();
    assertThat(patternEncoder.getPattern()).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level my-node app[][%logger{20}] %msg%n");
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) PatternLayoutEncoder(org.sonar.process.logging.PatternLayoutEncoder) ConsoleAppender(ch.qos.logback.core.ConsoleAppender) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.Test)

Example 27 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender 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(org.sonar.process.logging.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 28 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender in project sonarqube by SonarSource.

the class AppLoggingTest method root_logger_only_writes_to_console_with_formatting_when_running_from_sonar_script.

@Test
public void root_logger_only_writes_to_console_with_formatting_when_running_from_sonar_script() {
    emulateRunFromSonarScript();
    LoggerContext ctx = underTest.configure();
    Logger rootLogger = ctx.getLogger(ROOT_LOGGER_NAME);
    ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) rootLogger.getAppender("APP_CONSOLE");
    verifyAppFormattedLogEncoder(consoleAppender.getEncoder());
    assertThat(rootLogger.iteratorForAppenders()).toIterable().hasSize(1);
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.Test)

Example 29 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender in project engineblock by engineblock.

the class ScenarioLogger method start.

public ScenarioLogger start() {
    if (!loggerDir.exists()) {
        if (!loggerDir.mkdirs()) {
            throw new RuntimeException("Unable to create logger directory:" + loggerDir.getPath());
        }
    }
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    // List<LoggerContextListener> copyOfListenerList = loggerContext.getCopyOfListenerList();
    ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<>();
    ca.setContext(loggerContext);
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    // ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
    ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
    ple.setContext(loggerContext);
    ple.start();
    String scenarioLog = loggerDir.getPath() + File.separator + scenario.getName() + ".log";
    scenarioLog = scenarioLog.replaceAll("\\s", "_");
    FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
    fileAppender.setFile(scenarioLog);
    fileAppender.setEncoder(ple);
    fileAppender.setContext(loggerContext);
    System.err.println("Logging to " + scenarioLog);
    fileAppender.start();
    Logger logger = (Logger) LoggerFactory.getLogger("ROOT");
    logger.addAppender(fileAppender);
    logLevelOverrides.forEach((loggerName, loggingLevel) -> {
        logger.debug("Overriding log level for " + loggerName + " to " + loggingLevel);
        Logger toOverride = (Logger) LoggerFactory.getLogger(loggerName);
        toOverride.setLevel(loggingLevel);
        toOverride.debug("Log level was set to " + loggingLevel + " by engineblock CLI option.");
    });
    logger.setLevel(logLevel);
    logger.setAdditive(true);
    /* set to true if root should log too */
    purgeOldFiles(logger);
    return this;
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) FileAppender(ch.qos.logback.core.FileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 30 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender in project engineblock by engineblock.

the class ConsoleLogging method enableConsoleLogging.

public static void enableConsoleLogging(Level level, String loggingPattern) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    // List<LoggerContextListener> copyOfListenerList = loggerContext.getCopyOfListenerList();
    ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<>();
    ca.setContext(loggerContext);
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setPattern(loggingPattern);
    ple.setContext(loggerContext);
    ple.start();
    ca.setEncoder(ple);
    LevelFilter<ILoggingEvent> levelFilter = new LevelFilter<>(level);
    levelFilter.start();
    ca.addFilter(levelFilter);
    ca.start();
    Logger root = loggerContext.getLogger("ROOT");
    root.addAppender(ca);
    root.setLevel(Level.TRACE);
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext)

Aggregations

ConsoleAppender (ch.qos.logback.core.ConsoleAppender)38 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)31 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)21 LoggerContext (ch.qos.logback.classic.LoggerContext)19 Logger (ch.qos.logback.classic.Logger)16 Appender (ch.qos.logback.core.Appender)10 FileAppender (ch.qos.logback.core.FileAppender)9 Test (org.junit.Test)8 ThresholdFilter (ch.qos.logback.classic.filter.ThresholdFilter)5 OutputStreamAppender (ch.qos.logback.core.OutputStreamAppender)5 LayoutWrappingEncoder (ch.qos.logback.core.encoder.LayoutWrappingEncoder)5 SyslogAppender (ch.qos.logback.classic.net.SyslogAppender)4 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 AsyncAppender (ch.qos.logback.classic.AsyncAppender)2 JoranException (ch.qos.logback.core.joran.spi.JoranException)2 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)2 MetricRegistry (com.codahale.metrics.MetricRegistry)2 PmsConfiguration (net.pms.configuration.PmsConfiguration)2