diff --git a/pom.xml b/pom.xml index 798aa71b10c..7d0fe25598b 100644 --- a/pom.xml +++ b/pom.xml @@ -68,18 +68,13 @@ jsap 2.1 + - - org.apache.logging.log4j - log4j-api - 2.14.0 + org.slf4j + slf4j-api + 1.7.30 - - org.apache.logging.log4j - log4j-to-slf4j - 2.14.0 - - + commons-io commons-io 2.8.0 diff --git a/spoon-pom/pom.xml b/spoon-pom/pom.xml index 6dca595b0a6..7d746edb3c9 100644 --- a/spoon-pom/pom.xml +++ b/spoon-pom/pom.xml @@ -34,7 +34,7 @@ - + org.junit.vintage @@ -102,24 +102,6 @@ clean install - - - - src/main/resources - true - - log4j.properties - - - - src/main/resources - false - - log4j.properties - - - - org.apache.maven.plugins diff --git a/src/main/java/spoon/Launcher.java b/src/main/java/spoon/Launcher.java index ff80ff00928..2f4043114b3 100644 --- a/src/main/java/spoon/Launcher.java +++ b/src/main/java/spoon/Launcher.java @@ -17,9 +17,9 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.IOFileFilter; import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.event.Level; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import spoon.SpoonModelBuilder.InputType; import spoon.compiler.Environment; import spoon.compiler.SpoonResource; @@ -46,6 +46,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.lang.invoke.MethodHandles; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; @@ -540,7 +541,7 @@ protected void processArguments() { protected void reportClassPathMode() { String cpmode = jsapActualArgs.getString("cpmode").toUpperCase(); - Launcher.LOGGER.info("Running in " + cpmode + " mode (doc: http://spoon.gforge.inria.fr/launcher.html)."); + factory.getEnvironment().report(null, Level.INFO, "Running in " + cpmode + " mode (doc: http://spoon.gforge.inria.fr/launcher.html)."); } /** @@ -591,7 +592,7 @@ protected JSAPResult parseArgs() { /** * A default logger to be used by Spoon. */ - public static final Logger LOGGER = LogManager.getLogger(); + public static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); /** * Creates a new Spoon Java compiler in order to process and compile Java diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index f0319123b50..70cdb1a7c24 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -7,11 +7,12 @@ */ package spoon; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import spoon.support.compiler.SpoonPom; import java.io.File; +import java.lang.invoke.MethodHandles; import java.util.List; import java.util.regex.Pattern; @@ -23,7 +24,7 @@ public class MavenLauncher extends Launcher { private SOURCE_TYPE sourceType; private SpoonPom model; private boolean forceRefresh = false; - private static final Logger LOGGER = LogManager.getLogger(); + private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); /** * @return SpoonPom corresponding to the pom file used by the launcher diff --git a/src/main/java/spoon/compiler/Environment.java b/src/main/java/spoon/compiler/Environment.java index af4bd2ac410..169d455f73b 100644 --- a/src/main/java/spoon/compiler/Environment.java +++ b/src/main/java/spoon/compiler/Environment.java @@ -7,7 +7,7 @@ */ package spoon.compiler; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import spoon.OutputType; import spoon.compiler.builder.EncodingProvider; import spoon.processing.FileGenerator; diff --git a/src/main/java/spoon/processing/AbstractProcessor.java b/src/main/java/spoon/processing/AbstractProcessor.java index 26823ed7b75..684e03bbfa0 100644 --- a/src/main/java/spoon/processing/AbstractProcessor.java +++ b/src/main/java/spoon/processing/AbstractProcessor.java @@ -7,7 +7,7 @@ */ package spoon.processing; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import spoon.Launcher; import spoon.compiler.Environment; import spoon.reflect.declaration.CtElement; diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index 1f3e2478186..c820ba06330 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -7,8 +7,8 @@ */ package spoon.reflect.visitor; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import spoon.SpoonException; import spoon.compiler.Environment; import spoon.experimental.CtUnresolvedImport; @@ -120,6 +120,7 @@ import spoon.support.util.ModelList; import java.lang.annotation.Annotation; +import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -244,7 +245,7 @@ public DefaultJavaPrettyPrinter setLineSeparator(String lineSeparator) { } - protected static final Logger LOGGER = LogManager.getLogger(); + protected static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); public static final String ERROR_MESSAGE_TO_STRING = "Error in printing the node. One parent isn't initialized!"; /** * Prints an element. This method shall be called by the toString() method of an element. diff --git a/src/main/java/spoon/reflect/visitor/ModelConsistencyChecker.java b/src/main/java/spoon/reflect/visitor/ModelConsistencyChecker.java index e873adfdfe9..4c322256dfc 100644 --- a/src/main/java/spoon/reflect/visitor/ModelConsistencyChecker.java +++ b/src/main/java/spoon/reflect/visitor/ModelConsistencyChecker.java @@ -7,7 +7,7 @@ */ package spoon.reflect.visitor; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import spoon.compiler.Environment; import spoon.reflect.declaration.CtElement; import spoon.reflect.declaration.CtNamedElement; diff --git a/src/main/java/spoon/support/RuntimeProcessingManager.java b/src/main/java/spoon/support/RuntimeProcessingManager.java index beaaf73f23f..ef66617d1df 100644 --- a/src/main/java/spoon/support/RuntimeProcessingManager.java +++ b/src/main/java/spoon/support/RuntimeProcessingManager.java @@ -7,7 +7,7 @@ */ package spoon.support; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import spoon.processing.ProcessInterruption; import spoon.processing.ProcessingManager; import spoon.processing.Processor; diff --git a/src/main/java/spoon/support/StandardEnvironment.java b/src/main/java/spoon/support/StandardEnvironment.java index fdc5a1502b3..2a0fd376065 100644 --- a/src/main/java/spoon/support/StandardEnvironment.java +++ b/src/main/java/spoon/support/StandardEnvironment.java @@ -8,8 +8,8 @@ package spoon.support; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Logger; +import org.slf4j.event.Level; +import org.slf4j.Logger; import spoon.Launcher; import spoon.OutputType; import spoon.SpoonException; @@ -98,7 +98,7 @@ public void setPrettyPrintingMode(PRETTY_PRINTING_MODE prettyPrintingMode) { private transient Logger logger = Launcher.LOGGER; - private Level level = Level.OFF; + private Level level = Level.ERROR; private boolean shouldCompile = false; @@ -193,7 +193,13 @@ private Level toLevel(String level) { if (level == null || level.isEmpty()) { throw new SpoonException("Wrong level given at Spoon."); } - return Level.toLevel(level, Level.ALL); + Level levelEnum; + try { + levelEnum = Level.valueOf(level); + } catch (IllegalArgumentException e) { + return Level.TRACE; + } + return levelEnum; } @Override @@ -273,8 +279,18 @@ public void report(Processor processor, Level level, String message) { } private void print(String message, Level messageLevel) { - if (messageLevel.isMoreSpecificThan(this.level)) { - logger.log(messageLevel, message); + if (messageLevel.toInt() >= this.level.toInt()) { + switch (messageLevel) { + case ERROR: logger.error(message); + break; + case WARN: logger.warn(message); + break; + case INFO: logger.info(message); + break; + case DEBUG: logger.debug(message); + break; + case TRACE: logger.trace(message); + } } } diff --git a/src/main/java/spoon/support/compiler/SpoonPom.java b/src/main/java/spoon/support/compiler/SpoonPom.java index b4a7307826a..bd3510acb21 100644 --- a/src/main/java/spoon/support/compiler/SpoonPom.java +++ b/src/main/java/spoon/support/compiler/SpoonPom.java @@ -7,7 +7,7 @@ */ package spoon.support.compiler; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; import org.apache.maven.model.Build; import org.apache.maven.model.BuildBase; import org.apache.maven.model.Model; diff --git a/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java b/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java index e92bb0afe17..51c684efe41 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java @@ -8,7 +8,7 @@ package spoon.support.compiler.jdt; import org.apache.commons.io.IOUtils; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import org.eclipse.jdt.core.compiler.CategorizedProblem; import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.internal.compiler.ASTVisitor; diff --git a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java index 2cbcf8f5e8f..a29cede54b6 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java @@ -7,8 +7,8 @@ */ package spoon.support.compiler.jdt; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; import spoon.SpoonException; @@ -61,6 +61,7 @@ import java.io.Reader; import java.io.StringReader; import java.lang.annotation.Annotation; +import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -71,7 +72,7 @@ */ public class JDTCommentBuilder { - private static final Logger LOGGER = LogManager.getLogger(); + private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private final CompilationUnitDeclaration declarationUnit; private CompilationUnit spoonUnit; diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java index 60175548659..819265d192a 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java @@ -7,8 +7,7 @@ */ package spoon.support.compiler.jdt; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.compiler.ASTVisitor; import org.eclipse.jdt.internal.compiler.ast.AND_AND_Expression; @@ -113,6 +112,7 @@ import org.eclipse.jdt.internal.compiler.lookup.Scope; import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; import org.eclipse.jdt.internal.compiler.lookup.VariableBinding; +import org.slf4j.LoggerFactory; import spoon.SpoonException; import spoon.reflect.code.BinaryOperatorKind; import spoon.reflect.code.CtArrayAccess; @@ -159,6 +159,8 @@ import spoon.support.compiler.jdt.ContextBuilder.CastInfo; import spoon.support.reflect.CtExtendedModifier; +import java.lang.invoke.MethodHandles; + import static spoon.support.compiler.jdt.JDTTreeBuilderQuery.getBinaryOperatorKind; import static spoon.support.compiler.jdt.JDTTreeBuilderQuery.getModifiers; import static spoon.support.compiler.jdt.JDTTreeBuilderQuery.getUnaryOperator; @@ -187,7 +189,7 @@ public static Logger getLogger() { return LOGGER; } - private static final Logger LOGGER = LogManager.getLogger(); + private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); public PositionBuilder getPositionBuilder() { return position; diff --git a/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java b/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java index c3a215ee5a2..6ad66527913 100644 --- a/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java +++ b/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java @@ -7,8 +7,8 @@ */ package spoon.support.reflect.declaration; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import spoon.reflect.ModelElementContainerDefaultCapacities; import spoon.reflect.annotations.MetamodelPropertyField; import spoon.reflect.code.CtBlock; @@ -57,6 +57,7 @@ import java.io.Serializable; import java.lang.annotation.Annotation; +import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -74,7 +75,7 @@ */ public abstract class CtElementImpl implements CtElement, Serializable { private static final long serialVersionUID = 1L; - protected static final Logger LOGGER = LogManager.getLogger(); + protected static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); public static final String ERROR_MESSAGE_TO_STRING = "Error in printing the node. One parent isn't initialized!"; private static final Factory DEFAULT_FACTORY = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment()); diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties deleted file mode 100644 index 56a819d9565..00000000000 --- a/src/main/resources/log4j.properties +++ /dev/null @@ -1,16 +0,0 @@ -log4j.rootLogger=info, stdout -log4j.logger.spoon=debug, R - -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout - -log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %p %c - %m%n - -log4j.appender.R=org.apache.log4j.RollingFileAppender -log4j.appender.R.File=${spoon.log.path} - -log4j.appender.R.MaxFileSize=1000KB -log4j.appender.R.MaxBackupIndex=3 - -log4j.appender.R.layout=org.apache.log4j.PatternLayout -log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %p %c - %m%n diff --git a/src/test/java/spoon/test/logging/LogTest.java b/src/test/java/spoon/test/logging/LogTest.java index 22f07a90a72..e21e52ae2d3 100644 --- a/src/test/java/spoon/test/logging/LogTest.java +++ b/src/test/java/spoon/test/logging/LogTest.java @@ -16,14 +16,11 @@ */ package spoon.test.logging; -import org.apache.commons.lang3.reflect.FieldUtils; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Logger; import org.junit.Test; import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import org.mockito.ArgumentCaptor; +import org.slf4j.event.Level; import spoon.Launcher; import spoon.MavenLauncher; import uk.org.lidalia.slf4jtest.TestLogger; @@ -34,8 +31,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.mock; @RunWith(Enclosed.class) public class LogTest { @@ -44,13 +39,11 @@ public class LogTest { public static class ParameterizedTest { @Parameterized.Parameters public static Collection data() { - return Arrays.asList(new Object[][]{ - {Level.ALL, 5}, - {Level.DEBUG, 5}, - {Level.INFO, 1}, - {Level.WARN, 0}, - {Level.ERROR, 0}, - {Level.OFF, 0} + return Arrays.asList(new Object[][] { + {Level.DEBUG, 6 }, + {Level.INFO, 2 }, + {Level.WARN, 0 }, + {Level.ERROR, 0 } }); } @@ -61,18 +54,11 @@ public static Collection data() { public int nbLogMessagesMinimum; @Test - public void testAllLevelsForLogs() throws Exception { + public void testAllLevelsForLogs() { + final TestLogger logger = TestLoggerFactory.getTestLogger(Launcher.class); final Launcher launcher = new Launcher(); - - Logger logger = mock(Logger.class); - FieldUtils.writeField(launcher.getEnvironment(), "logger", logger, true); - - ArgumentCaptor valueCaptureMessage = ArgumentCaptor.forClass(String.class); - ArgumentCaptor valueCaptureLevel = ArgumentCaptor.forClass(Level.class); - - doNothing().when(logger).log(valueCaptureLevel.capture(), valueCaptureMessage.capture()); - - launcher.setArgs(new String[]{ + logger.clear(); + launcher.setArgs(new String[] { "-i", "./src/test/java/spoon/test/logging", "--level", level.toString() }); @@ -85,8 +71,7 @@ public void testAllLevelsForLogs() throws Exception { assertEquals(level, launcher.getFactory().getEnvironment().getLevel()); // contract: the number of messages increases with the log level - // System.out.println(level+ " " + valueCaptureMessage.getAllValues().size()); - assertTrue(valueCaptureMessage.getAllValues().size() >= nbLogMessagesMinimum); + assertTrue(logger.getLoggingEvents().size() >= nbLogMessagesMinimum); } } diff --git a/src/test/java/spoon/test/prettyprinter/DefaultPrettyPrinterTest.java b/src/test/java/spoon/test/prettyprinter/DefaultPrettyPrinterTest.java index a09f9e959f8..12325f0cc4e 100644 --- a/src/test/java/spoon/test/prettyprinter/DefaultPrettyPrinterTest.java +++ b/src/test/java/spoon/test/prettyprinter/DefaultPrettyPrinterTest.java @@ -18,7 +18,7 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; import org.apache.maven.model.Model; import org.apache.maven.model.Plugin; import org.apache.maven.model.Profile; diff --git a/src/test/java/spoon/test/processing/ProcessingTest.java b/src/test/java/spoon/test/processing/ProcessingTest.java index 6d6df8450ff..8f82261e93a 100644 --- a/src/test/java/spoon/test/processing/ProcessingTest.java +++ b/src/test/java/spoon/test/processing/ProcessingTest.java @@ -18,7 +18,7 @@ import com.google.common.io.Files; import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import org.junit.Test; import spoon.Launcher; import spoon.SpoonException; diff --git a/src/test/java/spoon/test/processing/processors/TestProcessor.java b/src/test/java/spoon/test/processing/processors/TestProcessor.java index 0c26f939353..c3ee3dfa511 100644 --- a/src/test/java/spoon/test/processing/processors/TestProcessor.java +++ b/src/test/java/spoon/test/processing/processors/TestProcessor.java @@ -18,7 +18,7 @@ import java.util.Date; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import spoon.processing.AbstractProcessor; import spoon.reflect.cu.CompilationUnit; import spoon.reflect.declaration.CtClass; diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml new file mode 100644 index 00000000000..e505519e6d5 --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file