Lighthouse 1047 patch for 1.2.x #317

Closed
wants to merge 2 commits into
from
View
11 framework/src/play/Logger.java
@@ -15,6 +15,8 @@
import org.apache.log4j.PatternLayout;
import org.apache.log4j.Priority;
import org.apache.log4j.PropertyConfigurator;
+import org.apache.log4j.xml.DOMConfigurator;
+
import play.exceptions.PlayException;
/**
@@ -54,7 +56,9 @@
public static void init() {
String log4jPath = Play.configuration.getProperty("application.log.path", "/log4j.xml");
URL log4jConf = Logger.class.getResource(log4jPath);
+ boolean isXMLConfig = log4jPath.endsWith(".xml");
if (log4jConf == null) { // try again with the .properties
+ isXMLConfig = false;
log4jPath = Play.configuration.getProperty("application.log.path", "/log4j.properties");
log4jConf = Logger.class.getResource(log4jPath);
}
@@ -69,8 +73,11 @@ public static void init() {
// so it's probably a custom configuration file
configuredManually = true;
}
-
- PropertyConfigurator.configure(log4jConf);
+ if (isXMLConfig) {
+ DOMConfigurator.configure(log4jConf);
+ } else {
+ PropertyConfigurator.configure(log4jConf);
+ }
Logger.log4j = org.apache.log4j.Logger.getLogger("play");
// In test mode, append logs to test-result/application.log
if (Play.runingInTestMode()) {
View
103 framework/test-src/play/LoggerTest.java
@@ -0,0 +1,103 @@
+package play;
+/**
+ *
+ */
+
+
+import java.io.File;
+import java.util.Properties;
+
+
+
+import org.apache.log4j.Level;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Test the Logger class. At the moment only a few methods.
+ * @author niels
+ *
+ */
+public class LoggerTest {
+
+ private static final String APPLICATION_LOG_PATH_PROPERTYNAME = "application.log.path";
+
+// private static String applicationLogPath;
+
+ private static Properties playConfig;
+
+ private static File applicationPath;
+
+ private static String id;
+
+ private static org.apache.log4j.Logger log4j;
+
+ /**
+ * Safes the original configuration and log.
+ * @throws java.lang.Exception
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ playConfig = Play.configuration;
+ applicationPath = Play.applicationPath;
+ id = Play.id;
+ log4j = Logger.log4j;
+ }
+
+ /**
+ * Restore the original configuration and log.
+ * @throws java.lang.Exception
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ Play.configuration = playConfig;
+ Play.applicationPath = applicationPath;
+ Play.id = id ;
+ Logger.log4j = log4j;
+ if (Play.configuration != null) {
+ Logger.init();
+ }
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ Play.configuration = new Properties();
+ Play.applicationPath = new File(".");
+ Play.id="test";
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ }
+
+
+ /**
+ * Test method for {@link play.Logger#init()}.
+ */
+ @Test
+ public void testInitWithProperties() {
+ Play.configuration.put(APPLICATION_LOG_PATH_PROPERTYNAME, "/play/testlog4j.properties");
+ Logger.log4j=null;
+ Logger.init();
+ org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger("logtest.properties");
+ assertEquals(Level.ERROR, log4jLogger.getLevel());
+ }
+
+ /**
+ * Test method for {@link play.Logger#init()}.
+ */
+ @Test
+ public void testInitWithXML() {
+ Play.configuration.put(APPLICATION_LOG_PATH_PROPERTYNAME, "/play/testlog4j.xml");
+ Logger.log4j=null;
+ Logger.init();
+ org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger("logtest.xml");
+ assertEquals(Level.ERROR, log4jLogger.getLevel());
+ }
+}
View
9 framework/test-src/play/testlog4j.properties
@@ -0,0 +1,9 @@
+log4j.rootLogger=DEBUG, PropertiesConsoleAppender
+
+log4j.appender.PropertiesConsoleAppender=org.apache.log4j.ConsoleAppender
+log4j.appender.PropertiesConsoleAppender.layout=org.apache.log4j.PatternLayout
+log4j.appender.PropertiesConsoleAppender.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c: %m%n
+
+
+
+log4j.logger.logtest.properties=ERROR
View
21 framework/test-src/play/testlog4j.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
+
+ <appender name="XmlConsoleAppender" class="org.apache.log4j.ConsoleAppender">
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %c: %m%n" />
+ </layout>
+ </appender>
+
+ <logger name="logtest.xml">
+ <level value="ERROR"/>
+ </logger>
+
+ <root>
+ <priority value="DEBUG" />
+ <appender-ref ref="XmlConsoleAppender" />
+ </root>
+
+</log4j:configuration>