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);
}
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);
}
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"));
}
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;
}
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();
}
Aggregations