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

@@ -1,35 +1,36 @@
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
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

@@ -1,53 +1,62 @@
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* 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
{
}
public void testSetUp() throws Throwable
{
// Check that the bean is present
@@ -56,11 +65,41 @@ public class Log4JHierarchyInitTest extends TestCase
Log log = LogFactory.getLog("log4j.logger.org.alfresco");
assertFalse("Expect log level ERROR for 'org.alfresco'.", log.isWarnEnabled());
}
public void testAddingLog4jProperties() throws Throwable
{
Log log = LogFactory.getLog(this.getClass());
// 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
log4j.logger.org.alfresco.repo.admin.Log4JHierarchyInitTest=DEBUG