Merge pull request #71 from informatik-ag-ngl/f/improveeed_logging

Improved logging
This commit is contained in:
Kai S. K. Engelbart 2019-12-21 18:30:51 +01:00 committed by GitHub
commit 27dc78cfe8
5 changed files with 35 additions and 33 deletions

View File

@ -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: <strong>envoy-client</strong><br>
* File: <strong>Client.java</strong><br>
* Created: <strong>28 Sep 2019</strong><br>
*
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;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: <br>
* Updating UserStatus of all users in LocalDB. (Server sends all users with
* their updated UserStatus to the client.) <br>
*
*
* @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
*/

View File

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

View File

@ -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: <strong>envoy-client</strong><br>
@ -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();

View File

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

View File

@ -10,7 +10,7 @@ import envoy.client.Config;
* Project: <strong>envoy-client</strong><br>
* File: <strong>EnvoyLogger.java</strong><br>
* Created: <strong>14 Dec 2019</strong><br>
*
*
* @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