From 13b387e185274f87ee6ea3886f6d23779ab6536a Mon Sep 17 00:00:00 2001 From: NITHIN NAMBIAR Date: Fri, 17 Apr 2020 08:54:32 +0100 Subject: [PATCH] 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 7fadc254f7223cd970138e174564205627f36949) --- .../repo/admin/Log4JHierarchyInit.java | 77 +++++++------ .../repo/admin/Log4JHierarchyInitTest.java | 101 ++++++++++++------ .../resources/log4j/custom-log4j.properties | 5 + .../resources/log4j/log4j-test-context.xml | 1 + src/test/resources/log4j/log4j.properties | 3 +- 5 files changed, 122 insertions(+), 65 deletions(-) create mode 100644 src/test/resources/log4j/custom-log4j.properties diff --git a/src/main/java/org/alfresco/repo/admin/Log4JHierarchyInit.java b/src/main/java/org/alfresco/repo/admin/Log4JHierarchyInit.java index 7c1c7ef345..261f5ac346 100644 --- a/src/main/java/org/alfresco/repo/admin/Log4JHierarchyInit.java +++ b/src/main/java/org/alfresco/repo/admin/Log4JHierarchyInit.java @@ -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 . - * #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 . + * #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); } } } diff --git a/src/test/java/org/alfresco/repo/admin/Log4JHierarchyInitTest.java b/src/test/java/org/alfresco/repo/admin/Log4JHierarchyInitTest.java index 9623086203..a2321852cf 100644 --- a/src/test/java/org/alfresco/repo/admin/Log4JHierarchyInitTest.java +++ b/src/test/java/org/alfresco/repo/admin/Log4JHierarchyInitTest.java @@ -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 . - * #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 . + * #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(); + } } diff --git a/src/test/resources/log4j/custom-log4j.properties b/src/test/resources/log4j/custom-log4j.properties new file mode 100644 index 0000000000..b5023acad6 --- /dev/null +++ b/src/test/resources/log4j/custom-log4j.properties @@ -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 \ No newline at end of file diff --git a/src/test/resources/log4j/log4j-test-context.xml b/src/test/resources/log4j/log4j-test-context.xml index e753a4f07e..c521c3971b 100644 --- a/src/test/resources/log4j/log4j-test-context.xml +++ b/src/test/resources/log4j/log4j-test-context.xml @@ -8,6 +8,7 @@ + classpath*:log4j/custom-log4j.properties classpath*:log4j/log4j.properties diff --git a/src/test/resources/log4j/log4j.properties b/src/test/resources/log4j/log4j.properties index 943cb8f036..610d1e9e21 100644 --- a/src/test/resources/log4j/log4j.properties +++ b/src/test/resources/log4j/log4j.properties @@ -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 \ No newline at end of file +log4j.logger.org.alfresco.repo.admin.Log4JHierarchyInitTest=DEBUG \ No newline at end of file