Search in sources :

Example 1 with ConsoleAppender

use of org.apache.logging.log4j.core.appender.ConsoleAppender in project ignite by apache.

the class Log4J2Logger method createConsoleLogger.

/**
     * Creates console appender with some reasonable default logging settings.
     *
     * @return Logger with auto configured console appender.
     */
public static Logger createConsoleLogger() {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(true);
    Configuration cfg = ctx.getConfiguration();
    PatternLayout.Builder builder = PatternLayout.newBuilder();
    builder.withPattern("%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n").withCharset(Charset.defaultCharset()).withAlwaysWriteExceptions(false).withNoConsoleNoAnsi(false);
    PatternLayout layout = builder.build();
    ConsoleAppender.Builder consoleAppenderBuilder = ConsoleAppender.newBuilder();
    consoleAppenderBuilder.withName(CONSOLE_APPENDER).withLayout(layout);
    ConsoleAppender consoleApp = consoleAppenderBuilder.build();
    consoleApp.start();
    AppenderRef ref = AppenderRef.createAppenderRef(CONSOLE_APPENDER, Level.TRACE, null);
    AppenderRef[] refs = { ref };
    LoggerConfig logCfg = LoggerConfig.createLogger(false, Level.INFO, LogManager.ROOT_LOGGER_NAME, "", refs, null, null, null);
    logCfg.addAppender(consoleApp, null, null);
    cfg.addAppender(consoleApp);
    cfg.addLogger(LogManager.ROOT_LOGGER_NAME, logCfg);
    ctx.updateLoggers(cfg);
    return (Logger) LogManager.getContext().getLogger(LogManager.ROOT_LOGGER_NAME);
}
Also used : ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef) IgniteLogger(org.apache.ignite.IgniteLogger) Logger(org.apache.logging.log4j.core.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 2 with ConsoleAppender

use of org.apache.logging.log4j.core.appender.ConsoleAppender in project logging-log4j2 by apache.

the class CompositeConfigurationTest method compositeLogger.

/*
    @Test
    public void compositeConfigurationUsed() {
        final LoggerContextRule lcr = new LoggerContextRule(
                "classpath:log4j-comp-appender.xml,log4j-comp-appender.json");
        Statement test = new Statement() {
            @Override
            public void evaluate() throws Throwable {
                assertTrue(lcr.getConfiguration() instanceof CompositeConfiguration);
            }
        };
        runTest(lcr, test);
    }

    @Test
    public void compositeProperties() {
        final LoggerContextRule lcr = new LoggerContextRule(
                "classpath:log4j-comp-properties.xml,log4j-comp-properties.json");
        Statement test = new Statement() {
            @Override
            public void evaluate() throws Throwable {
                CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration();
                assertEquals("json", config.getStrSubstitutor().replace("${propertyShared}"));
                assertEquals("xml", config.getStrSubstitutor().replace("${propertyXml}"));
                assertEquals("json", config.getStrSubstitutor().replace("${propertyJson}"));
            }
        };
        runTest(lcr, test);
    }

    @Test
    public void compositeAppenders() {
        final LoggerContextRule lcr = new LoggerContextRule(
                "classpath:log4j-comp-appender.xml,log4j-comp-appender.json");
        Statement test = new Statement() {
            @Override
            public void evaluate() throws Throwable {
                CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration();
                Map<String, Appender> appender = config.getAppenders();
                assertEquals(3, appender.size());
                assertTrue(appender.get("STDOUT") instanceof ConsoleAppender);
                assertTrue(appender.get("File") instanceof FileAppender);
                assertTrue(appender.get("Override") instanceof RollingFileAppender);
            }
        };
        runTest(lcr, test);
    }
*/
@Test
public void compositeLogger() {
    final LoggerContextRule lcr = new LoggerContextRule("classpath:log4j-comp-logger.xml,log4j-comp-logger.json");
    final Statement test = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration();
            Map<String, Appender> appendersMap = config.getLogger("cat1").getAppenders();
            assertEquals("Expected 2 Appender references for cat1 but got " + appendersMap.size(), 2, appendersMap.size());
            assertTrue(appendersMap.get("STDOUT") instanceof ConsoleAppender);
            appendersMap = config.getLogger("cat2").getAppenders();
            assertEquals("Expected 1 Appender reference for cat2 but got " + appendersMap.size(), 1, appendersMap.size());
            assertTrue(appendersMap.get("File") instanceof FileAppender);
            appendersMap = config.getLogger("cat3").getAppenders();
            assertEquals("Expected 1 Appender reference for cat3 but got " + appendersMap.size(), 1, appendersMap.size());
            assertTrue(appendersMap.get("File") instanceof FileAppender);
            appendersMap = config.getRootLogger().getAppenders();
            assertEquals("Expected 2 Appender references for the root logger but got " + appendersMap.size(), 2, appendersMap.size());
            assertTrue(appendersMap.get("File") instanceof FileAppender);
            assertTrue(appendersMap.get("STDOUT") instanceof ConsoleAppender);
        }
    };
    runTest(lcr, test);
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Statement(org.junit.runners.model.Statement) CompositeConfiguration(org.apache.logging.log4j.core.config.composite.CompositeConfiguration) LoggerContextRule(org.apache.logging.log4j.junit.LoggerContextRule) Test(org.junit.Test)

Example 3 with ConsoleAppender

use of org.apache.logging.log4j.core.appender.ConsoleAppender in project logging-log4j2 by apache.

the class LookupTest method testHostname.

@Test
public void testHostname() {
    final ConsoleAppender app = context.getRequiredAppender("console", ConsoleAppender.class);
    final Layout<?> layout = app.getLayout();
    assertNotNull("No Layout", layout);
    assertTrue("Layout is not a PatternLayout", layout instanceof PatternLayout);
    final String pattern = ((PatternLayout) layout).getConversionPattern();
    assertNotNull("No conversion pattern", pattern);
    assertTrue("No filters", pattern.contains("org.junit,org.apache.maven,org.eclipse,sun.reflect,java.lang.reflect"));
}
Also used : ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) Test(org.junit.Test)

Example 4 with ConsoleAppender

use of org.apache.logging.log4j.core.appender.ConsoleAppender in project hive by apache.

the class LogDivertAppender method getDefaultLayout.

public Layout<? extends Serializable> getDefaultLayout() {
    // There should be a ConsoleAppender. Copy its Layout.
    Logger root = LogManager.getRootLogger();
    Layout layout = null;
    for (Appender ap : ((org.apache.logging.log4j.core.Logger) root).getAppenders().values()) {
        if (ap.getClass().equals(ConsoleAppender.class)) {
            layout = ap.getLayout();
            break;
        }
    }
    return layout;
}
Also used : Appender(org.apache.logging.log4j.core.Appender) AbstractOutputStreamAppender(org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) Layout(org.apache.logging.log4j.core.Layout) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) PerfLogger(org.apache.hadoop.hive.ql.log.PerfLogger) Logger(org.apache.logging.log4j.Logger)

Example 5 with ConsoleAppender

use of org.apache.logging.log4j.core.appender.ConsoleAppender in project oxCore by GluuFederation.

the class LoggingHelper method configureConsoleAppender.

public static void configureConsoleAppender() {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
    ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(PatternLayout.createDefaultLayout());
    appender.start();
    config.addAppender(appender);
    AppenderRef[] refs = new AppenderRef[] { AppenderRef.createAppenderRef(appender.getName(), null, null) };
    LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.ALL, LogManager.ROOT_LOGGER_NAME, "true", refs, null, config, null);
    loggerConfig.addAppender(appender, null, null);
    config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig);
    ctx.updateLoggers();
}
Also used : AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)7 Appender (org.apache.logging.log4j.core.Appender)3 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)3 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)3 IgniteLogger (org.apache.ignite.IgniteLogger)2 Logger (org.apache.logging.log4j.core.Logger)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)2 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)2 Configuration (org.apache.logging.log4j.core.config.Configuration)2 Test (org.junit.Test)2 PerfLogger (org.apache.hadoop.hive.ql.log.PerfLogger)1 Logger (org.apache.logging.log4j.Logger)1 Layout (org.apache.logging.log4j.core.Layout)1 AbstractOutputStreamAppender (org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender)1 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)1 RoutingAppender (org.apache.logging.log4j.core.appender.routing.RoutingAppender)1 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)1 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)1 LoggerContextRule (org.apache.logging.log4j.junit.LoggerContextRule)1