Search in sources :

Example 21 with ConsoleAppender

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

the class LogConfigManager method getDefaultAppender.

public Appender<ILoggingEvent> getDefaultAppender() {
    OutputStreamAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
    appender.setName(DEFAULT_CONSOLE_APPENDER_NAME);
    appender.setContext(loggerContext);
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setPattern(LOG_PATTERN_DEFAULT);
    encoder.setContext(loggerContext);
    encoder.start();
    appender.setEncoder(encoder);
    appender.start();
    return appender;
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent)

Example 22 with ConsoleAppender

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

the class LoggingConfig method forceVerboseFileEncoder.

public static synchronized void forceVerboseFileEncoder() {
    final String timeStampFormat = "yyyy-MM-dd HH:mm:ss.SSS";
    if (loggerContext == null) {
        LOGGER.error("Unknown loggerContext, aborting buffered logging. Make sure that loadFile() has been called first.");
        return;
    }
    // Build iterator
    Iterators<Appender<ILoggingEvent>> iterators = new Iterators<>();
    // Add CacheLogger or rootLogger appenders depending on whether CacheLogger is active.
    if (CacheLogger.isActive()) {
        iterators.addIterator(CacheLogger.iteratorForAppenders());
    } else {
        iterators.addIterator(rootLogger.iteratorForAppenders());
    }
    // If syslog is active there probably are detached appenders there as well
    if (!syslogDetachedAppenders.isEmpty()) {
        iterators.addList(syslogDetachedAppenders);
    }
    // Iterate
    Iterator<Appender<ILoggingEvent>> it = iterators.combinedIterator();
    while (it.hasNext()) {
        Appender<ILoggingEvent> appender = it.next();
        if (appender instanceof OutputStreamAppender && !(appender instanceof ConsoleAppender<?>)) {
            // Appender has Encoder property
            Encoder<ILoggingEvent> encoder = ((OutputStreamAppender<ILoggingEvent>) appender).getEncoder();
            if (encoder instanceof PatternLayoutEncoder) {
                // Encoder has pattern
                PatternLayoutEncoder patternEncoder = (PatternLayoutEncoder) encoder;
                String logPattern = patternEncoder.getPattern();
                // Set timestamp format
                Pattern pattern = Pattern.compile("%((date|d)(\\{([^\\}]*)\\})?)(?=\\s)");
                Matcher matcher = pattern.matcher(logPattern);
                if (matcher.find()) {
                    boolean replace = true;
                    if (matcher.group(4) != null && matcher.group(4).equals(timeStampFormat)) {
                        replace = false;
                    }
                    if (replace) {
                        logPattern = logPattern.replaceFirst(pattern.pattern(), "%d{" + timeStampFormat + "}");
                    }
                } else {
                    if (logPattern.startsWith("%-5level")) {
                        logPattern = logPattern.substring(0, 8) + " %d{" + timeStampFormat + "}" + logPattern.substring(8);
                    } else {
                        logPattern = "d%{" + timeStampFormat + "} " + logPattern;
                    }
                }
                // Make sure %logger is included
                pattern = Pattern.compile("((%logger|%lo|%c)(\\{\\d+\\})?)(?=\\s)");
                matcher = pattern.matcher(logPattern);
                if (matcher.find()) {
                    boolean replace = true;
                    if (matcher.group().equals("%logger")) {
                        replace = false;
                    }
                    if (replace) {
                        logPattern = logPattern.replaceFirst(pattern.pattern(), "%logger");
                    }
                } else {
                    if (logPattern.contains("%msg")) {
                        logPattern = logPattern.substring(0, logPattern.indexOf("%msg")) + "%logger " + logPattern.substring(logPattern.indexOf("%msg"));
                    } else {
                        logPattern = "%logger " + logPattern;
                    }
                }
                // Activate changes
                patternEncoder.setPattern(logPattern);
                patternEncoder.start();
            }
        }
    }
    LOGGER.info("Verbose file logging pattern enforced");
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) OutputStreamAppender(ch.qos.logback.core.OutputStreamAppender) FileAppender(ch.qos.logback.core.FileAppender) SyslogAppender(ch.qos.logback.classic.net.SyslogAppender) Appender(ch.qos.logback.core.Appender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) OutputStreamAppender(ch.qos.logback.core.OutputStreamAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Iterators(net.pms.util.Iterators)

Example 23 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender in project pom-manipulation-ext by release-engineering.

the class TestUtils method runCli.

/**
 * Run pom-manipulation-cli via run() with java params (-D arguments) in workingDir directory.
 *
 * @param args - List of additional command line arguments
 * @param params - Map of String keys and String values representing -D arguments
 * @param workingDir - Working directory in which you want the cli to be run.
 * @return Exit value
 * @throws Exception if an error occurs
 */
public static Integer runCli(List<String> args, Map<String, String> params, String workingDir) throws Exception {
    ArrayList<String> arguments = new ArrayList<>(args);
    Collections.addAll(arguments, toJavaParams(params).split("\\s+"));
    boolean argsContainsFile = false;
    for (String s : args) {
        if (s.startsWith("--file")) {
            argsContainsFile = true;
        }
    }
    if (!argsContainsFile) {
        arguments.add("--log=" + workingDir + File.separator + "build.log");
        arguments.add("--file=" + workingDir + File.separator + "pom.xml");
    }
    logger.info("Invoking CLI with {} ", arguments);
    int result = new Cli().run(arguments.toArray(new String[arguments.size()]));
    // This is a bit of a hack. The CLI, if log-to-file is enabled resets the logging. As we don't fork and run
    // in the same process this means we need to reset it back again. The benefit of not forking is a simpler test
    // harness and it saves time when running the tests.
    final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    loggerContext.reset();
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setPattern("[%t] %level %logger{32} - %msg%n");
    ple.setContext(loggerContext);
    ple.start();
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setEncoder(ple);
    consoleAppender.setContext(loggerContext);
    consoleAppender.start();
    root.addAppender(consoleAppender);
    root.setLevel(Level.DEBUG);
    return result;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) ConsoleAppender(ch.qos.logback.core.ConsoleAppender) Cli(org.commonjava.maven.ext.cli.Cli) ArrayList(java.util.ArrayList) Logger(org.slf4j.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 24 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender in project ANNIS by korpling.

the class AnnisBaseRunner method setupLogging.

// configure logging
public static void setupLogging(boolean console) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator jc = new JoranConfigurator();
    jc.setContext(loggerContext);
    loggerContext.reset();
    try {
        jc.doConfigure(System.getProperty("annis.home") + "/conf/logback.xml");
    } catch (JoranException ex) {
        System.out.println(ex.getMessage());
    }
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setContext(loggerContext);
    consoleAppender.setName("CONSOLE");
    consoleAppender.setLayout(new ConsoleLayout());
    ThresholdFilter consoleFilter = new ConsoleFilter();
    consoleFilter.setLevel(console ? "DEBUG" : "WARN");
    consoleFilter.start();
    consoleAppender.addFilter(consoleFilter);
    consoleAppender.setTarget("System.err");
    consoleAppender.start();
    ch.qos.logback.classic.Logger logbackLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
    logbackLogger.addAppender(consoleAppender);
    SLF4JBridgeHandler.removeHandlersForRootLogger();
    ;
    SLF4JBridgeHandler.install();
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) ThresholdFilter(ch.qos.logback.classic.filter.ThresholdFilter) JoranException(ch.qos.logback.core.joran.spi.JoranException) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 25 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender in project kie-wb-common by kiegroup.

the class LogbackConfig method setConsoleAppender.

private ConsoleAppender setConsoleAppender(LoggerContext loggerContext, PatternLayoutEncoder pl) {
    ConsoleAppender consoleAppender = new ConsoleAppender<>();
    consoleAppender.setName("consoleAppender");
    consoleAppender.setContext(loggerContext);
    consoleAppender.setEncoder(pl);
    consoleAppender.start();
    return consoleAppender;
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender)

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