Merge branch '2-uniformisation-du-logger' into 'dev'

refactor(logger): passage de system.out.println()/e.printStackTrace() à LOGGER.log()

Closes #2

See merge request iut_rt/but2/sae302-applicom/bouclyma!18
This commit is contained in:
Emi Boucly 2025-02-13 08:58:17 +01:00
commit c5bb9d9af2
7 changed files with 49 additions and 42 deletions

View file

@ -3,6 +3,10 @@ package rtgre;
import rtgre.chat.ChatApplication; import rtgre.chat.ChatApplication;
import rtgre.server.ChatServer; import rtgre.server.ChatServer;
import java.util.logging.Level;
import static rtgre.chat.ChatApplication.LOGGER;
/** /**
* Application pour lancer soit ChatServer, soit ChatApplication * Application pour lancer soit ChatServer, soit ChatApplication
@ -14,19 +18,19 @@ public class ChatLauncher {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
for (String arg : args) { for (String arg : args) {
System.out.println(arg); LOGGER.log(Level.FINEST, arg);
} }
try { try {
if ("server".equals(args[0])) { if ("server".equals(args[0])) {
System.out.println("test1"); LOGGER.log(Level.INFO, "Lancement du serveur...");
ChatServer.main(args); ChatServer.main(args);
} else { } else {
LOGGER.log(Level.INFO, "Lancement du client...");
ChatApplication.main(args); ChatApplication.main(args);
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println(e.getMessage()); LOGGER.log(Level.FINEST, e.getMessage(), e);
e.printStackTrace(); LOGGER.log(Level.INFO, "Lancement du client...");
System.out.println("test2");
ChatApplication.main(args); ChatApplication.main(args);
} }

View file

@ -34,6 +34,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.*;
import java.util.logging.Level;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -126,7 +127,7 @@ public class ChatController implements Initializable {
try { try {
InputStream in = ChatController.class.getResourceAsStream("config.properties"); InputStream in = ChatController.class.getResourceAsStream("config.properties");
System.out.println(ChatController.class.getResource("config.properties").getPath()); LOGGER.finest("Chemin du fichier config.properties: " + ChatController.class.getResource("config.properties").getPath());
properties.load(in); properties.load(in);
if (contact != null) { if (contact != null) {
this.contact.setAvatar(Contact.base64ToImage(properties.getProperty("avatar"))); this.contact.setAvatar(Contact.base64ToImage(properties.getProperty("avatar")));
@ -145,14 +146,9 @@ public class ChatController implements Initializable {
exchangeSplitPane.setDividerPositions(Double.parseDouble(properties.getProperty("split2"))); exchangeSplitPane.setDividerPositions(Double.parseDouble(properties.getProperty("split2")));
} }
} catch (IOException e) { } catch (IOException | NullPointerException e) {
LOGGER.warning("Impossible de charger le fichier de configuration! Configuration par défaut chargée"); LOGGER.warning("Impossible de charger le fichier de configuration! Configuration par défaut chargée");
System.out.println(e.getMessage()); LOGGER.log(Level.SEVERE, e.getMessage(), e);
e.printStackTrace();
} catch (NullPointerException e) {
LOGGER.warning("Impossible de charger le fichier de configuration! Configuration par défaut chargée");
System.out.println(e.getMessage());
e.printStackTrace();
} }
Thread dateTimeLoop = new Thread(this::dateTimeLoop); Thread dateTimeLoop = new Thread(this::dateTimeLoop);
@ -291,7 +287,8 @@ public class ChatController implements Initializable {
} }
} catch (IOException e) { } catch (IOException e) {
LOGGER.warning("Impossible d'ouvrir la fenêtre de dialogue: fxml introuvable \n" + e.getMessage()); LOGGER.warning("Impossible d'ouvrir la fenêtre de dialogue: fxml introuvable \n" + e.getMessage());
e.printStackTrace(); LOGGER.log(Level.FINEST, e.getMessage(), e);
} }
} }
@ -659,9 +656,9 @@ public class ChatController implements Initializable {
*/ */
private void handlePostEvent(JSONObject content) { private void handlePostEvent(JSONObject content) {
System.out.println("Selected: " + roomsListView.getSelectionModel().getSelectedItem()); LOGGER.info("Selected: " + roomsListView.getSelectionModel().getSelectedItem());
System.out.println("From: " + content.getString("from")); LOGGER.info("From: " + content.getString("from"));
System.out.println("To: " + content.getString("to")); LOGGER.info("To: " + content.getString("to"));
try { try {
if (!content.getString("to").contains("#")) { if (!content.getString("to").contains("#")) {
@ -743,17 +740,17 @@ public class ChatController implements Initializable {
if (!content.getString("avatar").isEmpty()) { if (!content.getString("avatar").isEmpty()) {
avatar = Contact.base64ToImage(content.getString("avatar")); avatar = Contact.base64ToImage(content.getString("avatar"));
} }
System.out.println(avatar);
if (contact != null) { if (contact != null) {
LOGGER.info(contactMap.toString()); LOGGER.info(contactMap.toString());
contactMap.getContact(content.getString("login")).setConnected(content.getBoolean("connected")); contactMap.getContact(content.getString("login")).setConnected(content.getBoolean("connected"));
if (avatar != null) { if (avatar != null) {
LOGGER.log(Level.FINEST, avatar.toString());
contactMap.getContact(content.getString("login")).setAvatar(avatar); contactMap.getContact(content.getString("login")).setAvatar(avatar);
} }
contactsListView.refresh(); contactsListView.refresh();
LOGGER.info(contactMap.toString()); LOGGER.info(contactMap.toString());
} else { } else {
System.out.println(content); LOGGER.log(Level.FINEST, content.toString());
LOGGER.info(contactMap.toString()); LOGGER.info(contactMap.toString());
Contact user = Contact.fromJSON( Contact user = Contact.fromJSON(
content, content,

View file

@ -9,6 +9,7 @@ import rtgre.modeles.Message;
import rtgre.modeles.Post; import rtgre.modeles.Post;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import static rtgre.chat.ChatApplication.LOGGER; import static rtgre.chat.ChatApplication.LOGGER;
@ -48,13 +49,13 @@ public class ChatClient extends ClientTCP {
if (message == null) { // fin du flux stdIn if (message == null) { // fin du flux stdIn
message = END_MESSAGE; message = END_MESSAGE;
} }
System.out.println(BLUE + "Envoi: " + message + RST); LOGGER.log(Level.FINE, BLUE + "Envoi: " + message + RST);
this.send(message); this.send(message);
if (END_MESSAGE.equals(message)) { if (END_MESSAGE.equals(message)) {
connected = false; connected = false;
} }
} catch (IOException e) { } catch (IOException e) {
LOGGER.severe(e.toString()); LOGGER.log(Level.SEVERE, e.getMessage(), e);
connected = false; connected = false;
} }
} }
@ -110,10 +111,10 @@ public class ChatClient extends ClientTCP {
try { try {
while (connected) { while (connected) {
String message = this.receive(); String message = this.receive();
LOGGER.info(RED + "Réception: " + message + RST); LOGGER.finest(RED + "Réception: " + message + RST);
LOGGER.info(RED + message + RST); LOGGER.info(RED + message + RST);
if (listener != null) { if (listener != null) {
System.out.println(message); LOGGER.log(Level.FINE, message);
Platform.runLater(() -> listener.handleEvent(Event.fromJson(message))); Platform.runLater(() -> listener.handleEvent(Event.fromJson(message)));
} }
} }

View file

@ -4,6 +4,8 @@ package rtgre.chat.net;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import static rtgre.chat.ChatApplication.LOGGER; import static rtgre.chat.ChatApplication.LOGGER;
/** /**
@ -70,7 +72,7 @@ public class ClientTCP {
* @throws IOException si la connexion échoue ou si les flux ne sont pas récupérables * @throws IOException si la connexion échoue ou si les flux ne sont pas récupérables
*/ */
public ClientTCP(String host, int port) throws IOException { public ClientTCP(String host, int port) throws IOException {
System.out.printf("Connexion à [%s:%d]%n", host, port); LOGGER.info("Connexion à [%s:%d]".formatted(host, port));
sock = new Socket(host, port); sock = new Socket(host, port);
ipPort = "%s:%d".formatted(sock.getLocalAddress().getHostAddress(), sock.getLocalPort()); ipPort = "%s:%d".formatted(sock.getLocalAddress().getHostAddress(), sock.getLocalPort());
LOGGER.info("[%s] Connexion établie vers [%s:%d]".formatted(ipPort, host, port)); LOGGER.info("[%s] Connexion établie vers [%s:%d]".formatted(ipPort, host, port));
@ -149,24 +151,24 @@ public class ClientTCP {
* Boucle d'envoi de messages * Boucle d'envoi de messages
*/ */
public void sendLoop() { public void sendLoop() {
System.out.println(BLUE + "Boucle d'envoi de messages..." + RST); LOGGER.log(Level.FINE, BLUE + "Boucle d'envoi de messages..." + RST);
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
connected = true; connected = true;
try { try {
while (connected) { while (connected) {
System.out.println(BLUE + "Votre message (\"fin\" pour terminer) : " + RST); LOGGER.log(Level.FINE, BLUE + "Votre message (\"fin\" pour terminer) : " + RST);
String message = stdIn.readLine(); String message = stdIn.readLine();
if (message == null) { // fin du flux stdIn if (message == null) { // fin du flux stdIn
message = END_MESSAGE; message = END_MESSAGE;
} }
System.out.println(BLUE + "Envoi: " + message + RST); LOGGER.log(Level.FINE, BLUE + "Envoi: " + message + RST);
this.send(message); this.send(message);
if (END_MESSAGE.equals(message)) { if (END_MESSAGE.equals(message)) {
connected = false; connected = false;
} }
} }
} catch (IOException e) { } catch (IOException e) {
LOGGER.severe(e.toString()); LOGGER.log(Level.WARNING, e.getMessage(), e);
connected = false; connected = false;
} }
} }
@ -175,12 +177,12 @@ public class ClientTCP {
* Boucle de réception de messages * Boucle de réception de messages
*/ */
public void receiveLoop() { public void receiveLoop() {
System.out.println(RED + "Boucle de réception de messages..." + RST); LOGGER.log(Level.FINE, RED + "Boucle de réception de messages..." + RST);
connected = true; connected = true;
try { try {
while (connected) { while (connected) {
String message = this.receive(); String message = this.receive();
System.out.println(RED + "Réception: " + message + RST); LOGGER.log(Level.FINE, RED + "Réception: " + message + RST);
} }
} catch (IOException e) { } catch (IOException e) {
LOGGER.severe("[%s] %s".formatted(ipPort, e)); LOGGER.severe("[%s] %s".formatted(ipPort, e));

View file

@ -11,6 +11,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Base64; import java.util.Base64;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Level;
import static rtgre.chat.ChatApplication.LOGGER; import static rtgre.chat.ChatApplication.LOGGER;
@ -201,7 +202,7 @@ public class Contact {
try { try {
this.avatar = avatarFromLogin(file, this.login); this.avatar = avatarFromLogin(file, this.login);
} catch (IOException e) { } catch (IOException e) {
System.out.println("Erreur : " + e.getMessage()); LOGGER.log(Level.WARNING, e.getMessage(), e);
} }
} }
@ -230,7 +231,6 @@ public class Contact {
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
try { try {
ImageIO.write(img, "png", bos); ImageIO.write(img, "png", bos);
System.out.println(Base64.getEncoder().encodeToString(bos.toByteArray()));
return Base64.getEncoder().encodeToString(bos.toByteArray()); return Base64.getEncoder().encodeToString(bos.toByteArray());
} catch (IOException e) { } catch (IOException e) {
LOGGER.severe("Impossible de convertir l'image en base64"); LOGGER.severe("Impossible de convertir l'image en base64");

View file

@ -2,6 +2,8 @@ package rtgre.modeles;
import java.sql.*; import java.sql.*;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level;
import org.sqlite.JDBC; import org.sqlite.JDBC;
import static rtgre.chat.ChatApplication.LOGGER; import static rtgre.chat.ChatApplication.LOGGER;
@ -25,8 +27,8 @@ public class DatabaseApi {
initDB(con); initDB(con);
LOGGER.info("Database connected!"); LOGGER.info("Database connected!");
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.severe("Can't connect to database! \n" + e.getMessage()); LOGGER.severe("Can't connect to database! \n");
e.printStackTrace(); LOGGER.log(Level.FINE, e.getMessage(), e);
} }
} }
@ -47,8 +49,7 @@ public class DatabaseApi {
stmt.execute(sql); stmt.execute(sql);
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.severe("Cannot initialize database!"); LOGGER.severe("Cannot initialize database!");
System.out.println(e.getMessage()); LOGGER.log(Level.FINE, e.getMessage(), e);
e.printStackTrace();
} }
} }
@ -63,6 +64,7 @@ public class DatabaseApi {
return stmt.executeQuery(query); return stmt.executeQuery(query);
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.severe("Can't get post by id!"); LOGGER.severe("Can't get post by id!");
LOGGER.log(Level.FINE, e.getMessage(), e);
return null; return null;
} }
} }
@ -78,6 +80,7 @@ public class DatabaseApi {
return stmt.executeQuery(query); return stmt.executeQuery(query);
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.severe("Can't get post since " + timestamp + "!"); LOGGER.severe("Can't get post since " + timestamp + "!");
LOGGER.log(Level.FINE, e.getMessage(), e);
return null; return null;
} }
} }
@ -133,8 +136,7 @@ public class DatabaseApi {
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.severe("Cannot remove post!"); LOGGER.severe("Cannot remove post!");
System.out.println(e.getMessage()); LOGGER.log(Level.FINE, e.getMessage(), e);
e.printStackTrace();;
return false; return false;
} }
} }
@ -146,6 +148,7 @@ public class DatabaseApi {
try { try {
con.close(); con.close();
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.log(Level.FINE, e.getMessage(), e);
LOGGER.severe("Can't close database connection! Is database connected ?"); LOGGER.severe("Can't close database connection! Is database connected ?");
} }
} }

View file

@ -443,11 +443,11 @@ public class ChatServer {
private void doListPost(JSONObject content) throws JSONException, IllegalStateException { private void doListPost(JSONObject content) throws JSONException, IllegalStateException {
if (contactMap.getContact(user.getLogin()).isConnected()) { if (contactMap.getContact(user.getLogin()).isConnected()) {
if (!contactMap.containsKey(content.getString("select")) && !roomMap.containsKey(content.getString("select"))) { if (!contactMap.containsKey(content.getString("select")) && !roomMap.containsKey(content.getString("select"))) {
System.out.println("!select"); LOGGER.log(Level.FINEST, "!select");
throw new IllegalStateException(); throw new IllegalStateException();
} }
if (!content.getString("select").contains("#")) { if (!content.getString("select").contains("#")) {
System.out.println("!#"); LOGGER.log(Level.FINEST, "!#");
for (Post post : postVector.getPostsSince(content.getLong("since"))) { for (Post post : postVector.getPostsSince(content.getLong("since"))) {
if (post.getTo().equals(content.getString("select")) || if (post.getTo().equals(content.getString("select")) ||
post.getFrom().equals(content.getString("select"))) { post.getFrom().equals(content.getString("select"))) {
@ -455,7 +455,7 @@ public class ChatServer {
} }
} }
} else if (user.getCurrentRoom().equals(content.getString("select"))) { } else if (user.getCurrentRoom().equals(content.getString("select"))) {
System.out.println("#"); LOGGER.log(Level.FINEST, "#");
for (Post post: postVector.getPostsSince(content.getLong("since"))) { for (Post post: postVector.getPostsSince(content.getLong("since"))) {
if (post.getTo().equals(content.getString("select"))) { if (post.getTo().equals(content.getString("select"))) {
sendEventToContact(contactMap.getContact(user.getLogin()), new Event(Event.POST, post.toJsonObject())); sendEventToContact(contactMap.getContact(user.getLogin()), new Event(Event.POST, post.toJsonObject()));
@ -551,7 +551,7 @@ public class ChatServer {
LOGGER.info("Connexion de " + login); LOGGER.info("Connexion de " + login);
contactMap.getContact(login).setConnected(true); contactMap.getContact(login).setConnected(true);
this.user = contactMap.getContact(login); this.user = contactMap.getContact(login);
System.out.println(user.isConnected()); LOGGER.log(Level.FINEST, String.valueOf(user.isConnected()));
sendAllOtherClients( sendAllOtherClients(
findClient(contactMap.getContact(login)), findClient(contactMap.getContact(login)),
new Event("CONT", user.toJsonObject()).toJson() new Event("CONT", user.toJsonObject()).toJson()