Search in sources :

Example 76 with Appender

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

the class ITJULIntegration method testJULLogging.

/**
     * Checks the default settings. It runs the bundle with minimum dependencies
     */
@Test
public void testJULLogging() throws Exception {
    java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger("foo.jul.1");
    org.slf4j.Logger slf4jLogger = LoggerFactory.getLogger("foo.jul.1");
    assertEquals(java.util.logging.Level.FINEST, julLogger.getLevel());
    assertTrue(slf4jLogger.isTraceEnabled());
    // Now add an appender and see if JUL logs are handled
    TestAppender ta = new TestAppender();
    Dictionary<String, Object> props = new Hashtable<String, Object>();
    String[] loggers = { "foo.jul.1" };
    ch.qos.logback.classic.Logger bar = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(loggers[0]);
    bar.setLevel(Level.INFO);
    props.put("loggers", loggers);
    ServiceRegistration sr = bundleContext.registerService(Appender.class.getName(), ta, props);
    delay();
    // Level should be INFO now
    assertEquals(java.util.logging.Level.INFO, julLogger.getLevel());
    julLogger.info("Info message");
    julLogger.fine("Fine message");
    assertEquals(1, ta.events.size());
}
Also used : Appender(ch.qos.logback.core.Appender) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 77 with Appender

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

the class SlingLogPanel method appendOtherLoggerData.

private void appendOtherLoggerData(final PrintWriter pw, final LoggerStateContext ctx) throws UnsupportedEncodingException {
    if (ctx.nonOSgiConfiguredLoggers.isEmpty()) {
        return;
    }
    pw.println("<div class='table'>");
    pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>Logger (Configured via other means)</div>");
    pw.println("<table class='nicetable ui-widget'>");
    pw.println("<thead class='ui-widget-header'>");
    pw.println("<tr>");
    pw.println("<th>Log Level</th>");
    pw.println("<th>Additivity</th>");
    pw.println("<th>Name</th>");
    pw.println("<th>Appender</th>");
    pw.println("</tr>");
    pw.println("</thead>");
    pw.println("<tbody class='ui-widget-content'>");
    for (final Logger logger : ctx.nonOSgiConfiguredLoggers) {
        pw.println("<tr>");
        pw.print("<td>");
        pw.print(logger.getLevel());
        pw.println("</td>");
        pw.print("<td>");
        pw.print(Boolean.toString(logger.isAdditive()));
        pw.println("</td>");
        pw.print("<td>");
        pw.print(XmlUtil.escapeXml(logger.getName()));
        pw.println("</td>");
        pw.println("<td>");
        pw.println("<ul>");
        final Iterator<Appender<ILoggingEvent>> itr = logger.iteratorForAppenders();
        while (itr.hasNext()) {
            final Appender<ILoggingEvent> a = itr.next();
            pw.print("<li>");
            pw.print(XmlUtil.escapeXml(getName(a)));
            pw.print("</li>");
        }
        pw.println("</ul>");
        pw.println("</td>");
        pw.println("</tr>");
    }
    pw.println("</tbody>");
    pw.println("</table>");
    pw.println("</div>");
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) Appender(ch.qos.logback.core.Appender) SlingRollingFileAppender(org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent)

Example 78 with Appender

use of ch.qos.logback.core.Appender in project cdap by caskdata.

the class LogStageAppender method stop.

@Override
public void stop() {
    super.stop();
    RuntimeException ex = null;
    for (Appender appender : appenders) {
        try {
            appender.stop();
        } catch (Throwable t) {
            if (ex == null) {
                ex = new RuntimeException(t);
            } else {
                ex.addSuppressed(t);
            }
        }
    }
    if (ex != null) {
        throw ex;
    }
}
Also used : Appender(ch.qos.logback.core.Appender)

Example 79 with Appender

use of ch.qos.logback.core.Appender 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 80 with Appender

use of ch.qos.logback.core.Appender in project jhipster-sample-app-hazelcast by jhipster.

the class LoggingConfiguration method setMetricsMarkerLogbackFilter.

// Configure a log filter to remove "metrics" logs from all appenders except the "LOGSTASH" appender
private void setMetricsMarkerLogbackFilter(LoggerContext context) {
    log.info("Filtering metrics logs from all appenders except the {} appender", LOGSTASH_APPENDER_NAME);
    OnMarkerEvaluator onMarkerMetricsEvaluator = new OnMarkerEvaluator();
    onMarkerMetricsEvaluator.setContext(context);
    onMarkerMetricsEvaluator.addMarker("metrics");
    onMarkerMetricsEvaluator.start();
    EvaluatorFilter<ILoggingEvent> metricsFilter = new EvaluatorFilter<>();
    metricsFilter.setContext(context);
    metricsFilter.setEvaluator(onMarkerMetricsEvaluator);
    metricsFilter.setOnMatch(FilterReply.DENY);
    metricsFilter.start();
    for (ch.qos.logback.classic.Logger logger : context.getLoggerList()) {
        for (Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders(); it.hasNext(); ) {
            Appender<ILoggingEvent> appender = it.next();
            if (!appender.getName().equals(ASYNC_LOGSTASH_APPENDER_NAME)) {
                log.debug("Filter metrics logs from the {} appender", appender.getName());
                appender.setContext(context);
                appender.addFilter(metricsFilter);
                appender.start();
            }
        }
    }
}
Also used : LogstashTcpSocketAppender(net.logstash.logback.appender.LogstashTcpSocketAppender) Appender(ch.qos.logback.core.Appender) AsyncAppender(ch.qos.logback.classic.AsyncAppender) OnMarkerEvaluator(ch.qos.logback.classic.boolex.OnMarkerEvaluator) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) EvaluatorFilter(ch.qos.logback.core.filter.EvaluatorFilter)

Aggregations

Appender (ch.qos.logback.core.Appender)118 Test (org.junit.Test)62 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)51 Logger (ch.qos.logback.classic.Logger)32 LoggerContext (ch.qos.logback.classic.LoggerContext)32 FileAppender (ch.qos.logback.core.FileAppender)27 Logger (org.slf4j.Logger)25 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)21 ArgumentMatcher (org.mockito.ArgumentMatcher)19 AsyncAppender (ch.qos.logback.classic.AsyncAppender)18 File (java.io.File)14 OnMarkerEvaluator (ch.qos.logback.classic.boolex.OnMarkerEvaluator)13 EvaluatorFilter (ch.qos.logback.core.filter.EvaluatorFilter)13 LogstashTcpSocketAppender (net.logstash.logback.appender.LogstashTcpSocketAppender)13 ArrayList (java.util.ArrayList)11 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)11 OutputStreamAppender (ch.qos.logback.core.OutputStreamAppender)10 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)9 DBAppender (ch.qos.logback.classic.db.DBAppender)8 Test (org.junit.jupiter.api.Test)7