Search in sources :

Example 46 with Configuration

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());
}
Also used : DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Setup(org.openjdk.jmh.annotations.Setup)

Example 47 with Configuration

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());
}
Also used : Path(java.nio.file.Path) Script(org.apache.logging.log4j.core.script.Script) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) ArrayList(java.util.ArrayList) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 48 with Configuration

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);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Example 49 with Configuration

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();
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 50 with Configuration

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();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) Layout(org.apache.logging.log4j.core.Layout) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

Configuration (org.apache.logging.log4j.core.config.Configuration)105 LoggerContext (org.apache.logging.log4j.core.LoggerContext)54 Test (org.junit.Test)43 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)39 Appender (org.apache.logging.log4j.core.Appender)21 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)15 File (java.io.File)12 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)9 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)9 Path (java.nio.file.Path)7 Level (org.apache.logging.log4j.Level)7 NullConfiguration (org.apache.logging.log4j.core.config.NullConfiguration)7 ArrayList (java.util.ArrayList)6 Logger (org.apache.logging.log4j.Logger)6 Filter (org.apache.logging.log4j.core.Filter)6 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)6 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)5 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)5 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)4 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)4