Fixed Logger initialization

Renamed EnvoyLogger to EnvoyLog and moved it to the newl< created
envoy.client.util package.
This commit is contained in:
Kai S. K. Engelbart 2019-12-20 11:59:11 +01:00
parent 7e02217002
commit 88d2e42494
7 changed files with 83 additions and 96 deletions

View File

@ -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;

View File

@ -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.

View File

@ -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: <strong>envoy-client</strong><br>
* File: <strong>EnvoyLogger.java</strong><br>
* Created: <strong>14 Dec 2019</strong><br>
*
* @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); }
}

View File

@ -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;

View File

@ -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,

View File

@ -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

View File

@ -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: <strong>envoy-client</strong><br>
* File: <strong>EnvoyLogger.java</strong><br>
* Created: <strong>14 Dec 2019</strong><br>
*
* @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); }
}