Java Code Examples for org.apache.logging.log4j.core.LoggerContext#reconfigure()

The following examples show how to use org.apache.logging.log4j.core.LoggerContext#reconfigure() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Log4JController.java    From GreenSummer with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Reset.
 *
 * @return the response entity
 */
@RequestMapping(value = "reset", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<LogResponse> reset() {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    synchronized (ctx) {
        // If the config location is null, it means we are using a non-standard path for 
        // the config file (for example log4j2-spring.xml. In this case the name of the configuration
        // is usually the path to the configuration file. so we "fix" the path before reconfiguring
        if (ctx.getConfigLocation() == null) {
            ctx.setConfigLocation(Paths.get(ctx.getConfiguration().getName()).toUri());
        }
        ctx.reconfigure();
        initInMemoryAppender();
    }
    return new ResponseEntity<>(listLoggers(ctx), HttpStatus.OK);
}
 
Example 2
Source File: AdvertiserTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setupClass() {
    final File file = new File(STATUS_LOG);
    file.delete();
    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
    final LoggerContext ctx = LoggerContext.getContext();
    final Configuration config = ctx.getConfiguration();
    if (config instanceof XmlConfiguration) {
        final String name = config.getName();
        if (name == null || !name.equals("XMLConfigTest")) {
            ctx.reconfigure();
        }
    } else {
        ctx.reconfigure();
    }
}
 
Example 3
Source File: RollingAppenderDirectWriteWithReconfigureTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testRollingFileAppenderWithReconfigure() throws Exception {
    logger.debug("Before reconfigure");

    @SuppressWarnings("resource") // managed by the rule.
    final LoggerContext context = loggerContextRule.getLoggerContext();
    Configuration config = context.getConfiguration();
    context.setConfigLocation(new URI(CONFIG));
    context.reconfigure();
    logger.debug("Force a rollover");
    final File dir = new File(DIR);
    for (int i = 0; i < MAX_TRIES; ++i) {
        Thread.sleep(200);
        if (config != context.getConfiguration()) {
            break;
        }
    }

    assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
    final File[] files = dir.listFiles();
    assertNotNull(files);
    assertThat(dir.listFiles().length, is(equalTo(2)));
}
 
Example 4
Source File: AbstractJpaAppenderTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
public void tearDown() throws SQLException {
    final LoggerContext context = LoggerContext.getContext(false);
    try {
        String appenderName = "databaseAppender";
        final Appender appender = context.getConfiguration().getAppender(appenderName);
        assertNotNull("The appender '" + appenderName + "' should not be null.", appender);
        assertTrue("The appender should be a JpaAppender.", appender instanceof JpaAppender);
        ((JpaAppender) appender).getManager().close();
    } finally {
        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
        PropertiesUtil.getProperties().reload();
        context.reconfigure();
        StatusLogger.getLogger().reset();

        try (Statement statement = this.connection.createStatement();) {
            statement.execute("SHUTDOWN");
        }

        this.connection.close();
    }
}
 
Example 5
Source File: JdbcAppenderBenchmark.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Setup
public void setup() throws Exception {
    connectionHSQLDB = getConnectionHSQLDB();
    connectionH2 = getConnectionH2();
    createTable(connectionHSQLDB, toCreateTableSqlStringHQLDB("fmLogEntry"));
    createTable(connectionH2, toCreateTableSqlStringH2("fmLogEntry"));

    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "log4j2-jdbc-appender.xml");
    final LoggerContext context = LoggerContext.getContext(false);
    if (context.getConfiguration() instanceof DefaultConfiguration) {
        context.reconfigure();
    }
    StatusLogger.getLogger().reset();
    loggerH2 = LogManager.getLogger("H2Logger");
    loggerHSQLDB = LogManager.getLogger("HSQLDBLogger");
}
 
Example 6
Source File: LindenServer.java    From linden with Apache License 2.0 6 votes vote down vote up
public LindenServer(String conf) throws Exception {
  File lindenProperties = new File(FilenameUtils.concat(conf, LINDEN_PROPERTIES));
  File schemaXml = new File(FilenameUtils.concat(conf, SCHEMA_XML));
  Preconditions.checkArgument(lindenProperties.exists(), "can not find linden.properties.");

  lindenConf = LindenConfigBuilder.build(lindenProperties);
  if (schemaXml.exists()) {
    LindenSchema schema = LindenSchemaBuilder.build(schemaXml);
    lindenConf.setSchema(schema);
  } else {
    throw new Exception("schema.xml not found.");
  }
  port = lindenConf.getPort();
  Preconditions.checkNotNull(lindenConf.getLogPath(), "log path can not be null.");
  System.setProperty(LOG_PATH, lindenConf.getLogPath());
  System.setProperty(LOG4J_SHUTDOWN_HOOK_ENABLED, "false");
  System.setProperty(LOG4J_CONTEXT_SELECTOR, "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
  LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
  ctx.setConfigLocation(new File(FilenameUtils.concat(conf, LOG4j2_XML)).toURI());
  ctx.reconfigure();
  LOGGER = LoggerFactory.getLogger(LindenServer.class);
}
 
Example 7
Source File: JpaAppenderBenchmark.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup() throws Exception {
    connectionHSQLDB = getConnectionHSQLDB();
    connectionH2 = getConnectionH2();

    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "log4j2-jpa-appender.xml");
    final LoggerContext context = LoggerContext.getContext(false);
    if (context.getConfiguration() instanceof DefaultConfiguration) {
        context.reconfigure();
    }
    StatusLogger.getLogger().reset();
    loggerH2 = LogManager.getLogger("H2Logger");
    loggerHSQLDB = LogManager.getLogger("HSQLDBLogger");
}
 
Example 8
Source File: YamlLayoutTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setupClass() {
    ThreadContext.clearAll();
    ConfigurationFactory.setConfigurationFactory(cf);
    final LoggerContext ctx = LoggerContext.getContext();
    ctx.reconfigure();
}
 
Example 9
Source File: JsonLayoutTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setupClass() {
    ThreadContext.clearAll();
    ConfigurationFactory.setConfigurationFactory(cf);
    final LoggerContext ctx = LoggerContext.getContext();
    ctx.reconfigure();
}
 
Example 10
Source File: AdvertiserTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void cleanupClass() {
    System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
    final LoggerContext ctx = LoggerContext.getContext();
    ctx.reconfigure();
    StatusLogger.getLogger().reset();
    final File file = new File(STATUS_LOG);
    file.delete();
}
 
Example 11
Source File: XmlConfigurationPropsTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void cleanupClass() {
    System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
    final LoggerContext ctx = LoggerContext.getContext();
    ctx.reconfigure();
    StatusLogger.getLogger().reset();
}
 
Example 12
Source File: XmlConfigurationPropsTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoProps() {
    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
    final LoggerContext ctx = LoggerContext.getContext();
    ctx.reconfigure();
    final Configuration config = ctx.getConfiguration();
    assertTrue("Configuration is not an XmlConfiguration", config instanceof XmlConfiguration);
}
 
Example 13
Source File: XmlConfigurationPropsTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultStatus() {
    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG1);
    System.setProperty(Constants.LOG4J_DEFAULT_STATUS_LEVEL, "WARN");
    try {
        final LoggerContext ctx = LoggerContext.getContext();
        ctx.reconfigure();
        final Configuration config = ctx.getConfiguration();
        assertTrue("Configuration is not an XmlConfiguration", config instanceof XmlConfiguration);
    } finally {
        System.clearProperty(Constants.LOG4J_DEFAULT_STATUS_LEVEL);
    }
}
 
Example 14
Source File: XmlConfigurationPropsTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithProps() {
    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
    System.setProperty("log4j.level", "warn");
    System.setProperty("log.level", "warn");
    try {
        final LoggerContext ctx = LoggerContext.getContext();
        ctx.reconfigure();
        final Configuration config = ctx.getConfiguration();
        assertTrue("Configuration is not an XmlConfiguration", config instanceof XmlConfiguration);
    } finally {
        System.clearProperty("log4j.level");
        System.clearProperty("log.level");
    }
}
 
Example 15
Source File: HtmlLayoutTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setupClass() {
    ConfigurationFactory.setConfigurationFactory(cf);
    final LoggerContext ctx = LoggerContext.getContext();
    ctx.reconfigure();
}
 
Example 16
Source File: CsvLogEventLayoutTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setupClass() {
    ConfigurationFactory.setConfigurationFactory(cf);
    final LoggerContext ctx = LoggerContext.getContext();
    ctx.reconfigure();
}
 
Example 17
Source File: PatternLayoutTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setupClass() {
    ConfigurationFactory.setConfigurationFactory(cf);
    final LoggerContext ctx = LoggerContext.getContext();
    ctx.reconfigure();
}
 
Example 18
Source File: CustomConfigurationTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testConfig() {
    // don't bother using "error" since that's the default; try another level
    final LoggerContext ctx = this.init.getLoggerContext();
    ctx.reconfigure();
    final Configuration config = ctx.getConfiguration();
    assertThat(config, instanceOf(XmlConfiguration.class));
    for (final StatusListener listener : StatusLogger.getLogger().getListeners()) {
        if (listener instanceof StatusConsoleListener) {
            assertSame(listener.getStatusLevel(), Level.INFO);
            break;
        }
    }
    final Layout<? extends Serializable> layout = PatternLayout.newBuilder()
        .setPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN)
        .setConfiguration(config)
        .build();
    // @formatter:off
    final FileAppender appender = FileAppender.newBuilder()
        .setFileName(LOG_FILE)
        .setAppend(false)
        .setName("File")
        .setIgnoreExceptions(false)
        .setBufferSize(4000)
        .setBufferedIo(false)
        .setLayout(layout)
        .build();
    // @formatter:on
    appender.start();
    config.addAppender(appender);
    final AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
    final AppenderRef[] refs = new AppenderRef[] {ref};

    final LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.INFO, "org.apache.logging.log4j",
        "true", refs, null, config, null );
    loggerConfig.addAppender(appender, null, null);
    config.addLogger("org.apache.logging.log4j", loggerConfig);
    ctx.updateLoggers();
    final Logger logger = ctx.getLogger(CustomConfigurationTest.class.getName());
    logger.info("This is a test");
    final File file = new File(LOG_FILE);
    assertThat(file, exists());
    assertThat(file, hasLength(greaterThan(0L)));
}
 
Example 19
Source File: SyslogLayoutTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setupClass() {
    ConfigurationFactory.setConfigurationFactory(cf);
    final LoggerContext ctx = LoggerContext.getContext();
    ctx.reconfigure();
}
 
Example 20
Source File: DynamicThresholdFilterTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@After
public void cleanup() {
    final LoggerContext ctx = LoggerContext.getContext(false);
    ctx.reconfigure();
    StatusLogger.getLogger().reset();
}