logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1484059 - /logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerUpdateTest.java
Date Sat, 18 May 2013 05:39:17 GMT
Author: rgoers
Date: Sat May 18 05:39:17 2013
New Revision: 1484059

URL: http://svn.apache.org/r1484059
Log:
Add unit test for updating the logging level

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerUpdateTest.java
      - copied, changed from r1484047, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java

Copied: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerUpdateTest.java
(from r1484047, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerUpdateTest.java?p2=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerUpdateTest.java&p1=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java&r1=1484047&r2=1484059&rev=1484059&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerUpdateTest.java
Sat May 18 05:39:17 2013
@@ -16,10 +16,12 @@
  */
 package org.apache.logging.log4j.core;
 
+import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.MarkerManager;
 import org.apache.logging.log4j.ThreadContext;
 import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.LoggerConfig;
 import org.apache.logging.log4j.core.config.XMLConfigurationFactory;
 import org.apache.logging.log4j.core.helpers.Constants;
 import org.apache.logging.log4j.core.helpers.NetUtils;
@@ -45,7 +47,7 @@ import static org.junit.Assert.*;
 /**
  *
  */
-public class LoggerTest {
+public class LoggerUpdateTest {
 
     private static final String CONFIG = "log4j-test2.xml";
     private static Configuration config;
@@ -87,180 +89,26 @@ public class LoggerTest {
     }
 
 
-    org.apache.logging.log4j.Logger logger = LogManager.getLogger("LoggerTest");
+    org.apache.logging.log4j.Logger logger = LogManager.getLogger("com.apache.test");
 
     @Test
-    public void basicFlow() {
+    public void resetLevel() {
         logger.entry();
-        logger.exit();
-        final List<LogEvent> events = app.getEvents();
-        assertTrue("Incorrect number of events. Expected 2, actual " + events.size(), events.size()
== 2);
-        app.clear();
-    }
-
-    @Test
-    public void simpleFlow() {
-        logger.entry(CONFIG);
-        logger.exit(0);
-        final List<LogEvent> events = app.getEvents();
-        assertTrue("Incorrect number of events. Expected 2, actual " + events.size(), events.size()
== 2);
-        app.clear();
-    }
-
-    @Test
-    public void throwing() {
-        logger.throwing(new IllegalArgumentException("Test Exception"));
-        final List<LogEvent> events = app.getEvents();
-        assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size()
== 1);
-        app.clear();
-    }
-
-    @Test
-    public void catching() {
-        try {
-            throw new NullPointerException();
-        } catch (final Exception e) {
-            logger.catching(e);
-        }
-        final List<LogEvent> events = app.getEvents();
-        assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size()
== 1);
-        app.clear();
-    }
-
-    @Test
-    public void debug() {
-        logger.debug("Debug message");
-        final List<LogEvent> events = app.getEvents();
-        assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size()
== 1);
-        app.clear();
-    }
-
-    @Test
-    public void getLogger_String_MessageFactoryMismatch() {
-        final Logger testLogger = testMessageFactoryMismatch("getLogger_String_MessageFactoryMismatch",
-                StringFormatterMessageFactory.INSTANCE, ParameterizedMessageFactory.INSTANCE);
-        testLogger.debug("%,d", Integer.MAX_VALUE);
-        final List<LogEvent> events = app.getEvents();
-        assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size()
== 1);
-        assertEquals(String.format("%,d", Integer.MAX_VALUE), events.get(0).getMessage().getFormattedMessage());
-    }
-
-    @Test
-    public void getLogger_String_MessageFactoryMismatchNull() {
-        final Logger testLogger =  testMessageFactoryMismatch("getLogger_String_MessageFactoryMismatchNull",
StringFormatterMessageFactory.INSTANCE, null);
-        testLogger.debug("%,d", Integer.MAX_VALUE);
-        final List<LogEvent> events = app.getEvents();
-        assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size()
== 1);
-        assertEquals(String.format("%,d", Integer.MAX_VALUE), events.get(0).getMessage().getFormattedMessage());
-    }
-
-    private Logger testMessageFactoryMismatch(final String name, final MessageFactory messageFactory1,
final MessageFactory messageFactory2) {
-        final Logger testLogger = (Logger) LogManager.getLogger(name, messageFactory1);
-        assertNotNull(testLogger);
-        assertEquals(messageFactory1, testLogger.getMessageFactory());
-        final Logger testLogger2 = (Logger) LogManager.getLogger(name, messageFactory2);
-        assertEquals(messageFactory1, testLogger2.getMessageFactory());
-        return testLogger;
-    }
-
-    @Test
-    public void debugObject() {
-        logger.debug(new Date());
-        final List<LogEvent> events = app.getEvents();
+        List<LogEvent> events = app.getEvents();
         assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size()
== 1);
         app.clear();
-    }
-
-    @Test
-    public void debugWithParms() {
-        logger.debug("Hello, {}", "World");
-        final List<LogEvent> events = app.getEvents();
-        assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size()
== 1);
-        app.clear();
-    }
-
-    @Test
-    public void testHostname() {
-        final org.apache.logging.log4j.Logger testLogger = LogManager.getLogger("org.apache.logging.log4j.hosttest");
-        testLogger.debug("Hello, {}", "World");
-        final List<String> msgs = host.getMessages();
-        assertTrue("Incorrect number of events. Expected 1, actual " + msgs.size(), msgs.size()
== 1);
-        final String expected = NetUtils.getLocalHostname() + Constants.LINE_SEP;
-        assertTrue("Incorrect hostname - expected " + expected + " actual - " + msgs.get(0),
-            msgs.get(0).endsWith(expected));
-
-    }
-
-    @Test
-    public void testImpliedThrowable() {
-        final org.apache.logging.log4j.Logger testLogger = LogManager.getLogger("org.apache.logging.log4j.hosttest");
-        testLogger.debug("This is a test", new Throwable("Testing"));
-        final List<String> msgs = host.getMessages();
-        assertTrue("Incorrect number of messages. Expected 1, actual " + msgs.size(), msgs.size()
== 1);
-        final String expected = "java.lang.Throwable: Testing";
-        assertTrue("Incorrect message data", msgs.get(0).contains(expected));
-    }
-
-
-    @Test
-    public void testSuppressedThrowable() {
-        final org.apache.logging.log4j.Logger testLogger = LogManager.getLogger("org.apache.logging.log4j.nothrown");
-        testLogger.debug("This is a test", new Throwable("Testing"));
-        final List<String> msgs = noThrown.getMessages();
-        assertTrue("Incorrect number of messages. Expected 1, actual " + msgs.size(), msgs.size()
== 1);
-        final String suppressed = "java.lang.Throwable: Testing";
-        assertTrue("Incorrect message data", !msgs.get(0).contains(suppressed));
-    }
-
-
-    @Test
-    public void mdc() {
-        ThreadContext.put("TestYear", "2010");
-        logger.debug("Debug message");
-        ThreadContext.clear();
-        logger.debug("Debug message");
-        final List<LogEvent> events = app.getEvents();
-        assertTrue("Incorrect number of events. Expected 2, actual " + events.size(), events.size()
== 2);
-        app.clear();
-    }
-
-    @Test
-    public void structuredData() {
-        ThreadContext.put("loginId", "JohnDoe");
-        ThreadContext.put("ipAddress", "192.168.0.120");
-        ThreadContext.put("locale", Locale.US.getDisplayName());
-        final StructuredDataMessage msg = new StructuredDataMessage("Audit@18060", "Transfer
Complete", "Transfer");
-        msg.put("ToAccount", "123456");
-        msg.put("FromAccount", "123457");
-        msg.put("Amount", "200.00");
-        logger.info(MarkerManager.getMarker("EVENT"), msg);
-        ThreadContext.clear();
-        final List<LogEvent> events = app.getEvents();
-        assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size()
== 1);
+        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+        Configuration config = ctx.getConfiguration();
+        LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
+        /* You could also specify the actual logger name as below and it will return the
LoggerConfig used by the Logger.
+           LoggerConfig loggerConfig = getLoggerConfig("com.apache.test");
+        */
+        loggerConfig.setLevel(Level.DEBUG);
+        ctx.updateLoggers();  // This causes all Loggers to refetch information from their
LoggerConfig.
+        logger.entry();
+        events = app.getEvents();
+        assertTrue("Incorrect number of events. Expected 0, actual " + events.size(), events.size()
== 0);
         app.clear();
     }
-
-    @Test
-    public void testReconfiguration() throws Exception {
-        final File file = new File("target/test-classes/" + CONFIG);
-        final long orig = file.lastModified();
-        final long newTime = orig + 10000;
-        file.setLastModified(newTime);
-        Thread.sleep(6000);
-        for (int i = 0; i < 17; ++i) {
-            logger.debug("Reconfigure");
-        }
-        final Configuration cfg = ctx.getConfiguration();
-        assertNotNull("No configuration", cfg);
-        assertTrue("Reconfiguration failed", cfg != config);
-    }
-
-    @Test
-    public void testAdditivity() throws Exception {
-        Logger localLogger = (Logger) LogManager.getLogger("org.apache.test");
-        localLogger.error("Test parent additivity");
-        final List<LogEvent> events = app.getEvents();
-        assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size()
== 1);
-    }
 }
 



Mime
View raw message