ACS-3160 Replace Reload4j with Log4j2 (#1204)

* [ACS-3160] replace reload4j with log4j2

* [ACS-3160] refactor log4j version in pom.xml files

* ACS-3160 Add log4j2.properties file

* [ACS-3160] convert log4jHierarchyInit properties

* ACS-3222 Generate log4j2 rolling file in the Tomcat logs directory

* ACS-3160 Fix reconfiguration in Log4JHierarchyInit

* [ACS-3160] change loggers from log4j2 to slf4j (where possible)

* ACS-3160 Load a set of augmenting/overriding log4j2.properties files

* ACS-3160 Fix CheckRequiredClassesForLoggingConsoleUnitTest (adjust to log4j2)

* ACS-3160 Enable CheckRequiredClassesForLoggingConsoleUnitTest

* [ACS-3160] add appender util class

* [ACS-3160] fix removeAbstractAppenderFromLogger

* ACS-3160 Upgrade Log4j2 and Slf4j2 versions to the latest

* ACS-3160 Temporarily remove AOS for test purposes [tas][db]

* [ACS-3160] remove setting level from removeAbstractAppenderFromLogger

* ACS-3160 Remove references to SanitizingPatternLayout [tas]

* ACS-3892 Migrate ags-community-repo AMP to Log4j2

* ACS-3892 Reduce log4j-core scope to test

* ACS-3160 Update the aos-module version to 1.5.0-DEV-LOG4J2 [tas][db]

* [ACS-3924] upgrade greenmail version

* [ACS-3924] remove exclusion from greenmail

* ACS-3160 Fix NDC

* ACS-3160 Revert the NDC_REF

* ACS-3160 Test with googledrive with Log4j2

* ACS-3160 Log4j references cleanup

Co-authored-by: Domenico Sibilio <domenicosibilio@gmail.com>
Co-authored-by: Damian.Ujma@hyland.com <Damian.Ujma@hyland.com>
This commit is contained in:
Aleksandra Onych
2022-12-05 15:07:50 +01:00
committed by GitHub
parent 728e305a93
commit b9ccd79a73
31 changed files with 976 additions and 524 deletions

View File

@@ -46,10 +46,6 @@
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
@@ -222,8 +218,9 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>${dependency.log4j.version}</version>
</dependency>
<dependency>
<groupId>xerces</groupId>

View File

@@ -2,37 +2,45 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* Copyright (C) 2005 - 2022 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
* 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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
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.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.properties.PropertiesConfiguration;
import org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@@ -45,54 +53,51 @@ import org.springframework.core.io.support.ResourcePatternResolver;
* The actual implementation uses introspection to avoid any hard-coded references to Log4J classes. If Log4J is
* not present, this class will do nothing.
* <p>
* Alfresco modules can provide their own log4j.properties file, which augments/overrides the global log4j.properties
* Alfresco modules can provide their own log4j2.properties file, which augments/overrides the global log4j2.properties
* within the Alfresco webapp. Within the module's source tree, suppose you create:
* <pre>
* config/alfresco/module/{module.id}/log4j.properties
* config/alfresco/module/{module.id}/log4j2.properties
* </pre>
* At deployment time, this log4j.properties file will be placed in:
* At deployment time, this log4j2.properties file will be placed in:
* <pre>
* WEB-INF/classes/alfresco/module/{module.id}/log4j.properties
* WEB-INF/classes/alfresco/module/{module.id}/log4j2.properties
* </pre>
* Where {module.id} is whatever value is set within the AMP's module.properties file. For details, see: <a
* href='http://wiki.alfresco.com/wiki/Developing_an_Alfresco_Module'>Developing an Alfresco Module</a>
* <p>
* For example, if {module.id} is "org.alfresco.module.someModule", then within your source code you'll have:
*
*
* <pre>
* config / alfresco / module / org.alfresco.module.someModule / log4j.properties
* config / alfresco / module / org.alfresco.module.someModule / log4j2.properties
* </pre>
*
* <p>
* This would be deployed to:
* <pre>
* WEB - INF / classes / alfresco / module / org.alfresco.module.someModule / log4j.properties
* WEB - INF / classes / alfresco / module / org.alfresco.module.someModule / log4j2.properties
* </pre>
*/
public class Log4JHierarchyInit implements ApplicationContextAware
{
private static Log logger = LogFactory.getLog(Log4JHierarchyInit.class);
private List<String> extraLog4jUrls;
private static final Log LOGGER = LogFactory.getLog(Log4JHierarchyInit.class);
private final List<String> extraLog4jUrls;
private ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
public Log4JHierarchyInit()
{
extraLog4jUrls = new ArrayList<String>();
extraLog4jUrls = new ArrayList<>();
}
/**
* Loads a set of augmenting/overriding log4j.properties files from locations specified via an array of Srping URLS.
* Loads a set of augmenting/overriding log4j2.properties files from locations specified via an array of Spring URLS.
* <p>
* This function supports Spring's syntax for retrieving multiple class path resources with the same name,
* via the "classpath&#042;:" prefix. For details, see: {@link PathMatchingResourcePatternResolver}.
*/
public void setExtraLog4jUrls(List<String> urls)
{
for (String url : urls)
{
extraLog4jUrls.add(url);
}
extraLog4jUrls.addAll(urls);
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
this.resolver = applicationContext;
@@ -107,34 +112,50 @@ 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", Properties.class);
// Import using this method
Properties mainProperties = new Properties();
importMainLogSettings(mainProperties);
for (String url : extraLog4jUrls)
{
importLogSettings(url, mainProperties);
}
method.invoke(null, mainProperties);
PropertiesConfiguration propertiesConfiguration = new PropertiesConfigurationBuilder()
.setConfigurationSource(null)
.setRootProperties(mainProperties)
.setLoggerContext((LoggerContext) LogManager.getContext(false))
.build();
propertiesConfiguration.initialize();
((LoggerContext) LogManager.getContext(false)).reconfigure(propertiesConfiguration);
}
catch (ClassNotFoundException e)
catch (Throwable t)
{
// Log4J not present
return;
}
catch (NoSuchMethodException e)
{
throw new RuntimeException("Unable to find method 'configure' on class 'org.apache.log4j.PropertyConfigurator'");
}
catch(Throwable t)
{
if (logger.isDebugEnabled())
if (LOGGER.isDebugEnabled())
{
logger.debug("Failed to add extra Logger configuration: \n" + " Error: " + t.getMessage(), t);
LOGGER.debug("Failed to add extra Logger configuration: \n" + " Error: " + t.getMessage(), t);
}
}
}
private static void importMainLogSettings(Properties mainProperties) throws IOException
{
File file = ((LoggerContext) LogManager.getContext()).getConfiguration().getConfigurationSource().getFile();
if (file != null)
{
try (FileInputStream fis = new FileInputStream(file))
{
mainProperties.load(fis);
}
catch (FileNotFoundException e)
{
if (LOGGER.isDebugEnabled())
{
LOGGER.debug("Failed to find initial configuration: \n" + " Error: " + e.getMessage(), e);
}
}
}
}
private void importLogSettings(String springUrl, Properties mainProperties)
@@ -147,7 +168,7 @@ public class Log4JHierarchyInit implements ApplicationContextAware
}
catch (Exception e)
{
logger.warn("Failed to find additional Logger configuration: " + springUrl);
LOGGER.warn("Failed to find additional Logger configuration: " + springUrl);
}
// Read each resource
@@ -162,12 +183,13 @@ public class Log4JHierarchyInit implements ApplicationContextAware
}
catch (Throwable e)
{
if (logger.isDebugEnabled())
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

@@ -2,7 +2,7 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* Copyright (C) 2005 - 2022 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
@@ -34,13 +34,13 @@ import org.alfresco.query.PagingResults;
import org.alfresco.repo.admin.patch.AbstractPatch;
import org.alfresco.repo.calendar.CalendarModel;
import org.alfresco.service.cmr.calendar.CalendarEntry;
import org.alfresco.service.cmr.calendar.CalendarEntryDTO;
import org.alfresco.service.cmr.calendar.CalendarService;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService;
import org.apache.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.extensions.surf.util.I18NUtil;
/**
@@ -57,7 +57,7 @@ public class CalendarAllDayEventDatesCorrectingPatch extends AbstractPatch
{
private static final String MSG_SUCCESS = "patch.calendarAllDayEventDatesCorrectingPatch.result";
private static final Logger LOGGER = Logger.getLogger(CalendarAllDayEventDatesCorrectingPatch.class);
private static final Logger LOGGER = LoggerFactory.getLogger(CalendarAllDayEventDatesCorrectingPatch.class);
private int batchSize = 1000;

View File

@@ -1,20 +1,20 @@
/*
* #%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,
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2022 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.
@@ -35,7 +35,8 @@ import org.alfresco.repo.bulkimport.DirectoryAnalyser;
import org.alfresco.repo.bulkimport.FilesystemTracker;
import org.alfresco.repo.bulkimport.ImportableItem;
import org.alfresco.util.PropertyCheck;
import org.apache.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
@@ -44,7 +45,7 @@ import org.apache.log4j.Logger;
*/
public abstract class AbstractFilesystemTracker implements FilesystemTracker
{
protected static Logger logger = Logger.getLogger(FilesystemTracker.class);
protected static Logger logger = LoggerFactory.getLogger(FilesystemTracker.class);
protected DirectoryAnalyser directoryAnalyser = null;

View File

@@ -2,7 +2,7 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2020 Alfresco Software Limited
* Copyright (C) 2005 - 2022 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
@@ -37,7 +37,8 @@ import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.namespace.NamespaceException;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.apache.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract {@link EventFilter} implementation, containing common event filtering
@@ -47,7 +48,7 @@ import org.apache.log4j.Logger;
*/
public abstract class AbstractNodeEventFilter implements EventFilter<QName>
{
private static final Logger LOGGER = Logger.getLogger(AbstractNodeEventFilter.class);
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractNodeEventFilter.class);
private static final String MARKER_INCLUDE_SUBTYPES = "include_subtypes";
private static final String WILDCARD = "*";

View File

@@ -13,16 +13,16 @@
<!-- NOTE: value entries are listed from lowest precedence to highest. -->
<!-- Installed AMP modules -->
<value>classpath*:alfresco/module/*/log4j.properties</value>
<value>classpath*:alfresco/module/*/log4j2.properties</value>
<!-- Enterprise extensions -->
<value>classpath*:alfresco/enterprise/*-log4j.properties</value>
<value>classpath*:alfresco/enterprise/*-log4j2.properties</value>
<!-- Other installed extensions -->
<value>classpath*:alfresco/extension/*-log4j.properties</value>
<value>classpath*:alfresco/extension/*-log4j2.properties</value>
<!-- private developer overrides -->
<value>classpath*:alfresco/extension/dev-log4j.properties</value>
<value>classpath*:alfresco/extension/dev-log4j2.properties</value>
</list>
</property>
</bean>

View File

@@ -2,7 +2,7 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2021 Alfresco Software Limited
* Copyright (C) 2005 - 2022 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
@@ -47,8 +47,9 @@ import org.alfresco.util.TestHelper;
import org.alfresco.util.testing.category.DBTests;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.config.Configurator;
import org.junit.experimental.categories.Category;
import org.springframework.context.ApplicationContext;
@@ -389,8 +390,8 @@ public class JobLockServiceTest extends TestCase
public synchronized void testLockCallbackReleaseSelf() throws Exception
{
// ACE-4347 extra debug logging just for this test so we can see what's going on when it next fails
Level saveLogLevel = Logger.getLogger("org.alfresco.repo.lock").getLevel();
Logger.getLogger("org.alfresco.repo.lock").setLevel(Level.ALL);
Level saveLogLevel = LogManager.getLogger("org.alfresco.repo.lock").getLevel();
Configurator.setLevel(LogManager.getLogger("org.alfresco.repo.lock"), Level.ALL);
try
{
final QName lockQName = QName.createQName(NAMESPACE, getName());
@@ -444,7 +445,7 @@ public class JobLockServiceTest extends TestCase
}
finally
{
Logger.getLogger("org.alfresco.repo.lock").setLevel(saveLogLevel);
Configurator.setLevel(LogManager.getLogger("org.alfresco.repo.lock"), saveLogLevel);
}
}
@@ -520,8 +521,8 @@ public class JobLockServiceTest extends TestCase
public void runGetLockWithCallback(int t)
{
// ACE-4347 extra debug logging just for this test so we can see what's going on when it next fails
Level saveLogLevel = Logger.getLogger("org.alfresco.repo.lock").getLevel();
Logger.getLogger("org.alfresco.repo.lock").setLevel(Level.ALL);
Level saveLogLevel = LogManager.getLogger("org.alfresco.repo.lock").getLevel();
Configurator.setLevel(LogManager.getLogger("org.alfresco.repo.lock"), Level.ALL);
logger.debug("runGetLockWithCallback "+t+
"\n----------------------------------------"+
@@ -618,7 +619,7 @@ public class JobLockServiceTest extends TestCase
logger.debug("runGetLockWithCallback\n----------------------------------------");
Logger.getLogger("org.alfresco.repo.lock").setLevel(saveLogLevel);
Configurator.setLevel(LogManager.getLogger("org.alfresco.repo.lock"), saveLogLevel);
}
}

View File

@@ -2,7 +2,7 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2021 Alfresco Software Limited
* Copyright (C) 2005 - 2022 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
@@ -30,7 +30,7 @@ import org.junit.Test;
public class CheckRequiredClassesForLoggingConsoleUnitTest extends TestCase
{
private static final String CLASS_NAME = "org.apache.log4j.jmx.HierarchyDynamicMBean";
private static final String CLASS_NAME = "org.apache.logging.log4j.core.jmx.Server";
@Test
public void testRequiredClassIsOnAClasspath() throws ClassNotFoundException

View File

@@ -81,10 +81,12 @@ import org.alfresco.util.GUID;
import org.alfresco.util.TempFileProvider;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
@@ -107,6 +109,7 @@ import static org.alfresco.repo.rendition2.TestSynchronousTransformClient.TEST_F
import static org.alfresco.repo.rendition2.TestSynchronousTransformClient.TEST_LONG_RUNNING_MIME_TYPE;
import static org.alfresco.repo.rendition2.TestSynchronousTransformClient.TEST_LONG_RUNNING_PROPERTY_VALUE;
import static org.alfresco.repo.rendition2.TestSynchronousTransformClient.TEST_LONG_RUNNING_TRANSFORM_TIME;
import static org.alfresco.util.log4j.Log4jAppenderUtil.addAbstractAppenderToLogger;
/**
* Thumbnail service implementation unit test
@@ -709,19 +712,19 @@ public class ThumbnailServiceImplTest extends BaseAlfrescoSpringTest
*
* That is useful if you need to use the log output for your tests.
*/
private class LogErrorAppender extends AppenderSkeleton
@Plugin (name = "LogErrorAppender", category = "Core")
private class LogErrorAppender extends AbstractAppender
{
private final List<LoggingEvent> log = new ArrayList<LoggingEvent>();
private final List<LogEvent> log = new ArrayList<LogEvent>();
@Override
public boolean requiresLayout()
protected LogErrorAppender()
{
return false;
super("LogErrorAppender", null, null, false, null);
}
@Override
protected void append(final LoggingEvent loggingEvent)
public void append(final LogEvent loggingEvent)
{
if(loggingEvent.getLevel() == Level.ERROR)
{
@@ -729,14 +732,9 @@ public class ThumbnailServiceImplTest extends BaseAlfrescoSpringTest
}
}
@Override
public void close()
public List<LogEvent> getLog()
{
}
public List<LoggingEvent> getLog()
{
return new ArrayList<LoggingEvent>(log);
return new ArrayList<LogEvent>(log);
}
}
@@ -750,7 +748,9 @@ public class ThumbnailServiceImplTest extends BaseAlfrescoSpringTest
{
// Add the log appender to the root logger
LogErrorAppender logErrorAppender = new LogErrorAppender();
Logger.getRootLogger().addAppender(logErrorAppender);
Logger rootLogger = LogManager.getRootLogger();
addAbstractAppenderToLogger(logErrorAppender, rootLogger);
// create content node for thumbnail node
NodeRef pdfOrig = createOriginalContent(folder, MimetypeMap.MIMETYPE_PDF);

View File

@@ -40,8 +40,8 @@ import org.alfresco.transform.config.TransformOptionValue;
import org.alfresco.transform.config.Transformer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
@@ -217,7 +217,7 @@ public class LocalTransformServiceRegistryConfigTest extends TransformRegistryMo
initTestData();
super.setUp();
LogManager.getLogger(LocalTransformServiceRegistryConfigTest.class).setLevel(Level.DEBUG);
Configurator.setLevel(LocalTransformServiceRegistryConfigTest.class, Level.DEBUG);
}
@Override

View File

@@ -0,0 +1,65 @@
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2022 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.util.log4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.LoggerConfig;
/**
* A utility class to work with log4j2 Appenders in test mode.
*
* @author Aleksandra Onych
*/
public class Log4jAppenderUtil
{
public static void addAbstractAppenderToLogger(AbstractAppender appender, Logger logger)
{
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
appender.start();
LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
loggerConfig.addAppender(appender, null, null);
ctx.updateLoggers();
}
public static void removeAbstractAppenderFromLogger(AbstractAppender appender, Logger logger)
{
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
appender.stop();
LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
loggerConfig.removeAppender(appender.getName());
ctx.updateLoggers();
appender = null;
}
}

View File

@@ -1,5 +0,0 @@
## 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

@@ -0,0 +1,11 @@
## Test to see that Log4J additions are picked up
logger.alfresco-repo-model-filefolder-FileFolderPerformanceTester.name=org.alfresco.repo.model.filefolder.FileFolderPerformanceTester
logger.alfresco-repo-model-filefolder-FileFolderPerformanceTester.level=DEBUG
logger.alfresco-repo-model-filefolder-FileFolderPerformanceTester.appenderRef.stdout.ref=consoleAppender
logger.alfresco-repo-admin-Log4JHierarchyInitTest.name=org.alfresco.repo.admin.Log4JHierarchyInitTest
logger.alfresco-repo-admin-Log4JHierarchyInitTest.level=DEBUG
logger.alfresco-repo-admin-Log4JHierarchyInitTest.appenderRef.stdout.ref=consoleAppender
appender.console.type=Console
appender.console.name=ConsoleAppender
appender.console.layout.type=PatternLayout

View File

@@ -8,8 +8,8 @@
<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>
<value>classpath*:log4j/custom-log4j2.properties</value>
<value>classpath*:log4j/log4j2.properties</value>
</list>
</property>
</bean>

View File

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

View File

@@ -0,0 +1,3 @@
## Test to see that Log4J additions are picked up
logger.alfresco-repo-admin-Log4JHierarchyInitTest.name=org.alfresco.repo.admin.Log4JHierarchyInitTest
logger.alfresco-repo-admin-Log4JHierarchyInitTest.level=DEBUG