Author: echatellier Date: 2014-07-20 22:25:48 +0200 (Sun, 20 Jul 2014) New Revision: 4048 Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/4048 Log: Switch to log4j2 (testing) Added: branches/4.3.0-log4j2/ Modified: branches/4.3.0-log4j2/pom.xml branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/IsisConfig.java branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/IsisFish.java branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/datastore/SimulationStorage.java branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/entities/StrategyMonthInfoImpl.java branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/logging/SimulationLoggerUtil.java branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/logging/ThreadFilter.java branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/simulator/launcher/InProcessSimulatorLauncher.java branches/4.3.0-log4j2/src/test/java/fr/ifremer/isisfish/logging/SimulationLoggerUtilTest.java Property changes on: branches/4.3.0-log4j2 ___________________________________________________________________ Added: svn:ignore + maven.log target velocity.log .classpath .project isis-fish.ipr isis-fish.iws isis-fish.iml .settings bin nbproject Inputs_Langoustine .idea Added: svn:mergeinfo + /branches/4.0.0-svnkit:3665-3698 /branches/4.0.1:3616-3968 /branches/4.1:3601-3670 Modified: branches/4.3.0-log4j2/pom.xml =================================================================== --- trunk/pom.xml 2014-07-19 13:52:09 UTC (rev 4047) +++ branches/4.3.0-log4j2/pom.xml 2014-07-20 20:25:48 UTC (rev 4048) @@ -213,29 +213,37 @@ </dependency> <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>1.7.7</version> + <scope>runtime</scope> + </dependency> + + <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> + <version>1.2</version> </dependency> <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <version>1.2.17</version> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>2.0</version> <scope>compile</scope> </dependency> <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-api</artifactId> - <version>1.7.7</version> - <scope>runtime</scope> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-jcl</artifactId> + <version>2.0</version> + <scope>runtime</scope> </dependency> <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <version>1.7.7</version> - <scope>runtime</scope> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j-impl</artifactId> + <version>2.0</version> + <scope>runtime</scope> </dependency> <dependency> Modified: branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/IsisConfig.java =================================================================== --- trunk/src/main/java/fr/ifremer/isisfish/IsisConfig.java 2014-07-19 13:52:09 UTC (rev 4047) +++ branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/IsisConfig.java 2014-07-20 20:25:48 UTC (rev 4048) @@ -1000,10 +1000,10 @@ } /** - * Seuil d'uitilisation des matrices creuse + * Seuil d'utilisation des matrices creuse * * @return - * @since 4.2.0.2 + * @since 4.3.1.0 */ public int getSimulationMatrixThresholdUseSparse() { ApplicationConfig config = SimulationContext.get().getConfig(); @@ -1012,7 +1012,7 @@ } /** - * Seuil d'uitilisation des matrices creuse + * Seuil d'utilisation des matrices creuse * * @return * @since 4.3.1.0 @@ -1028,7 +1028,7 @@ * de temps. * * @return - * @since 4.2.0.2 + * @since 4.3.1.0 */ public int getSimulationStoreResultOnDisk() { ApplicationConfig config = SimulationContext.get().getConfig(); @@ -1040,7 +1040,7 @@ * Indique le nombre de pas qui doivent rester en memoire durant la simulation * * @return - * @since 4.2.0.2 + * @since 4.3.1.0 */ public int getSimulationStoreResultCacheStep() { ApplicationConfig config = SimulationContext.get().getConfig(); Modified: branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/IsisFish.java =================================================================== --- trunk/src/main/java/fr/ifremer/isisfish/IsisFish.java 2014-07-19 13:52:09 UTC (rev 4047) +++ branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/IsisFish.java 2014-07-20 20:25:48 UTC (rev 4048) @@ -121,6 +121,9 @@ public static void main(String... args) throws Exception { + // TODO as of 20140720, hibernate fails to detect log4j 2.0, force slf4j + System.setProperty("org.jboss.logging.provider", "slf4j"); + // permet de faire fonctionner la compilation en webstart System.setSecurityManager(null); Modified: branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/datastore/SimulationStorage.java =================================================================== --- trunk/src/main/java/fr/ifremer/isisfish/datastore/SimulationStorage.java 2014-07-19 13:52:09 UTC (rev 4047) +++ branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/datastore/SimulationStorage.java 2014-07-20 20:25:48 UTC (rev 4048) @@ -41,6 +41,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.logging.log4j.ThreadContext; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; import org.nuiton.util.FileUtil; @@ -880,7 +881,7 @@ public void addSimulationLogger(String simulLogLevel, String scriptLogLevel, String libLogLevel, String threadName) { - try { + /*try { SimulationLoggerUtil.addSimulationAppender(getSimulationLogFile(), getAppenderId(), simulLogLevel, scriptLogLevel, libLogLevel, threadName); @@ -889,7 +890,13 @@ useLog = false; log.warn(t("Can't create simulation logger", eee)); // we do not throw any exception, this is a shame but not required - } + }*/ + + ThreadContext.put("contextId", name); + ThreadContext.put("contextLogFile", getSimulationLogFile()); + ThreadContext.put("contextSimulLevel", simulLogLevel); + ThreadContext.put("contextScriptLevel", scriptLogLevel); + ThreadContext.put("contextLibLevel", libLogLevel); } /** @@ -897,12 +904,12 @@ */ public void removeSimulationLogger() { - try { + /*try { String appenderId = getAppenderId(); SimulationLoggerUtil.removeSimulationAppender(appenderId); } finally { useLog = false; - } + }*/ } /** Modified: branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/entities/StrategyMonthInfoImpl.java =================================================================== --- trunk/src/main/java/fr/ifremer/isisfish/entities/StrategyMonthInfoImpl.java 2014-07-19 13:52:09 UTC (rev 4047) +++ branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/entities/StrategyMonthInfoImpl.java 2014-07-20 20:25:48 UTC (rev 4048) @@ -74,7 +74,7 @@ public List<Metier> getMetierWithProportion() { MatrixND mat = super.getProportionMetier(); List<Metier> result = new ArrayList<Metier>(mat.getDim(0)); - for (MatrixIterator i=mat.iteratorNotZero(); i.next();) { + for (MatrixIterator i = mat.iteratorNotZero(); i.next();) { result.add((Metier)i.getSemanticsCoordinates()[0]); } return result; Modified: branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/logging/SimulationLoggerUtil.java =================================================================== --- trunk/src/main/java/fr/ifremer/isisfish/logging/SimulationLoggerUtil.java 2014-07-19 13:52:09 UTC (rev 4047) +++ branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/logging/SimulationLoggerUtil.java 2014-07-20 20:25:48 UTC (rev 4048) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2002 - 2010 Ifremer, Code Lutin, Benjamin Poussin, Tony Chemit + * Copyright (C) 2002 - 2014 Ifremer, Code Lutin, Benjamin Poussin, Tony Chemit * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as @@ -25,17 +25,6 @@ package fr.ifremer.isisfish.logging; -import fr.ifremer.isisfish.IsisFish; -import fr.ifremer.isisfish.IsisFishException; -import fr.ifremer.isisfish.datastore.SimulationStorage; -import fr.ifremer.isisfish.logging.console.LogConsole; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.log4j.Appender; -import org.apache.log4j.FileAppender; -import org.apache.log4j.Level; -import org.apache.log4j.Logger; -import org.apache.log4j.PatternLayout; import static org.nuiton.i18n.I18n.t; import java.io.File; @@ -44,6 +33,23 @@ import java.util.HashMap; import java.util.Map; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.FileAppender; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.DefaultConfiguration; +import org.apache.logging.log4j.core.layout.PatternLayout; + +import fr.ifremer.isisfish.IsisFish; +import fr.ifremer.isisfish.IsisFishException; +import fr.ifremer.isisfish.datastore.SimulationStorage; +import fr.ifremer.isisfish.logging.console.LogConsole; + /** * FIXME poussin, a priori cette classe ne supporte pas plusieurs simulation * en meme temps, ce qui est le cas si on a plusieurs processeurs de dispo :( @@ -115,8 +121,9 @@ String libLogLevel, String threadName) throws IsisFishException { - PatternLayout layout = new PatternLayout(); - layout.setConversionPattern(LOG_PATTERN); + PatternLayout layout = PatternLayout.newBuilder() + .withPattern(LOG_PATTERN) + .build(); ThreadFilter filter = new ThreadFilter(threadName); @@ -124,25 +131,43 @@ scriptLevelKeeper = prepareLogger(scriptLogLevel, scriptLoggerToChange); libLevelKeeper = prepareLogger(libLogLevel, libLoggerToChange); - //TODO See if we use should use the buffered version : define buffer size + Configuration configuration = new DefaultConfiguration(); + //try { + //FileAppender appender = new FileAppender(layout, filename, false); + FileAppender appender = FileAppender.createAppender(filename, + "false" /*append*/, + "false" /*locking*/, + threadName /*name*/, + "true" /*immediateFlush*/, + "true" /*ignore*/, + "true" /*bufferedIo*/, + "8192" /*bufferSizeStr*/, + layout, + filter, + "false" /*advertise*/, + threadName /*advertiseUri*/, + configuration); + + //appender.setName(appenderId); - try { - FileAppender appender = new FileAppender(layout, filename, false); - appender.setName(appenderId); - // thread filter come first - appender.addFilter(filter); + //appender.addFilter(filter); // add the appender to the root appender - Logger.getRootLogger().addAppender(appender); + //Logger.getRootLogger().addAppender(appender); + configuration.addAppender(appender); + LoggerContext context = (LoggerContext) LogManager.getContext(); + context.start(configuration); + context.updateLoggers(); + if (log.isDebugEnabled()) { log.debug(t("isisfish.log.addAppender", appenderId)); } - } catch (IOException ex) { + /*} catch (IOException ex) { log.error(t("isisfish.error.log.createAppender", appenderId, ex.getMessage())); throw new IsisFishException("Can't add appender", ex); - } + }*/ } @@ -203,7 +228,7 @@ */ static Map<String, Level> prepareLogger(String logLevel, String[] categoriesToChange) { Map<String, Level> result = new HashMap<String, Level>(); - Enumeration enumeration; + /*Enumeration enumeration; Level level = Level.toLevel(logLevel); @@ -226,7 +251,7 @@ } } } - } + }*/ return result; } @@ -236,7 +261,7 @@ * @param levelKeeper the dico of categories to swap back to original levels */ static void retablishLogger(Map<String, Level> levelKeeper) { - Logger rootLogger = Logger.getRootLogger(); + /*Logger rootLogger = Logger.getRootLogger(); for (Map.Entry<String, Level> entry : levelKeeper.entrySet()) { Logger logger = rootLogger.getLoggerRepository().getLogger(entry.getKey()); if (logger != null) { @@ -247,7 +272,7 @@ logger.setLevel(oldLevel); } } - levelKeeper.clear(); + levelKeeper.clear();*/ } /** @@ -260,7 +285,7 @@ */ static void removeAppender(String category, String name) { // get logger for the category - Logger logger = category == null ? Logger.getRootLogger() : Logger.getLogger(category); + /*Logger logger = category == null ? Logger.getRootLogger() : Logger.getLogger(category); if (logger == null) { Logger.getRootLogger().warn(t("isisfish.error.log.closeAppender", name, category)); return; @@ -275,6 +300,6 @@ // close appender app.close(); // and remove it from the logger - logger.removeAppender(app); + logger.removeAppender(app);*/ } } Modified: branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/logging/ThreadFilter.java =================================================================== --- trunk/src/main/java/fr/ifremer/isisfish/logging/ThreadFilter.java 2014-07-19 13:52:09 UTC (rev 4047) +++ branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/logging/ThreadFilter.java 2014-07-20 20:25:48 UTC (rev 4048) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2002 - 2010 Ifremer, Code Lutin, Benjamin Poussin, Tony Chemit + * Copyright (C) 2002 - 2014 Ifremer, Code Lutin, Benjamin Poussin, Tony Chemit * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as @@ -25,27 +25,45 @@ package fr.ifremer.isisfish.logging; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.filter.AbstractFilter; +import org.apache.logging.log4j.message.Message; + /** * this class is Log4J filter to accept only incoming log events from a unique thread. * it retains all logEvent coming from other threads (compare with given thread name). * * @author chemit */ -public class ThreadFilter extends org.apache.log4j.spi.Filter { +public class ThreadFilter extends AbstractFilter { /** the name of the only thread to accept */ protected String threadName; public ThreadFilter(String threadName) { - this.threadName=threadName; + this.threadName = threadName; } - public int decide(org.apache.log4j.spi.LoggingEvent event) { - // only accept event coming from a specific thread - return event.getThreadName().equals(threadName) ? ACCEPT : DENY; + @Override + public Result filter(LogEvent event) { + return event.getThreadName().equals(threadName) ? Result.ACCEPT : Result.DENY; } - - public void setThreadName(String threadName) { - this.threadName = threadName; + + @Override + public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) { + throw new UnsupportedOperationException("filter"); } + + @Override + public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) { + throw new UnsupportedOperationException("filter"); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, String msg, Object... params) { + throw new UnsupportedOperationException("filter"); + } } Modified: branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/simulator/launcher/InProcessSimulatorLauncher.java =================================================================== --- trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/InProcessSimulatorLauncher.java 2014-07-19 13:52:09 UTC (rev 4047) +++ branches/4.3.0-log4j2/src/main/java/fr/ifremer/isisfish/simulator/launcher/InProcessSimulatorLauncher.java 2014-07-20 20:25:48 UTC (rev 4048) @@ -227,14 +227,14 @@ String simulLogLevel = simulation.getParameter().getSimulLogLevel(); String scriptLogLevel = simulation.getParameter().getScriptLogLevel(); String libLogLevel = simulation.getParameter().getLibLogLevel(); - try { + /*try { simulation.addSimulationLogger(simulLogLevel, scriptLogLevel, libLogLevel, simThread.getName()); } catch (Exception e) { if (log.isWarnEnabled()) { log.warn(t("isisfish.error.add.logger.simulation", e)); } - } + }*/ try { simThread.start(); try { @@ -263,6 +263,13 @@ @Override public void run() { + + String simulLogLevel = simulation.getParameter().getSimulLogLevel(); + String scriptLogLevel = simulation.getParameter().getScriptLogLevel(); + String libLogLevel = simulation.getParameter().getLibLogLevel(); + simulation.addSimulationLogger(simulLogLevel, scriptLogLevel, + libLogLevel, getName()); + simulation = localSimulateSameThread(control, simulation); } } Modified: branches/4.3.0-log4j2/src/test/java/fr/ifremer/isisfish/logging/SimulationLoggerUtilTest.java =================================================================== --- trunk/src/test/java/fr/ifremer/isisfish/logging/SimulationLoggerUtilTest.java 2014-07-19 13:52:09 UTC (rev 4047) +++ branches/4.3.0-log4j2/src/test/java/fr/ifremer/isisfish/logging/SimulationLoggerUtilTest.java 2014-07-20 20:25:48 UTC (rev 4048) @@ -32,8 +32,9 @@ import org.apache.commons.io.FileUtils; 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.Logger; import org.junit.Assert; import org.junit.Test; @@ -63,7 +64,7 @@ String threadName = Thread.currentThread().getName(); // use a logger we know to be scan as been keep for original level - Logger hibernatelog = Logger.getLogger("org.hibernate"); + Logger hibernatelog = LogManager.getLogger("org.hibernate"); Level hibernateLogLevel = hibernatelog.getLevel(); @@ -82,7 +83,7 @@ ); // check hibernate level was not changed - Assert.assertEquals(hibernateLogLevel, Logger.getLogger("org.hibernate").getLevel()); + Assert.assertEquals(hibernateLogLevel, LogManager.getLogger("org.hibernate").getLevel()); final Log logger = LogFactory.getLog(appenderId); @@ -168,7 +169,7 @@ String threadName = Thread.currentThread().getName(); // use a logger we know to be scan as been keep for original level - Logger hibernatelog = Logger.getLogger("org.hibernate"); + Logger hibernatelog = LogManager.getLogger("org.hibernate"); Level hibernateLogLevel = hibernatelog.getLevel(); @@ -185,7 +186,7 @@ ); // check hibernate level was not changed - Assert.assertEquals(hibernateLogLevel, Logger.getLogger("org.hibernate").getLevel()); + Assert.assertEquals(hibernateLogLevel, LogManager.getLogger("org.hibernate").getLevel()); final Log logger = LogFactory.getLog(appenderId);