Java Code Examples for org.apache.logging.log4j.core.Logger#info()

The following examples show how to use org.apache.logging.log4j.core.Logger#info() . 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: JeroMqAppenderTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test(timeout = DEFAULT_TIMEOUT_MILLIS)
public void testClientServer() throws Exception {
    final JeroMqAppender appender = ctx.getRequiredAppender(APPENDER_NAME, JeroMqAppender.class);
    final int expectedReceiveCount = 3;
    final JeroMqTestClient client = new JeroMqTestClient(JeroMqManager.getContext(), ENDPOINT, expectedReceiveCount);
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    try {
        final Future<List<String>> future = executor.submit(client);
        Thread.sleep(100);
        final Logger logger = ctx.getLogger(getClass().getName());
        appender.resetSendRcs();
        logger.info("Hello");
        logger.info("Again");
        ThreadContext.put("foo", "bar");
        logger.info("World");
        final List<String> list = future.get();
        Assert.assertEquals(expectedReceiveCount, appender.getSendRcTrue());
        Assert.assertEquals(0, appender.getSendRcFalse());
        Assert.assertEquals("Hello", list.get(0));
        Assert.assertEquals("Again", list.get(1));
        Assert.assertEquals("barWorld", list.get(2));
    } finally {
        executor.shutdown();
    }
}
 
Example 2
Source File: JeroMqAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test(timeout = DEFAULT_TIMEOUT_MILLIS)
public void testServerOnly() throws Exception {
    final Logger logger = ctx.getLogger(getClass().getName());
    final JeroMqAppender appender = ctx.getRequiredAppender(APPENDER_NAME, JeroMqAppender.class);
    appender.resetSendRcs();
    logger.info("Hello");
    logger.info("Again");
    Assert.assertEquals(2, appender.getSendRcTrue());
    Assert.assertEquals(0, appender.getSendRcFalse());
}
 
Example 3
Source File: ReconfigureAppenderTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void addAndRemoveAppenderTest()
{
	// this will create a rolling file appender and add it to the logger
	// of this class. The file manager is created for the first time.
	// see AbstractManager.getManager(...).
	this.createAndAddAppender();

	// let's write something to the logger to ensure the output stream is opened.
	// We expect this call to create a a new output stream (which is does).
	// see OutputStreamManager.writeToDestination(...).
	Logger logger = (Logger)LogManager.getLogger(this.getClass());
	logger.info("test message 1");

	// this will close the rolling file appender and remove it from the logger
	// of this class. We expect the file output stream to be closed (which is it)
	// however the FileManager instance is kept in AbstractManager.MAP. This means that
	// when we create a new rolling file appender with the DirectWriteRolloverStrategy
	// this OLD file manager will be retrieved from the map (since it has the SAME file pattern)
	// and this is a problem as the output stream on that file manager is CLOSED. The problem
	// here is that we attempt to remove a file manager call NULL instead of FILE PATTERN
	this.removeAppender();

	// create a new rolling file appender for this logger. We expect this to create a new file
	// manager as the old one should have been removed. Since the instance of this file manager
	// is still in AbstractManager.MAP, it is returned and assigned to our new rolling file
	// appender instance. The problem here is that the file manager is create with the name
	// FILE PATTERN and that its output stream is closed.
	this.createAndAddAppender();

	// try and log something. This will not be logged anywhere. An exception will be thrown:
	// Caused by: java.io.IOException: Stream Closed
	logger.info("test message 2");

	// remove the appender as before.
	this.removeAppender();

	// this method will use reflection to go and remove the instance of FileManager from the AbstractManager.MAP
	// ourselves. This means that the rolling file appender has been stopped (previous method) AND its
	// manager has been removed.
	this.removeManagerUsingReflection();

	// now that the instance of FileManager is not present in MAP, creating the appender will actually
	// create a new rolling file manager, and put this in the map (keyed on file pattern again).
	this.createAndAddAppender();

	// because we have a new instance of file manager, this will create a new output stream. We can verify
	// this by looking inside the filepattern.1.log file inside the working directory, and noticing that
	// we have 'test message 1' followed by 'test message 3'. 'test message 2' is missing because we attempted
	// to write while the output stream was closed.
	logger.info("test message 3");

	// possible fixes:
	// 1) create the RollingFileManager and set it's name to FILE PATTERN when using DirectWriteRolloverStrategy
	// 2) when stopping the appender (and thus the manager), remove on FILE PATTERN if DirectWriteRolloverStrategy
	// 3) on OutputStreamManager.getOutputStream(), determine if the output stream is closed, and if it is create
	//              a new one. Note that this isn't really desirable as the only fix as if the file pattern had to change
	//              an instance of file manager would still exist in MAP, causing a resource leak.

	// now the obvious problem here is that if multiple file appenders use the same rolling file manager. We may run
	// into a case where the file manager is removed and the output stream is closed, and the remaining appenders
	// may not work correctly. I'm not sure  of the use case in this scenario, and if people actually do this
	// but based on the code it would be possible. I have also not tested this scenario out as it is not the
	// scenario we would ever use, but it should be considered while fixing this issue.
}