Fix/mnt 20916 log4j configuration hierarchy

* MNT-20916 Log4j configuration hierarchy

* MNT-20916 Log4j configuration hierarchy

* MNT-20916 Log4j configuration hierarchy

* MNT-20916 Log4j configuration hierarchy,code cleanup

* MNT-20916 Log4j configuration hierarchy,code cleanup

* MNT-20916 Log4j configuration hierarchy,code cleanup

(cherry picked from commit 7fadc254f7)
This commit is contained in:
NITHIN NAMBIAR
2020-04-17 08:54:32 +01:00
committed by Nithin Nambiar
parent eafc654736
commit 13b387e185
5 changed files with 122 additions and 65 deletions

View File

@@ -25,11 +25,12 @@
*/
package org.alfresco.repo.admin;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
@@ -106,14 +107,16 @@ public class Log4JHierarchyInit implements ApplicationContextAware
{
try
{
Properties mainProperties=new Properties();
// Get the PropertyConfigurator
Class<?> clazz = Class.forName("org.apache.log4j.PropertyConfigurator");
Method method = clazz.getMethod("configure", URL.class);
Method method = clazz.getMethod("configure", Properties.class);
// Import using this method
for (String url : extraLog4jUrls)
{
importLogSettings(method, url);
importLogSettings(url, mainProperties);
}
method.invoke(null, mainProperties);
}
catch (ClassNotFoundException e)
{
@@ -124,10 +127,17 @@ public class Log4JHierarchyInit implements ApplicationContextAware
{
throw new RuntimeException("Unable to find method 'configure' on class 'org.apache.log4j.PropertyConfigurator'");
}
catch(Throwable t)
{
if (logger.isDebugEnabled())
{
logger.debug("Failed to add extra Logger configuration: \n" + " Error: " + t.getMessage(), t);
}
}
}
private void importLogSettings(Method method, String springUrl)
private void importLogSettings(String springUrl, Properties mainProperties)
{
Resource[] resources = null;
@@ -145,14 +155,17 @@ public class Log4JHierarchyInit implements ApplicationContextAware
{
try
{
URL url = resource.getURL();
method.invoke(null, url);
InputStream inputStream = resource.getInputStream();
Properties properties = new Properties();
properties.load(inputStream);
mainProperties.putAll(properties);
}
catch (Throwable e)
{
if (logger.isDebugEnabled())
{
logger.debug("Failed to add extra Logger configuration: \n" + " URL: " + springUrl + "\n" + " Error: " + e.getMessage(), e);
logger.debug("Failed to add extra Logger configuration: \n" + " URL: " + springUrl + "\n"
+ " Error: " + e.getMessage(), e);
}
}
}

View File

@@ -23,26 +23,35 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.repo.admin;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.StringContains.containsString;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import junit.framework.TestCase;
import org.alfresco.repo.model.filefolder.FileFolderPerformanceTester;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @see Log4JHierarchyInit
*
* @author Derek Hulley
* @see Log4JHierarchyInit
* @since 2.2.3
*/
public class Log4JHierarchyInitTest extends TestCase
{
private PrintStream sysErr;
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private static ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] {"classpath:log4j/log4j-test-context.xml"}
);
new String[] { "classpath:log4j/log4j-test-context.xml" });
public void setUp() throws Exception
{
@@ -63,4 +72,34 @@ public class Log4JHierarchyInitTest extends TestCase
// We expect DEBUG to be on
assertTrue("DEBUG was not enabled for logger " + this.getClass(), log.isDebugEnabled());
}
public void setUpStreams() throws UnsupportedEncodingException
{
sysErr = System.err;
System.setErr(new PrintStream(errContent, false, "UTF-8"));
}
public void revertStreams()
{
System.setErr(sysErr);
}
public void testLog4jAppenderClosedError() throws Throwable
{
setUpStreams();
Log log = LogFactory.getLog(this.getClass());
Log4JHierarchyInit log4JHierarchyInit = (Log4JHierarchyInit) ctx.getBean("log4JHierarchyInit");
Log log2 = LogFactory.getLog(FileFolderPerformanceTester.class);
log4JHierarchyInit.init();
log2.info("test");
// We expect DEBUG to be on
assertTrue("DEBUG was not enabled for logger " + this.getClass(), log.isDebugEnabled());
assertFalse(errContent.toString().contains("Attempted to append to closed appender named"));
revertStreams();
}
}

View File

@@ -0,0 +1,5 @@
## Test to see that Log4J additions are picked up
log4j.logger.org.alfresco.repo.model.filefolder.FileFolderPerformanceTester=DEBUG, consoleAppender
log4j.logger.org.alfresco.repo.admin.Log4JHierarchyInitTest=DEBUG, consoleAppender
log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout

View File

@@ -8,6 +8,7 @@
<property name="extraLog4jUrls">
<list>
<!-- Uses 'test-resources' folder, which is on the classpath -->
<value>classpath*:log4j/custom-log4j.properties</value>
<value>classpath*:log4j/log4j.properties</value>
</list>
</property>

View File

@@ -1,3 +1,2 @@
## Test to see that Log4J additions are picked up
log4j.logger.org.alfresco.repo.admin.Log4JHierarchyInitTest=DEBUG
log4j.logger.org.alfresco.repo.model.filefolder.FileFolderPerformanceTester=DEBUG