Skip to content

Commit 88d2092

Browse files
committed
Remove log4j 1.2.17 JAR from libs and switch to SLF4J backed by Log4j2
1 parent fa96917 commit 88d2092

9 files changed

Lines changed: 91 additions & 191 deletions

File tree

build.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ repositories {
2727
dependencies {
2828
testImplementation("junit:junit:4.13.1")
2929

30+
implementation("org.slf4j:slf4j-api:2.0.17")
31+
implementation("org.apache.logging.log4j:log4j-core:2.25.3")
32+
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:2.25.3")
33+
3034
// Using flatDir dependencies (lookup by name only)
3135
implementation(":JFlex")
3236
implementation(":cup_v10k")
33-
implementation(":log4j-1.2.17")
3437
implementation(":mj-runtime-1.1")
3538
implementation(":symboltable")
3639
}

libs/log4j-1.2.17.jar

-478 KB
Binary file not shown.

src/main/java/dev/askov/mjcompiler/MJCompiler.java

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,23 @@
2323
import dev.askov.mjcompiler.inheritancetree.InheritanceTree;
2424
import dev.askov.mjcompiler.inheritancetree.visitor.InheritanceTreePrinter;
2525
import dev.askov.mjcompiler.mjsymboltable.MJTab;
26-
import dev.askov.mjcompiler.util.Log4JUtils;
2726
import dev.askov.mjcompiler.vmt.VMTCodeGenerator;
2827
import dev.askov.mjcompiler.vmt.VMTCreator;
2928
import dev.askov.mjcompiler.vmt.VMTStartAddressGenerator;
3029
import java.io.BufferedReader;
3130
import java.io.File;
3231
import java.io.FileOutputStream;
3332
import java.io.FileReader;
34-
import org.apache.log4j.Logger;
35-
import org.apache.log4j.xml.DOMConfigurator;
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
3635
import rs.etf.pp1.mj.runtime.Code;
3736

3837
/**
3938
* @author Danijel Askov
4039
*/
4140
public class MJCompiler {
4241

43-
static {
44-
DOMConfigurator.configure(Log4JUtils.instance().findLoggerConfigFile());
45-
Log4JUtils.instance().prepareLogFile(Logger.getRootLogger());
46-
}
47-
48-
private static final Logger LOGGER = Logger.getLogger(MJCompiler.class);
42+
private static final Logger LOGGER = LoggerFactory.getLogger(MJCompiler.class);
4943

5044
public static void tsdump() {
5145
MJTab.dump(LOGGER);
@@ -58,22 +52,21 @@ public static void main(String[] args) throws Exception {
5852
}
5953
var sourceFile = new File(args[0]);
6054
if (!sourceFile.exists()) {
61-
LOGGER.error("Source file \"" + sourceFile.getAbsolutePath() + "\" has not been found!");
55+
LOGGER.error("Source file \"{}\" has not been found!", sourceFile.getAbsolutePath());
6256
return;
6357
}
64-
LOGGER.info("Compiling source file \"" + sourceFile.getAbsolutePath() + "\"...");
58+
LOGGER.info("Compiling source file \"{}\"...", sourceFile.getAbsolutePath());
6559
try (var br = new BufferedReader(new FileReader(sourceFile))) {
6660
var lexer = new MJLexer(br);
6761
var parser = new MJParser(lexer);
6862
var symbol = parser.parse();
6963

7064
if (!parser.lexicalErrorDetected() && !parser.syntaxErrorDetected()) {
71-
LOGGER.info(
72-
"No syntax errors have been detected in \"" + sourceFile.getAbsolutePath() + "\"");
65+
LOGGER.info("No syntax errors have been detected in \"{}\"", sourceFile.getAbsolutePath());
7366

7467
var program = (Program) symbol.value;
7568

76-
LOGGER.info("Abstract syntax tree:\n" + program.toString(""));
69+
LOGGER.info("Abstract syntax tree:\n{}", program.toString(""));
7770

7871
MJTab.init();
7972
var semanticAnalyzer = new SemanticAnalyzer();
@@ -94,18 +87,17 @@ public static void main(String[] args) throws Exception {
9487
semanticAnalyzer.getStaticVarsCount() + vmtStartAddressGenerator.getTotalVMTSize();
9588

9689
LOGGER.info(
97-
"No semantic errors have been detected in \"" + sourceFile.getAbsolutePath() + "\"");
90+
"No semantic errors have been detected in \"{}\"", sourceFile.getAbsolutePath());
9891

9992
var objFile = new File(args[1]);
100-
LOGGER.info("Generating bytecode file \"" + objFile.getAbsolutePath() + "\"...");
93+
LOGGER.info("Generating bytecode file \"{}\"...", objFile.getAbsolutePath());
10194
if (objFile.exists()) {
102-
LOGGER.info("Deleting old bytecode file \"" + objFile.getAbsolutePath() + "\"...");
95+
LOGGER.info("Deleting old bytecode file \"{}\"...", objFile.getAbsolutePath());
10396
if (objFile.delete())
104-
LOGGER.info(
105-
"Old bytecode file \"" + objFile.getAbsolutePath() + "\" has been deleted.");
97+
LOGGER.info("Old bytecode file \"{}\" has been deleted.", objFile.getAbsolutePath());
10698
else
10799
LOGGER.error(
108-
"Old bytecode file \"" + objFile.getAbsolutePath() + "\" has not been deleted.");
100+
"Old bytecode file \"{}\" has not been deleted.", objFile.getAbsolutePath());
109101
}
110102

111103
var codeGenerator = new CodeGenerator();
@@ -138,35 +130,32 @@ public static void main(String[] args) throws Exception {
138130
Code.put(Code.return_);
139131

140132
Code.write(new FileOutputStream(objFile));
141-
LOGGER.info("Bytecode file \"" + objFile.getAbsolutePath() + "\" has been generated.");
133+
LOGGER.info("Bytecode file \"{}\" has been generated.", objFile.getAbsolutePath());
142134
LOGGER.info(
143-
"Compilation of source file \""
144-
+ sourceFile.getAbsolutePath()
145-
+ "\" has finished successfully.\n");
135+
"Compilation of source file \"{}\" has finished successfully.\n",
136+
sourceFile.getAbsolutePath());
146137

147-
LOGGER.info("Inheritance tree:\n" + inheritanceTreeNodePrinter.getOutput());
138+
LOGGER.info("Inheritance tree:\n{}", inheritanceTreeNodePrinter.getOutput());
148139
} else {
149140
LOGGER.error(
150-
"Source file \"" + sourceFile.getAbsolutePath() + "\" contains semantic error(s)!");
141+
"Source file \"{}\" contains semantic error(s)!", sourceFile.getAbsolutePath());
151142
LOGGER.error(
152-
"Compilation of source file \""
153-
+ sourceFile.getAbsolutePath()
154-
+ "\" has finished unsuccessfully.");
143+
"Compilation of source file \"{}\" has finished unsuccessfully.",
144+
sourceFile.getAbsolutePath());
155145
}
156146

157147
} else {
158148
if (parser.lexicalErrorDetected()) {
159149
LOGGER.error(
160-
"Source file \"" + sourceFile.getAbsolutePath() + "\" contains lexical error(s)!");
150+
"Source file \"{}\" contains lexical error(s)!", sourceFile.getAbsolutePath());
161151
}
162152
if (parser.syntaxErrorDetected()) {
163153
LOGGER.error(
164-
"Source file \"" + sourceFile.getAbsolutePath() + "\" contains syntax error(s)!");
154+
"Source file \"{}\" contains syntax error(s)!", sourceFile.getAbsolutePath());
165155
}
166156
LOGGER.error(
167-
"Compilation of source file \""
168-
+ sourceFile.getAbsolutePath()
169-
+ "\" has finished unsuccessfully.");
157+
"Compilation of source file \"{}\" has finished unsuccessfully.",
158+
sourceFile.getAbsolutePath());
170159
}
171160
}
172161
}

src/main/java/dev/askov/mjcompiler/loggers/MJLogger.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
package dev.askov.mjcompiler.loggers;
2121

22-
import org.apache.log4j.Logger;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
2324

2425
/**
2526
* @author Danijel Askov
@@ -31,7 +32,7 @@ public enum Type {
3132
ERROR_LOGGER
3233
}
3334

34-
protected final Logger log = Logger.getLogger(getClass());
35+
protected final Logger log = LoggerFactory.getLogger(getClass());
3536
private final Type type;
3637
protected final String messageHead;
3738

@@ -43,20 +44,16 @@ public MJLogger(Type type, String messageHead) {
4344
protected abstract String messageBody(T loggedObject, Object... context);
4445

4546
public final void log(T loggedObject, Integer line, Integer column, Object... context) {
46-
var message =
47-
String.format("%-14s", this.messageHead)
48-
+ (line != null
49-
? " (line "
50-
+ String.format("%3d", line)
51-
+ (column != null ? ", column " + String.format("%3d", column) : "")
52-
+ ")"
53-
: "")
54-
+ ": "
55-
+ this.messageBody(loggedObject, context)
56-
+ ".";
47+
var head = String.format("%-14s", this.messageHead);
48+
var location =
49+
line != null
50+
? String.format(
51+
" (line %3d%s)", line, column != null ? String.format(", column %3d", column) : "")
52+
: "";
53+
var body = this.messageBody(loggedObject, context);
5754
switch (this.type) {
58-
case INFO_LOGGER -> log.info(message);
59-
case ERROR_LOGGER -> log.error(message);
55+
case INFO_LOGGER -> log.info("{}{}: {}.", head, location, body);
56+
case ERROR_LOGGER -> log.error("{}{}: {}.", head, location, body);
6057
}
6158
}
6259
}

src/main/java/dev/askov/mjcompiler/mjsymboltable/MJTab.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.HashMap;
2626
import java.util.List;
2727
import java.util.Map;
28-
import org.apache.log4j.Logger;
28+
import org.slf4j.Logger;
2929
import rs.etf.pp1.symboltable.Tab;
3030
import rs.etf.pp1.symboltable.concepts.Obj;
3131
import rs.etf.pp1.symboltable.concepts.Struct;
@@ -143,7 +143,7 @@ public static void dump(MJDumpSymbolTableVisitor mjSymbolTableVisitor, Logger lo
143143
for (var s = currentScope; s != null; s = s.getOuter()) {
144144
s.accept(mjSymbolTableVisitor);
145145
}
146-
logger.info("Symbol table:\n" + mjSymbolTableVisitor.getOutput());
146+
logger.info("Symbol table:\n{}", mjSymbolTableVisitor.getOutput());
147147
}
148148

149149
public static void dump(Logger logger) {

src/main/java/dev/askov/mjcompiler/util/Log4JUtils.java

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/main/resources/log4j.xml

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)