Skip to content

Commit dac5edc

Browse files
committed
Configure logging to output to both console and file
- Added `logging.properties` to resource to include `ConsoleHandler` for console output and `FileHandler` to log messages at INFO level and above to logs/trafficlight.log. - Ensured the logs directory is created if it does not exist to prevent NoSuchFileException. - This setup provides persistent logging in trafficlight.log alongside console output for better visibility and tracking. - Added startup welcome title message in ascii art
1 parent 4a07f2e commit dac5edc

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ application {
88
}
99

1010
group = 'org.senegas'
11-
version = '1.1.0'
11+
version = '1.1.1'
1212

1313
repositories {
1414
mavenCentral()

src/main/java/org/senegas/trafficlight/TrafficLightApp.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,36 @@
99

1010
import javax.swing.*;
1111
import java.awt.*;
12+
import java.io.IOException;
13+
import java.nio.file.Files;
14+
import java.nio.file.Paths;
1215
import java.text.MessageFormat;
13-
14-
16+
import java.util.logging.LogManager;
17+
import java.util.logging.Logger;
1518

1619
public class TrafficLightApp {
1720

21+
private static final Logger logger = Logger.getLogger(TrafficLightApp.class.getName());
22+
1823
public static final String TITLE = "Traffic Light App";
1924
//TODO
2025
// // see https://stackoverflow.com/questions/33020069/how-to-get-version-attribute-from-a-gradle-build-to-be-included-in-runtime-swing
21-
public static final String VERSION = "1.1.0";
26+
public static final String VERSION = "1.1.1";
27+
28+
static {
29+
try {
30+
// Ensure the logs directory exists
31+
Files.createDirectories(Paths.get("logs"));
32+
33+
// Load the custom logging configuration from resources
34+
LogManager.getLogManager().readConfiguration(
35+
TrafficLightApp.class.getClassLoader().getResourceAsStream("logging.properties")
36+
);
37+
} catch (IOException e) {
38+
logger.severe("Failed to load logging configuration: " + e.getMessage());
39+
throw new RuntimeException(e);
40+
}
41+
}
2242

2343
public static void main(String[] args) {
2444
EventQueue.invokeLater(() -> new TrafficLightApp().create());

src/main/java/org/senegas/trafficlight/view/TrafficLightFrame.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ public class TrafficLightFrame extends JFrame {
2121

2222
public TrafficLightFrame(String title) throws HeadlessException {
2323
super(title);
24+
25+
final String asciiArtTitle = "\n _____ __ __ _ _ _ _ _ \n" +
26+
"|_ _| __ __ _ / _|/ _(_) ___| | (_) __ _| |__ | |_ \n" +
27+
" | || '__/ _` | |_| |_| |/ __| | | |/ _` | '_ \\| __|\n" +
28+
" | || | | (_| | _| _| | (__| |___| | (_| | | | | |_ \n" +
29+
" |_||_| \\__,_|_| |_| |_|\\___|_____|_|\\__, |_| |_|\\__|\n" +
30+
" |___/ ";
31+
LOGGER.log(Level.INFO, asciiArtTitle);
32+
LOGGER.log(Level.INFO, title + " has started");
33+
2434
createTrayIcon();
2535
}
2636

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Root logger level and handlers
2+
.level=INFO
3+
handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler
4+
5+
# ConsoleHandler configuration
6+
java.util.logging.ConsoleHandler.level=INFO
7+
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
8+
9+
# FileHandler configuration
10+
java.util.logging.FileHandler.level=INFO
11+
java.util.logging.FileHandler.pattern=logs/trafficlight.log
12+
java.util.logging.FileHandler.append=true
13+
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter

0 commit comments

Comments
 (0)