From 88d2e42494accf5bbb8c9be466ef9521d80257c9 Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 20 Dec 2019 11:59:11 +0100 Subject: [PATCH] Fixed Logger initialization Renamed EnvoyLogger to EnvoyLog and moved it to the newl< created envoy.client.util package. --- src/main/java/envoy/client/Client.java | 6 +- src/main/java/envoy/client/LocalDB.java | 4 +- .../java/envoy/client/event/EnvoyLogger.java | 83 ------------------- src/main/java/envoy/client/ui/ChatWindow.java | 5 +- .../java/envoy/client/ui/SettingsScreen.java | 4 +- src/main/java/envoy/client/ui/Startup.java | 7 +- src/main/java/envoy/client/util/EnvoyLog.java | 70 ++++++++++++++++ 7 files changed, 83 insertions(+), 96 deletions(-) delete mode 100644 src/main/java/envoy/client/event/EnvoyLogger.java create mode 100644 src/main/java/envoy/client/util/EnvoyLog.java diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index bd1555a..1a8ce34 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -1,5 +1,7 @@ package envoy.client; +import java.util.logging.Logger; + import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; @@ -8,7 +10,7 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; -import envoy.client.event.EnvoyLogger; +import envoy.client.util.EnvoyLog; import envoy.schema.ObjectFactory; import envoy.schema.Sync; import envoy.schema.User; @@ -29,7 +31,7 @@ public class Client { private Config config; private User sender, recipient; - private static final EnvoyLogger logger = new EnvoyLogger(Client.class.getSimpleName()); + private static final Logger logger = EnvoyLog.getLogger(Client.class.getSimpleName()); public Client(Config config, String username) { this.config = config; diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 51e2eb1..b09c808 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -14,9 +14,9 @@ import java.util.logging.Logger; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; -import envoy.client.event.EnvoyLogger; 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.Message.Metadata.MessageState; @@ -45,7 +45,7 @@ public class LocalDB { private Sync sync = objectFactory.createSync(); private Sync readMessages = objectFactory.createSync(); - private static final Logger logger = new EnvoyLogger(LocalDB.class.getSimpleName()); + private static final Logger logger = EnvoyLog.getLogger(LocalDB.class.getSimpleName()); /** * Constructs an empty local database. diff --git a/src/main/java/envoy/client/event/EnvoyLogger.java b/src/main/java/envoy/client/event/EnvoyLogger.java deleted file mode 100644 index cbc2c88..0000000 --- a/src/main/java/envoy/client/event/EnvoyLogger.java +++ /dev/null @@ -1,83 +0,0 @@ -package envoy.client.event; - -import java.io.IOException; -import java.util.logging.ConsoleHandler; -import java.util.logging.FileHandler; -import java.util.logging.Level; -import java.util.logging.LogRecord; -import java.util.logging.Logger; -import java.util.logging.SimpleFormatter; - -/** - * Project: envoy-client
- * File: EnvoyLogger.java
- * Created: 14 Dec 2019
- * - * @author Leon Hofmeister - * @since Envoy v0.2-alpha - */ -public class EnvoyLogger extends Logger { - - private Logger logger; - private Level fileLevelBarrier = Level.CONFIG; - - public EnvoyLogger(String name) { - super(name, null); - try { - SimpleFormatter formatter = new SimpleFormatter(); - FileHandler fh = new FileHandler("envoy_user.log"); - fh.setLevel(fileLevelBarrier); - fh.setFormatter(formatter); - ConsoleHandler ch = new ConsoleHandler(); - ch.setLevel(Level.FINEST); - ch.setFormatter(formatter); - logger.addHandler(fh); - logger.addHandler(ch); - } catch (IOException | SecurityException e) { - e.printStackTrace(); - this.log(Level.FINE, "Ironically, the logger encountered an error while initialising. That certainly needs to be logged :)"); - } - } - - /** - * Logs a message. If the problem severity is above the FileLevel-barrier, then - * the log entry will be written to a file. Regardless of problem severity, - * everything will be printed to the console. - * - * @param level the problem severity - * @param msg the message to be written in the log - * @since Envoy v0.2-alpha - */ - @Override - public void log(Level level, String msg) { - LogRecord lr = new LogRecord(level, msg); - logger.log(lr); - } - - /** - * Logs a message. If the problem severity is above the {@code FileLevel} - * barrier, then the log entry will be written to a file. - * Regardless of problem severity, everything will be printed to the console. - * - * @param logRecord the LogRecord (Level and String) to be treated by the - * Logger. - * @since Envoy v0.2-alpha - */ - @Override - public void log(LogRecord logRecord) { logger.log(logRecord); } - - /** - * @return the fileLevelBarrier: The current barrier for writing logs to a file. - * @since Envoy v0.2-alpha - */ - public Level getFileLevelBarrier() { return fileLevelBarrier; } - - /** - * @param fileLevelBarrier the severity below which logRecords will be written - * only to the console. At or above they'll also be - * logged in a file. Can be written either in Digits - * from 0 - 1000 or with the according name of the level - * @since Envoy v0.2-alpha - */ - public void setFileLevel(String fileLevelBarrier) { this.fileLevelBarrier = Level.parse(fileLevelBarrier); } -} diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 8761b74..cbdd7f5 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -12,6 +12,7 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.util.logging.Level; +import java.util.logging.Logger; import javax.swing.DefaultListModel; import javax.swing.JButton; @@ -32,7 +33,7 @@ import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; import envoy.client.Settings; -import envoy.client.event.EnvoyLogger; +import envoy.client.util.EnvoyLog; import envoy.schema.Message; import envoy.schema.Sync; import envoy.schema.User; @@ -68,7 +69,7 @@ public class ChatWindow extends JFrame { private static int space = 4; - private static final EnvoyLogger logger = new EnvoyLogger(ChatWindow.class.getSimpleName()); + private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class.getSimpleName()); public ChatWindow(Client client, LocalDB localDB) { this.client = client; diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index c9fea6c..490985a 100644 --- a/src/main/java/envoy/client/ui/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/SettingsScreen.java @@ -27,7 +27,7 @@ import javax.swing.JTextPane; import javax.swing.ListSelectionModel; import envoy.client.Settings; -import envoy.client.event.EnvoyLogger; +import envoy.client.util.EnvoyLog; /** * This class provides the GUI to change the user specific settings. @@ -64,7 +64,7 @@ public class SettingsScreen extends JDialog { private Theme selectedTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); private Theme temporaryTheme; - private static final Logger logger = new EnvoyLogger(SettingsScreen.class.getSimpleName()); + private static final Logger logger = EnvoyLog.getLogger(SettingsScreen.class.getSimpleName()); // TODO: Add a JPanel with all the Information necessary: // change (Picture,Username, Email, Password) and toggle(light/dark mode, diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 33e5c78..bbfedb6 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -3,7 +3,6 @@ package envoy.client.ui; import java.awt.EventQueue; import java.io.IOException; import java.util.Properties; -import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; @@ -12,7 +11,7 @@ import envoy.client.Client; import envoy.client.Config; import envoy.client.LocalDB; import envoy.client.Settings; -import envoy.client.event.EnvoyLogger; +import envoy.client.util.EnvoyLog; import envoy.exception.EnvoyException; /** @@ -29,11 +28,9 @@ import envoy.exception.EnvoyException; */ public class Startup { - private static final Logger logger = new EnvoyLogger(Startup.class.getSimpleName()); + private static final Logger logger = EnvoyLog.getLogger(Startup.class.getSimpleName()); public static void main(String[] args) { - logger.setLevel(Level.ALL); - Config config = Config.getInstance(); // Load the configuration from client.properties first diff --git a/src/main/java/envoy/client/util/EnvoyLog.java b/src/main/java/envoy/client/util/EnvoyLog.java new file mode 100644 index 0000000..db4e03c --- /dev/null +++ b/src/main/java/envoy/client/util/EnvoyLog.java @@ -0,0 +1,70 @@ +package envoy.client.util; + +import java.io.File; +import java.io.IOException; +import java.util.logging.ConsoleHandler; +import java.util.logging.FileHandler; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; + +/** + * Project: envoy-client
+ * File: EnvoyLogger.java
+ * Created: 14 Dec 2019
+ * + * @author Leon Hofmeister + * @since Envoy v0.2-alpha + */ +public class EnvoyLog { + + private static Level fileLevelBarrier = Level.CONFIG; + + private EnvoyLog() {} + + /** + * Creates a {@link Logger} with a specified name + * @param name the name of the {@link Logger} to create + * @return the created {@link Logger} + */ + public static Logger getLogger(String name) { + // Get a logger with the specified name + Logger logger = Logger.getLogger(name); + + final String logPath = "log/envoy_user.log"; + new File(logPath).getParentFile().mkdirs(); + + SimpleFormatter formatter = new SimpleFormatter(); + + try { + FileHandler fh = new FileHandler(logPath); + fh.setLevel(fileLevelBarrier); + fh.setFormatter(formatter); + logger.addHandler(fh); + } catch (SecurityException | IOException e) { + e.printStackTrace(); + } + + ConsoleHandler ch = new ConsoleHandler(); + ch.setLevel(Level.FINEST); + ch.setFormatter(formatter); + logger.addHandler(ch); + + return logger; + } + + /** + * @return the fileLevelBarrier: The current barrier for writing logs to a file. + * @since Envoy v0.2-alpha + */ + public static Level getFileLevelBarrier() { return fileLevelBarrier; } + + /** + * @param fileLevelBarrier the severity below which logRecords will be written + * only to the console. At or above they'll also be + * logged in a file. Can be written either in Digits + * from 0 - 1000 or with the according name of the level + * @since Envoy v0.2-alpha + */ + public static void setFileLevel(String fileLevelBarrier) { EnvoyLog.fileLevelBarrier = Level.parse(fileLevelBarrier); } +}