|
18 | 18 | */ |
19 | 19 | package org.tweetyproject.web.services; |
20 | 20 |
|
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
21 | 23 | import java.util.logging.ConsoleHandler; |
22 | 24 | import java.util.logging.FileHandler; |
23 | 25 | import java.util.logging.Logger; |
24 | 26 | import java.util.logging.SimpleFormatter; |
25 | | -import java.io.IOException; |
| 27 | + |
26 | 28 |
|
27 | 29 | /** |
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. |
29 | 31 | * |
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> |
34 | 35 | * |
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> |
37 | 38 | * |
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> |
42 | 41 | * |
| 42 | + * <p>This utility is intended for use in TweetyProject's web services module.</p> |
| 43 | + * @author Jonas Klein |
43 | 44 | * @see Logger |
44 | 45 | * @see ConsoleHandler |
45 | 46 | * @see FileHandler |
46 | 47 | * @see SimpleFormatter |
47 | 48 | */ |
| 49 | + |
48 | 50 | public class LoggerUtil { |
49 | | - /** logger */ |
50 | 51 | public static final Logger logger = Logger.getLogger(LoggerUtil.class.getName()); |
51 | 52 |
|
52 | 53 | static { |
53 | 54 | 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()); |
57 | 58 |
|
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 | + } |
61 | 70 |
|
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()); |
65 | 73 |
|
66 | | - // Set the log level (optional, default is INFO) |
| 74 | + // 3) Attach handlers & set level |
| 75 | + logger.addHandler(ch); |
| 76 | + logger.addHandler(fh); |
67 | 77 | logger.setLevel(java.util.logging.Level.INFO); |
68 | 78 |
|
69 | 79 | } catch (IOException e) { |
70 | 80 | e.printStackTrace(); |
71 | 81 | } |
72 | 82 | } |
73 | 83 | } |
74 | | - |
|
0 commit comments