Search in sources :

Example 1 with PatternLayout

use of org.apache.logging.log4j.core.layout.PatternLayout 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 PatternLayout

use of org.apache.logging.log4j.core.layout.PatternLayout in project logging-log4j2 by apache.

the class Log4j1ConfigurationFactoryTest method testConsoleSimpleLayout.

@Test
public void testConsoleSimpleLayout() throws Exception {
    final PatternLayout layout = (PatternLayout) testConsole("config-1.2/log4j-console-SimpleLayout.properties");
    assertEquals("%level - %m%n", layout.getConversionPattern());
}
Also used : PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) Test(org.junit.Test)

Example 3 with PatternLayout

use of org.apache.logging.log4j.core.layout.PatternLayout 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 PatternLayout

use of org.apache.logging.log4j.core.layout.PatternLayout in project logging-log4j2 by apache.

the class ConsoleAppenderBuilderTest method testDefaultLayoutDefaultCharset.

/**
     * Tests https://issues.apache.org/jira/browse/LOG4J2-1636
     * 
     * Tested with Oracle 7 and 8 and IBM Java 8.
     */
@Test
public void testDefaultLayoutDefaultCharset() {
    final ConsoleAppender appender = ConsoleAppender.newBuilder().withName("test").build();
    final PatternLayout layout = (PatternLayout) appender.getLayout();
    final String charsetName = System.getProperty("sun.stdout.encoding");
    final String expectedName = charsetName != null ? charsetName : Charset.defaultCharset().name();
    Assert.assertEquals(expectedName, layout.getCharset().name());
}
Also used : PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) Test(org.junit.Test)

Example 5 with PatternLayout

use of org.apache.logging.log4j.core.layout.PatternLayout in project logging-log4j2 by apache.

the class InMemoryAppenderTest method testHeaderRequested.

@Test
public void testHeaderRequested() {
    final PatternLayout layout = PatternLayout.newBuilder().withHeader("HEADERHEADER").build();
    final boolean writeHeader = true;
    final InMemoryAppender app = new InMemoryAppender("test", layout, null, false, writeHeader);
    final String expectedHeader = "HEADERHEADER";
    assertMessage("Test", app, expectedHeader);
}
Also used : InMemoryAppender(org.apache.logging.log4j.test.appender.InMemoryAppender) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) Test(org.junit.Test)

Aggregations

PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)17 Test (org.junit.Test)12 LoggerContext (org.apache.logging.log4j.core.LoggerContext)5 Configuration (org.apache.logging.log4j.core.config.Configuration)5 Appender (org.apache.logging.log4j.core.Appender)4 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)3 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)3 File (java.io.File)2 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)2 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)2 InMemoryAppender (org.apache.logging.log4j.test.appender.InMemoryAppender)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 Layout (org.apache.logging.log4j.core.Layout)1 Logger (org.apache.logging.log4j.core.Logger)1 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)1 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)1 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)1