Skip to content

Commit 5b67d98

Browse files
author
“jklein94”
committed
Fixed bug in logger of tweetyproject-web
1 parent 7c6bd77 commit 5b67d98

File tree

5 files changed

+54
-48
lines changed

5 files changed

+54
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ testBuild
3636

3737
gurobi-9.1.0.jar
3838
isula-1.1.1.jar
39+
logs/server.log.lck

org-tweetyproject-web/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<!-- Logging with java.util.logging -->
4141
<dependency>
4242
<groupId>org.springframework.boot</groupId>
43-
<artifactId>spring-boot-starter-logging</artifactId>
43+
<artifactId>spring-boot-starter-logging</artifactId>
4444
</dependency>
4545
<dependency>
4646
<groupId>org.openjfx</groupId>
@@ -127,7 +127,7 @@
127127
<version>20231013</version>
128128
</dependency>
129129

130-
<!-- SLF4J API -->
130+
<!-- SLF4J API
131131
<dependency>
132132
<groupId>org.slf4j</groupId>
133133
<artifactId>slf4j-api</artifactId>
@@ -138,14 +138,14 @@
138138
<groupId>ch.qos.logback</groupId>
139139
<artifactId>logback-core</artifactId>
140140
<version>1.5.13</version>
141-
</dependency>
141+
</dependency> -->
142142

143143
<!-- Logback (Logging Implementation) -->
144-
<dependency>
144+
<!-- <dependency>
145145
<groupId>ch.qos.logback</groupId>
146146
<artifactId>logback-classic</artifactId>
147147
<version>1.4.12</version>
148-
</dependency>
148+
</dependency> -->
149149

150150
</dependencies>
151151

org-tweetyproject-web/src/main/java/org/tweetyproject/web/services/LoggerUtil.java

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,57 +18,66 @@
1818
*/
1919
package org.tweetyproject.web.services;
2020

21+
import java.io.File;
22+
import java.io.IOException;
2123
import java.util.logging.ConsoleHandler;
2224
import java.util.logging.FileHandler;
2325
import java.util.logging.Logger;
2426
import java.util.logging.SimpleFormatter;
25-
import java.io.IOException;
27+
2628

2729
/**
28-
* Utility class for configuring a logger with console and file handlers.
30+
* Utility class for configuring a Java {@link Logger} with both console and file handlers.
2931
*
30-
* The LoggerUtil class provides a static logger instance with both console and file handlers attached.
31-
* The log messages are formatted using SimpleFormatter. The log level is set to INFO by default but can be
32-
* customized. Log messages are written to both the console and a log file named "server.log" located in the
33-
* specified file path.
32+
* <p>This logger is initialized statically and can be accessed through the public
33+
* {@code logger} field. It is configured to output log messages to both the console
34+
* and a rolling log file named {@code logs/server.log}.</p>
3435
*
35-
* Usage of this utility class ensures that log messages are directed to both the console and a log file
36-
* simultaneously, allowing for effective logging and monitoring of application activities.
36+
* <p>The log file is created within a {@code logs} directory relative to the working
37+
* directory. If this directory does not exist, it will be created automatically.</p>
3738
*
38-
* Note: The file handler is configured to append to the existing log file (if any) rather than overwriting it.
39-
*
40-
* To customize the log level or handle exceptions during configuration, users can modify the static block
41-
* accordingly. The logger instance is accessible as a public static field, e.g., LoggerUtil.logger.
39+
* <p>The logger uses {@link SimpleFormatter} for formatting both console and file outputs,
40+
* and it is configured at {@code Level.INFO} by default.</p>
4241
*
42+
* <p>This utility is intended for use in TweetyProject's web services module.</p>
43+
* @author Jonas Klein
4344
* @see Logger
4445
* @see ConsoleHandler
4546
* @see FileHandler
4647
* @see SimpleFormatter
4748
*/
49+
4850
public class LoggerUtil {
49-
/** logger */
5051
public static final Logger logger = Logger.getLogger(LoggerUtil.class.getName());
5152

5253
static {
5354
try {
54-
// Create a console handler
55-
ConsoleHandler consoleHandler = new ConsoleHandler();
56-
consoleHandler.setFormatter(new SimpleFormatter());
55+
// 1) Console handler
56+
ConsoleHandler ch = new ConsoleHandler();
57+
ch.setFormatter(new SimpleFormatter());
5758

58-
// Create a file handler
59-
FileHandler fileHandler = new FileHandler("TweetyProject/org-tweetyproject-web/src/main/java/org/tweetyproject/web/spring_services/server.log",true);
60-
fileHandler.setFormatter(new SimpleFormatter());
59+
// 2) File handler — ensure parent dirs exist first
60+
// (you probably don't want to write into your src folder;
61+
// better to use a dedicated "logs" directory)
62+
String logPath = "logs/server.log";
63+
File logFile = new File(logPath);
64+
File parent = logFile.getParentFile();
65+
if (parent != null && !parent.exists()) {
66+
if (!parent.mkdirs()) {
67+
System.err.println("Could not create log directory: " + parent);
68+
}
69+
}
6170

62-
// Attach handlers to the logger
63-
logger.addHandler(consoleHandler);
64-
logger.addHandler(fileHandler);
71+
FileHandler fh = new FileHandler(logFile.getAbsolutePath(), true);
72+
fh.setFormatter(new SimpleFormatter());
6573

66-
// Set the log level (optional, default is INFO)
74+
// 3) Attach handlers & set level
75+
logger.addHandler(ch);
76+
logger.addHandler(fh);
6777
logger.setLevel(java.util.logging.Level.INFO);
6878

6979
} catch (IOException e) {
7080
e.printStackTrace();
7181
}
7282
}
7383
}
74-

org-tweetyproject-web/src/main/java/org/tweetyproject/web/services/RestServiceCorsApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
public class RestServiceCorsApplication {
3535
/** Allowed origins for production and debug environments */
3636
static String allowedOrigins = "http://tweetyproject.org";
37-
String debug_allowedOrigins = "http://127.0.0.1:5500/";
37+
static String debug_allowedOrigins = "http://127.0.0.1:5500/";
3838
/**
3939
* Main method that starts the Spring Boot application and logs the allowed origins.
4040
* It also sets up necessary configurations and initializes the Spring context.
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
<!-- logback.xml -->
2-
<!-- <configuration>
3-
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4-
<encoder>
5-
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
6-
</encoder>
7-
</appender> -->
1+
<configuration>
82

9-
<!-- File Appender -->
10-
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
11-
<file>/home/jklein/dev/tweety_clean/TweetyProject/org-tweetyproject-web/src/main/resources/logs/tweety_server.logs</file>
12-
<encoder>
13-
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
14-
</encoder>
15-
<immediateFlush>true</immediateFlush>
16-
</appender>
3+
<!-- File Appender -->
4+
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
5+
<!-- I recommend writing logs outside of src/ so they don't get packaged into your JAR -->
6+
<file>logs/tweety_server.log</file>
7+
<encoder>
8+
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
9+
</encoder>
10+
<immediateFlush>true</immediateFlush>
11+
</appender>
1712

18-
<root level="debug">
19-
<appender-ref ref="FILE" />
20-
</root>
13+
<!-- Root logger -->
14+
<root level="DEBUG">
15+
<appender-ref ref="FILE"/>
16+
</root>
2117

2218
</configuration>

0 commit comments

Comments
 (0)