From ab13d5adb42b155521d0abb926d545f6e0fccaa0 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 21 Dec 2019 18:19:10 +0100 Subject: [PATCH 1/2] Improved logging Logs are now written to System.out instead of System.err. Also they are not duplicated as the default ConsoleHandler has been removed. When using the application, logs may not appear in the console immediately as the StreamHandler used to output them used an internal buffer that may only be flushed when closing the application. Logs are now formatted as [DATE TIME] [LEVEL] [LOGGER] MSG --- src/main/java/envoy/client/Config.java | 7 ++++--- src/main/java/envoy/client/ui/Startup.java | 2 +- src/main/java/envoy/client/util/EnvoyLog.java | 15 +++++++++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index 3e511f9..17da0d8 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -32,7 +32,8 @@ public class Config { items.put("port", new ConfigItem<>("port", "p", (input) -> Integer.parseInt(input), null)); items.put("localDB", new ConfigItem<>("localDB", "db", (input) -> new File(input), new File("localDB"))); items.put("syncTimeout", new ConfigItem<>("syncTimeout", "st", (input) -> Integer.parseInt(input), 1000)); - items.put("homeDirectory", new ConfigItem<>("homeDirectory", "h", (input) -> new File(input), new File(System.getProperty("user.home"), ".envoy"))); + items.put("homeDirectory", + new ConfigItem<>("homeDirectory", "h", (input) -> new File(input), new File(System.getProperty("user.home"), ".envoy"))); items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", (input) -> Level.parse(input), Level.CONFIG)); items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", (input) -> Level.parse(input), Level.FINEST)); } @@ -130,13 +131,13 @@ public class Config { * @since Envoy v0.2-alpha */ public File getHomeDirectory() { return (File) items.get("homeDirectory").get(); } - + /** * @return the minimal {@link Level} to log inside the log file * @since Envoy v0.2-alpha */ public Level getFileLevelBarrier() { return (Level) items.get("fileLevelBarrier").get(); } - + /** * @return the minimal {@link Level} to log inside the console * @since Envoy v0.2-alpha diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 21e4cf0..db9644a 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -59,7 +59,7 @@ public class Startup { System.exit(1); e.printStackTrace(); } - + // Set new logger levels loaded from config EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier()); EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier()); diff --git a/src/main/java/envoy/client/util/EnvoyLog.java b/src/main/java/envoy/client/util/EnvoyLog.java index 282e1b4..b59b714 100644 --- a/src/main/java/envoy/client/util/EnvoyLog.java +++ b/src/main/java/envoy/client/util/EnvoyLog.java @@ -10,7 +10,7 @@ import envoy.client.Config; * Project: envoy-client
* File: EnvoyLogger.java
* Created: 14 Dec 2019
- * + * * @author Leon Hofmeister * @author Kai S. K. Engelbart * @since Envoy v0.2-alpha @@ -18,11 +18,18 @@ import envoy.client.Config; public class EnvoyLog { private static FileHandler fileHandler; - private static ConsoleHandler consoleHandler; + private static StreamHandler consoleHandler; static { + // Remove default console handler + LogManager.getLogManager().reset(); + + // Configure log file File logFile = new File(Config.getInstance().getHomeDirectory(), "log/envoy_user.log"); logFile.getParentFile().mkdirs(); + + // Configure formatting + System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] [%4$-7s] [%3$s] %5$s %n"); SimpleFormatter formatter = new SimpleFormatter(); try { @@ -32,7 +39,7 @@ public class EnvoyLog { } catch (SecurityException | IOException e) { e.printStackTrace(); } - consoleHandler = new ConsoleHandler(); + consoleHandler = new StreamHandler(System.out, formatter); consoleHandler.setLevel(Config.getInstance().getConsoleLevelBarrier()); consoleHandler.setFormatter(formatter); } @@ -41,7 +48,7 @@ public class EnvoyLog { /** * Creates a {@link Logger} with a specified name - * + * * @param name the name of the {@link Logger} to create * @return the created {@link Logger} * @since Envoy v0.2-alpha From 5f3fce1f71052fbae0f56b68cf04492fddabeffd Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 21 Dec 2019 18:29:59 +0100 Subject: [PATCH 2/2] Removed unnecessary log message from LocalDB, logging Sync --- src/main/java/envoy/client/Client.java | 23 +++++++++++++++-------- src/main/java/envoy/client/LocalDB.java | 21 ++++----------------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index be0dd89..cfc894a 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -1,7 +1,9 @@ package envoy.client; +import java.io.StringWriter; import java.util.HashMap; import java.util.Map; +import java.util.logging.Logger; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; @@ -11,6 +13,7 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; +import envoy.client.util.EnvoyLog; import envoy.exception.EnvoyException; import envoy.schema.ObjectFactory; import envoy.schema.Sync; @@ -20,7 +23,7 @@ import envoy.schema.User; * Project: envoy-client
* File: Client.java
* Created: 28 Sep 2019
- * + * * @author Kai S. K. Engelbart * @author Maximilian Käfer * @author Leon Hofmeister @@ -33,10 +36,12 @@ public class Client { private User sender, recipient; private boolean online = false; + private static final Logger logger = EnvoyLog.getLogger(Client.class.getSimpleName()); + /** * Initializes the client. At this state, the client user has yet to be * initialized, which can be done by calling {@link Client#onlineInit(String)}. - * + * * @param config The {@link Config} instance to use in this client * @since Envoy v0.2-alpha */ @@ -44,7 +49,7 @@ public class Client { /** * Enters the online mode by acquiring a user ID from the server. - * + * * @param userName the name of the client user * @throws EnvoyException if the online mode could not be entered or the request * failed for some other reason @@ -88,7 +93,7 @@ public class Client { /** * Returns a {@link User} with a specific id by name. - * + * * @param name - the name of the {@link User} * @return a {@link User} with the specified name * @throws EnvoyException if the server does not return the requested ID @@ -146,7 +151,7 @@ public class Client { * Users:
* Updating UserStatus of all users in LocalDB. (Server sends all users with * their updated UserStatus to the client.)
- * + * * @param userId the id of the {@link Client} who sends the {@link Sync} * @param sync the sync object (yet to be converted from java class to * sync.xml) @@ -163,7 +168,9 @@ public class Client { jc = JAXBContext.newInstance("envoy.schema"); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - m.marshal(sync, System.out); + StringWriter stringWriter = new StringWriter(); + m.marshal(sync, stringWriter); + logger.fine("Sending sync:\n" + stringWriter.toString()); } catch (JAXBException e) { e.printStackTrace(); } @@ -180,7 +187,7 @@ public class Client { /** * Sets the client user which is used to send messages. - * + * * @param sender the client user to set * @since Envoy v0.2-alpha */ @@ -194,7 +201,7 @@ public class Client { /** * Sets the recipient. - * + * * @param recipient the recipient to set * @since Envoy v0.1-alpha */ diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 8c6a40b..dcc8b12 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -1,16 +1,8 @@ package envoy.client; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; +import java.io.*; import java.time.Instant; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.logging.Logger; import javax.xml.datatype.DatatypeConfigurationException; @@ -20,11 +12,8 @@ import envoy.client.event.EventBus; import envoy.client.event.MessageCreationEvent; import envoy.client.util.EnvoyLog; import envoy.exception.EnvoyException; -import envoy.schema.Message; +import envoy.schema.*; import envoy.schema.Message.Metadata.MessageState; -import envoy.schema.ObjectFactory; -import envoy.schema.Sync; -import envoy.schema.User; /** * Project: envoy-client
@@ -249,10 +238,8 @@ public class LocalDB { // Updating UserStatus of all users in LocalDB for (User user : returnSync.getUsers()) for (Chat chat : getChats()) - if (user.getID() == chat.getRecipient().getID()) { + if (user.getID() == chat.getRecipient().getID()) chat.getRecipient().setStatus(user.getStatus()); - logger.info(chat.getRecipient().getStatus().toString()); - } sync.getMessages().clear(); sync.getUsers().clear();