use of org.apache.logging.log4j.core.config.Configuration in project logging-log4j2 by apache.
the class SimpleBenchmark method setup.
@Setup
public void setup() {
final Configuration config = (LoggerContext.getContext()).getConfiguration();
if (!DefaultConfiguration.DEFAULT_NAME.equals(config.getName())) {
System.out.println("Configuration was " + config.getName());
(LoggerContext.getContext()).start(new DefaultConfiguration());
}
logger = LogManager.getLogger(SimpleBenchmark.class.getName());
}
use of org.apache.logging.log4j.core.config.Configuration in project logging-log4j2 by apache.
the class ScriptConditionTest method testSelectFilesToDelete3.
@Test
@Category(Scripts.Groovy.class)
public void testSelectFilesToDelete3() {
final Configuration config = new DefaultConfiguration();
// creates the ScriptManager
config.initialize();
final List<PathWithAttributes> pathList = new ArrayList<>();
pathList.add(new PathWithAttributes(Paths.get("/path/1/abc/a.txt"), new DummyFileAttributes()));
pathList.add(new PathWithAttributes(Paths.get("/path/2/abc/bbb.txt"), new DummyFileAttributes()));
pathList.add(new PathWithAttributes(Paths.get("/path/3/abc/c.txt"), new DummyFileAttributes()));
final String scriptText = //
"" + //
"import java.nio.file.*;" + //
"def pattern = ~/(\\d*)[\\/\\\\]abc[\\/\\\\].*\\.txt/;" + //
"assert pattern.getClass() == java.util.regex.Pattern;" + "def copy = pathList.collect{it};" + //
"pathList.each { pathWithAttribs -> \n" + //
" def relative = basePath.relativize pathWithAttribs.path;" + //
" println 'relative path: ' + relative;" + " def str = relative.toString();" + //
" def m = pattern.matcher(str);" + //
" if (m.find()) {" + //
" def index = m.group(1) as int;" + //
" println 'extracted index: ' + index;" + " def isOdd = (index % 2) == 1;" + //
" println 'is odd: ' + isOdd;" + " if (isOdd) { copy.remove pathWithAttribs}" + //
" }" + //
"}" + "println copy;" + "copy;";
final Script script = new Script("test", "groovy", scriptText);
final ScriptCondition condition = new ScriptCondition(script, config);
final Path base = Paths.get("/path");
final List<PathWithAttributes> result = condition.selectFilesToDelete(base, pathList);
assertEquals(1, result.size());
assertEquals(Paths.get("/path/2/abc/bbb.txt"), result.get(0).getPath());
}
use of org.apache.logging.log4j.core.config.Configuration in project logging-log4j2 by apache.
the class JsonLayoutTest method testLayout.
/**
* Test case for MDC conversion pattern.
*/
@Test
public void testLayout() throws Exception {
final Map<String, Appender> appenders = this.rootLogger.getAppenders();
for (final Appender appender : appenders.values()) {
this.rootLogger.removeAppender(appender);
}
final Configuration configuration = rootLogger.getContext().getConfiguration();
// set up appender
// Use [[ and ]] to test header and footer (instead of [ and ])
final boolean propertiesAsList = false;
// @formatter:off
final AbstractJacksonLayout layout = JsonLayout.newBuilder().setConfiguration(configuration).setLocationInfo(true).setProperties(true).setPropertiesAsList(propertiesAsList).setComplete(true).setCompact(false).setEventEol(false).setHeader("[[".getBytes(Charset.defaultCharset())).setFooter("]]".getBytes(Charset.defaultCharset())).setIncludeStacktrace(true).build();
// @formatter:on
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
// set appender on root and set level to debug
this.rootLogger.addAppender(appender);
this.rootLogger.setLevel(Level.DEBUG);
// output starting message
this.rootLogger.debug("starting mdc pattern test");
this.rootLogger.debug("empty mdc");
ThreadContext.put("key1", "value1");
ThreadContext.put("key2", "value2");
this.rootLogger.debug("filled mdc");
ThreadContext.remove("key1");
ThreadContext.remove("key2");
this.rootLogger.error("finished mdc pattern test", new NullPointerException("test"));
appender.stop();
final List<String> list = appender.getMessages();
this.checkAt("[[", 0, list);
this.checkAt("{", 1, list);
this.checkContains("\"loggerFqcn\" : \"" + AbstractLogger.class.getName() + "\",", list);
this.checkContains("\"level\" : \"DEBUG\",", list);
this.checkContains("\"message\" : \"starting mdc pattern test\",", list);
for (final Appender app : appenders.values()) {
this.rootLogger.addAppender(app);
}
}
use of org.apache.logging.log4j.core.config.Configuration in project gatk by broadinstitute.
the class LoggingUtils method setLog4JLoggingLevel.
private static void setLog4JLoggingLevel(Log.LogLevel verbosity) {
// Now establish the logging level used by log4j by propagating the requested
// logging level to all loggers associated with our logging configuration.
final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
final Configuration loggerContextConfig = loggerContext.getConfiguration();
final String contextClassName = LoggingUtils.class.getName();
final LoggerConfig loggerConfig = loggerContextConfig.getLoggerConfig(contextClassName);
loggerConfig.setLevel(levelToLog4jLevel(verbosity));
loggerContext.updateLoggers();
}
use of org.apache.logging.log4j.core.config.Configuration in project Anserini by castorini.
the class TrainingDataGenerator method createNewLoggerConfig.
/**
* Dynamically creates a logger configuration with an appender that writes to a file.
* This logger is used to write training data.
*
* @param loggerName the name of the logger to create
* @param outputFilePath the file path to write logs to
* @param patternLayout layout for the logger, if null just display
* the message using DEFAULT_CONVERSION_PATTERN
*/
private static void createNewLoggerConfig(String loggerName, String outputFilePath, String patternLayout) {
// Ignore null output files
if (outputFilePath == null)
return;
// Create a logger to write the training data
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
Layout layout = PatternLayout.createLayout(patternLayout == null ? PatternLayout.DEFAULT_CONVERSION_PATTERN : patternLayout, config, null, StandardCharsets.UTF_8, false, false, null, null);
Appender appender = FileAppender.createAppender(outputFilePath, "false", "false", loggerName, "true", "false", "false", "2000", layout, null, "false", null, config);
appender.start();
config.addAppender(appender);
// Adding reference to the appender
AppenderRef ref = AppenderRef.createAppenderRef(loggerName, null, null);
AppenderRef[] refs = new AppenderRef[] { ref };
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.TRACE, loggerName, "true", refs, null, config, null);
// Adding appender to logger, and adding logger to context
loggerConfig.addAppender(appender, null, null);
config.addLogger(loggerName, loggerConfig);
ctx.updateLoggers();
}
Aggregations