Merge pull request #58 from informatik-ag-ngl/f/logger

Advanced Logger with the ability to write logs to a file
This commit is contained in:
delvh 2019-12-20 12:52:13 +01:00 committed by GitHub
commit 31195cbd23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 283 additions and 229 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
/target/
/localDB/
/themes.ser
/log/
/themes.ser

View File

@ -18,6 +18,7 @@ import javax.xml.datatype.DatatypeFactory;
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;
@ -48,7 +49,7 @@ public class LocalDB {
private Sync sync = objectFactory.createSync();
private Sync readMessages = objectFactory.createSync();
private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName());
private static final Logger logger = EnvoyLog.getLogger(LocalDB.class.getSimpleName());
/**
* Constructs an empty local database. To serialize any chats to the file

View File

@ -88,7 +88,7 @@ public class Settings {
/**
* Updates the preferences when the save button is clicked.
*
* @throws IOException
* @throws IOException if saving was not successful
* @since Envoy v0.2-alpha
*/
public void save() throws IOException {
@ -105,7 +105,7 @@ public class Settings {
/**
* adds new theme to the theme map and sets current theme to the new theme.
*
* @param theme
* @param theme the theme to add
* @since Envoy v0.2-alpha
*/
public void addNewThemeToMap(Theme theme) {
@ -114,7 +114,7 @@ public class Settings {
}
/**
* @return {@link currentTheme}
* @return the name of the current theme
* @since Envoy v0.2-alpha
*/
public String getCurrentTheme() { return currentTheme; }
@ -122,7 +122,7 @@ public class Settings {
/**
* Sets the currentTheme
*
* @param themeName
* @param themeName the name of the new current theme
* @since Envoy v0.2-alpha
*/
public void setCurrentTheme(String themeName) { currentTheme = themeName; }
@ -144,15 +144,13 @@ public class Settings {
public void setEnterToSend(boolean enterToSend) { this.enterToSend = enterToSend; }
/**
* @return {@link themes} map
* @return the map of all themes by name and colorContent
* @since Envoy v0.2-alpha
*/
public Map<String, Theme> getThemes() { return themes; }
/**
* Sets {@link themes}
*
* @param themes
* @param themes the the the the
* @since Envoy v0.2-alpha
*/
public void setThemes(Map<String, Theme> themes) { this.themes = themes; }

View File

@ -6,6 +6,7 @@ package envoy.client.event;
* Created: <strong>04.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @param <T> the type of our Event
* @since Envoy v0.2-alpha
*/
public interface Event<T> {

View File

@ -30,8 +30,10 @@ import envoy.client.Client;
import envoy.client.Config;
import envoy.client.LocalDB;
import envoy.client.Settings;
import envoy.client.util.EnvoyLog;
import envoy.client.event.EventBus;
import envoy.client.event.ThemeChangeEvent;
import envoy.schema.Message;
import envoy.schema.User;
@ -66,7 +68,7 @@ public class ChatWindow extends JFrame {
private static int space = 4;
private static final Logger logger = Logger.getLogger(ChatWindow.class.getSimpleName());
private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class.getSimpleName());
public ChatWindow(Client client, LocalDB localDB) {
this.client = client;
@ -167,9 +169,9 @@ public class ChatWindow extends JFrame {
settingsButton.addActionListener((evt) -> {
try {
SettingsScreen.open();
new SettingsScreen().setVisible(true);
changeChatWindowColors(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
} catch (Exception e) {
SettingsScreen.open();
logger.log(Level.WARNING, "An error occured while opening the settings screen", e);
e.printStackTrace();
}
@ -236,7 +238,8 @@ public class ChatWindow extends JFrame {
/**
* Used to immediately reload the ChatWindow when settings were changed.
*
*
* @param theme the theme to change colors into
* @since Envoy v0.1-alpha
*/
private void changeChatWindowColors(Theme theme) {

View File

@ -11,6 +11,7 @@ import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
@ -25,8 +26,8 @@ import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import envoy.client.LocalDB;
import envoy.client.Settings;
import envoy.client.util.EnvoyLog;
import envoy.client.event.EventBus;
import envoy.client.event.ThemeChangeEvent;
@ -56,44 +57,23 @@ public class SettingsScreen extends JDialog {
private GridBagConstraints gbc_themeContent = new GridBagConstraints();
private Theme selectedTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme());
private JButton createNewThemeButton = new JButton("Create New Theme");
private JPanel colorsPanel = new JPanel();
private JButton okButton = new JButton("Save");
private JButton cancelButton = new JButton("Cancel");
private JButton createNewThemeButton = new JButton("Create New Theme");
private JPanel colorsPanel = new JPanel();
private JButton okButton = new JButton("Save");
private JButton cancelButton = new JButton("Cancel");
private static int space = 5;
private Theme temporaryTheme;
private Theme temporaryTheme, selectedTheme;
private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName());
private static SettingsScreen settingsScreen;
// TODO: Add a JPanel with all the Information necessary:
// change (Picture,Username, Email, Password) and toggle(light/dark mode,
// "ctrl+enter"/"enter"
// to send a message directly)
/**
* Opens the settings screen.<br>
*
* @since Envoy v0.1-alpha
*/
public static void open() {
settingsScreen = new SettingsScreen();
settingsScreen.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
settingsScreen.setModal(true);
settingsScreen.setVisible(true);
}
private static final Logger logger = EnvoyLog.getLogger(SettingsScreen.class.getSimpleName());
/**
* Builds the settings screen.
*
* @since Envoy v0.1-alpha
*/
private SettingsScreen() {
public SettingsScreen() {
logger.info(Settings.getInstance().getCurrentTheme());
setBounds(10, 10, 450, 650);
@ -122,7 +102,7 @@ public class SettingsScreen extends JDialog {
@SuppressWarnings("unchecked")
final JList<String> selectedOption = (JList<String>) listSelectionEvent.getSource();
final String option = selectedOption.getSelectedValue();
System.out.println(option);
logger.log(Level.FINEST, option);
switch (option) {
case "Color Themes":
@ -173,7 +153,7 @@ public class SettingsScreen extends JDialog {
@Override
public void itemStateChanged(ItemEvent e) {
String selectedValue = (String) themes.getSelectedItem();
System.out.println(selectedValue);
logger.log(Level.FINEST, selectedValue);
selectedTheme = Settings.getInstance().getThemes().get(selectedValue);
}
});
@ -229,7 +209,7 @@ public class SettingsScreen extends JDialog {
createNewThemeButton.addActionListener((evt) -> {
try {
String s = JOptionPane.showInputDialog("Enter a name for the new theme");
System.out.println(s);
logger.log(Level.FINEST, s);
Settings.getInstance()
.addNewThemeToMap(new Theme(s, temporaryTheme.getBackgroundColor(), temporaryTheme.getCellColor(),
temporaryTheme.getInteractableForegroundColor(), temporaryTheme.getInteractableBackgroundColor(),
@ -295,7 +275,7 @@ public class SettingsScreen extends JDialog {
Settings.getInstance().setEnterToSend(Settings.getInstance().isEnterToSend());// still temporary
Settings.getInstance().setCurrentTheme(selectedTheme.getThemeName());
System.out.println(selectedTheme.getThemeName());
logger.log(Level.FINER, selectedTheme.getThemeName());
final Theme currentTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme());
changeSettingsScreenColors(currentTheme);
@ -306,13 +286,18 @@ public class SettingsScreen extends JDialog {
revalidate();
repaint();
} catch (Exception e) {
logger.info("Something went wrong when changing the setting");
settingsScreen.dispose();
logger.warning("Something went wrong when changing the setting");
JOptionPane.showMessageDialog(this, "Something went wrong when changing the setting");
dispose();
}
});
}
}
changeSettingsScreenColors(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModal(true);
}
private void changeSettingsScreenColors(Theme theme) {
@ -447,8 +432,7 @@ public class SettingsScreen extends JDialog {
try {
Color newColor = JColorChooser.showDialog(null, "Choose a color", color);
if (newColor.getRGB() != color.getRGB()) {
System.out.println("New Color");
System.out.println(color.getRGB());
logger.log(Level.FINEST, "New Color: " + String.valueOf(color.getRGB()));
// TODO: When Theme changed in same settings screen, color variable doesnt
// update.
temporaryTheme.setColor(yIndex, newColor);

View File

@ -11,6 +11,7 @@ import javax.swing.JOptionPane;
import envoy.client.Client;
import envoy.client.Config;
import envoy.client.LocalDB;
import envoy.client.util.EnvoyLog;
import envoy.exception.EnvoyException;
import envoy.schema.User;
@ -28,11 +29,9 @@ import envoy.schema.User;
*/
public class Startup {
private static final Logger logger = Logger.getLogger(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();
try {
@ -57,7 +56,7 @@ public class Startup {
logger.severe("User name is not set or empty. Exiting...");
System.exit(1);
}
// Initialize the local database
LocalDB localDB;
try {
@ -81,19 +80,18 @@ public class Startup {
// Try entering offline mode
localDB.loadUsers();
User clientUser = localDB.getUsers().get(userName);
if(clientUser == null)
throw new EnvoyException("Could not enter offline mode: user name unknown");
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
client.setSender(clientUser);
} catch(Exception e2) {
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, e2.toString(), "Client error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
return;
}
}
// Set client user in local database
localDB.setUser(client.getSender());
// Initialize chats in local database
try {
localDB.initializeDBFile();
@ -105,13 +103,12 @@ public class Startup {
"Local DB error",
JOptionPane.WARNING_MESSAGE);
}
logger.info("Client user ID: " + client.getSender().getID());
// Save all users to the local database
if(client.isOnline())
localDB.setUsers(client.getUsers());
if (client.isOnline()) localDB.setUsers(client.getUsers());
EventQueue.invokeLater(() -> {
try {
ChatWindow chatWindow = new ChatWindow(client, localDB);
@ -119,7 +116,7 @@ public class Startup {
try {
new StatusTrayIcon(chatWindow).show();
// If the tray icon is supported, hide the chat window on close
chatWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
} catch (EnvoyException e) {

View File

@ -1,161 +1,161 @@
package envoy.client.ui;
import java.awt.Color;
import java.io.Serializable;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>Theme.java</strong><br>
* Created: <strong>23 Nov 2019</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy v0.2-alpha
*/
public class Theme implements Serializable {
private static final long serialVersionUID = 141727847527060352L;
private String themeName;
private Color backgroundColor;
private Color cellColor;
private Color interactableBackgroundColor;
private Color userNameColor;
private Color interactableForegroundColor;
private Color messageColorChat;
private Color dateColorChat;
private Color selectionColor;
private Color typingMessageColor;
public Theme(String themeName, Color backgroundColor, Color cellColor, Color interactableForegroundColor, Color interactableBackgroundColor,
Color messageColorChat, Color dateColorChat, Color selectionColor, Color typingMessageColor, Color userNameColor) {
this.themeName = themeName;
this.backgroundColor = backgroundColor;
this.cellColor = cellColor;
this.interactableForegroundColor = interactableForegroundColor;
this.interactableBackgroundColor = interactableBackgroundColor;
this.messageColorChat = messageColorChat;
this.dateColorChat = dateColorChat;
this.selectionColor = selectionColor;
this.typingMessageColor = typingMessageColor;
this.userNameColor = userNameColor;
}
public Theme(String name, Theme other) {
this(name, other.backgroundColor, other.cellColor, other.interactableForegroundColor,
other.interactableBackgroundColor, other.messageColorChat, other.dateColorChat, other.selectionColor,
other.typingMessageColor, other.userNameColor);
}
/**
* @return name of the theme
* @since Envoy v0.2-alpha
*/
public String getThemeName() { return themeName; }
/**
* @return interactableForegroundColor
* @since Envoy v0.2-alpha
*/
public Color getInteractableForegroundColor() { return interactableForegroundColor; }
/**
* @return messageColorChat
* @since Envoy v0.2-alpha
*/
public Color getMessageColorChat() { return messageColorChat; }
/**
* @return dateColorChat
* @since Envoy v0.2-alpha
*/
public Color getDateColorChat() { return dateColorChat; }
/**
* @return selectionColor
* @since Envoy v0.2-alpha
*/
public Color getSelectionColor() { return selectionColor; }
/**
* @return typingMessageColor
* @since Envoy v0.2-alpha
*/
public Color getTypingMessageColor() { return typingMessageColor; }
/**
* @return backgroundColor
* @since Envoy v0.2-alpha
*/
public Color getBackgroundColor() { return backgroundColor; }
/**
* @return cellColor
* @since Envoy v0.2-alpha
*/
public Color getCellColor() { return cellColor; }
/**
* @return interactableBackgroundColor
* @since Envoy v0.2-alpha
*/
public Color getInteractableBackgroundColor() { return interactableBackgroundColor; }
/**
* @return userNameColor
* @since Envoy v0.2-alpha
*/
public Color getUserNameColor() { return userNameColor; }
/**
* Sets the a specific {@link Color} in this theme to a new {@link Color}
*
* @param index - index of the color </br>
* 0 = backgroundColor </br>
* 1 = cellColor </br>
* 2 = interactableForegroundColor </br>
* 3 = interactableBackgroundColor </br>
* 4 = messageColorChat </br>
* 5 = dateColorChat </br>
* 6 = selectionColor </br>
* 7 = typingMessageColor </br>
* 8 = userNameColor </br>
* </br>
*
* @param newColor - new {@link Color} to be set
* @since Envoy 0.2-alpha
*/
public void setColor(int index, Color newColor) {
switch (index) {
case 0:
this.backgroundColor = newColor;
break;
case 1:
this.cellColor = newColor;
break;
case 2:
this.interactableForegroundColor = newColor;
break;
case 3:
this.interactableBackgroundColor = newColor;
break;
case 4:
this.messageColorChat = newColor;
break;
case 5:
this.dateColorChat = newColor;
break;
case 6:
this.selectionColor = newColor;
break;
case 7:
this.typingMessageColor = newColor;
break;
case 8:
this.userNameColor = newColor;
break;
}
}
}
package envoy.client.ui;
import java.awt.Color;
import java.io.Serializable;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>Theme.java</strong><br>
* Created: <strong>23 Nov 2019</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy v0.2-alpha
*/
public class Theme implements Serializable {
private static final long serialVersionUID = 141727847527060352L;
private String themeName;
private Color backgroundColor;
private Color cellColor;
private Color interactableBackgroundColor;
private Color userNameColor;
private Color interactableForegroundColor;
private Color messageColorChat;
private Color dateColorChat;
private Color selectionColor;
private Color typingMessageColor;
public Theme(String themeName, Color backgroundColor, Color cellColor, Color interactableForegroundColor, Color interactableBackgroundColor,
Color messageColorChat, Color dateColorChat, Color selectionColor, Color typingMessageColor, Color userNameColor) {
this.themeName = themeName;
this.backgroundColor = backgroundColor;
this.cellColor = cellColor;
this.interactableForegroundColor = interactableForegroundColor;
this.interactableBackgroundColor = interactableBackgroundColor;
this.messageColorChat = messageColorChat;
this.dateColorChat = dateColorChat;
this.selectionColor = selectionColor;
this.typingMessageColor = typingMessageColor;
this.userNameColor = userNameColor;
}
public Theme(String name, Theme other) {
this(name, other.backgroundColor, other.cellColor, other.interactableForegroundColor,
other.interactableBackgroundColor, other.messageColorChat, other.dateColorChat, other.selectionColor,
other.typingMessageColor, other.userNameColor);
}
/**
* @return name of the theme
* @since Envoy v0.2-alpha
*/
public String getThemeName() { return themeName; }
/**
* @return interactableForegroundColor
* @since Envoy v0.2-alpha
*/
public Color getInteractableForegroundColor() { return interactableForegroundColor; }
/**
* @return messageColorChat
* @since Envoy v0.2-alpha
*/
public Color getMessageColorChat() { return messageColorChat; }
/**
* @return dateColorChat
* @since Envoy v0.2-alpha
*/
public Color getDateColorChat() { return dateColorChat; }
/**
* @return selectionColor
* @since Envoy v0.2-alpha
*/
public Color getSelectionColor() { return selectionColor; }
/**
* @return typingMessageColor
* @since Envoy v0.2-alpha
*/
public Color getTypingMessageColor() { return typingMessageColor; }
/**
* @return backgroundColor
* @since Envoy v0.2-alpha
*/
public Color getBackgroundColor() { return backgroundColor; }
/**
* @return cellColor
* @since Envoy v0.2-alpha
*/
public Color getCellColor() { return cellColor; }
/**
* @return interactableBackgroundColor
* @since Envoy v0.2-alpha
*/
public Color getInteractableBackgroundColor() { return interactableBackgroundColor; }
/**
* @return userNameColor
* @since Envoy v0.2-alpha
*/
public Color getUserNameColor() { return userNameColor; }
/**
* Sets the a specific {@link Color} in this theme to a new {@link Color}
*
* @param index - index of the color </br>
* 0 = backgroundColor </br>
* 1 = cellColor </br>
* 2 = interactableForegroundColor </br>
* 3 = interactableBackgroundColor </br>
* 4 = messageColorChat </br>
* 5 = dateColorChat </br>
* 6 = selectionColor </br>
* 7 = typingMessageColor </br>
* 8 = userNameColor </br>
* </br>
*
* @param newColor - new {@link Color} to be set
* @since Envoy 0.2-alpha
*/
public void setColor(int index, Color newColor) {
switch (index) {
case 0:
this.backgroundColor = newColor;
break;
case 1:
this.cellColor = newColor;
break;
case 2:
this.interactableForegroundColor = newColor;
break;
case 3:
this.interactableBackgroundColor = newColor;
break;
case 4:
this.messageColorChat = newColor;
break;
case 5:
this.dateColorChat = newColor;
break;
case 6:
this.selectionColor = newColor;
break;
case 7:
this.typingMessageColor = newColor;
break;
case 8:
this.userNameColor = newColor;
break;
}
}
}

View File

@ -12,8 +12,8 @@ import envoy.schema.User;
import envoy.schema.User.UserStatus;
/**
* Defines how the {@code UserList} is displayed.
*
* Defines how the {@code UserList} is displayed.<br>
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>UserListRenderer.java</strong><br>
* Created: <strong>12 Oct 2019</strong><br>
@ -26,7 +26,6 @@ public class UserListRenderer extends JLabel implements ListCellRenderer<User> {
private static final long serialVersionUID = 5164417379767181198L;
@SuppressWarnings("incomplete-switch")
@Override
public Component getListCellRendererComponent(JList<? extends User> list, User value, int index, boolean isSelected, boolean cellHasFocus) {
if (isSelected) {

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); }
}