Skip to content

Commit

Permalink
Refactored logging framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward-Knight committed Feb 2, 2016
1 parent ad2ceb7 commit 546b5a9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 66 deletions.
100 changes: 63 additions & 37 deletions src/main/java/chat/haver/server/Logger.java
Original file line number Diff line number Diff line change
@@ -1,72 +1,98 @@
package chat.haver.server;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;

/**
* Basic logging class
*/
public class Logger {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final StringWriter STRING_WRITER = new StringWriter();
private static final PrintWriter PRINT_WRITER = new PrintWriter(STRING_WRITER);

public static void printStackTrace(Exception e) {
e.printStackTrace(PRINT_WRITER);
for (String line : STRING_WRITER.toString().split("\\r?\\n")) {
severe(line);
}
STRING_WRITER.flush();
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

/**
* Logs a {@link Level}.INFO level message.
*
* @param message to be logged
*/
public static void info(final String message) {
out(format(message, Level.INFO));
}

public static void info(String message) {
out(format(message, "info"));
/**
* Logs a {@link Level}.WARNING level message.
*
* @param message to be logged
*/
public static void warning(final String message) {
err(format(message, Level.WARNING));
}

public static void warning(String message) {
err(format(message, "warning"));
/**
* Logs a {@link Level}.SEVERE level message.
*
* @param e the exception to log
*/
public static void severe(final Exception e) {
err(format(formatStackTrace(e), Level.SEVERE));
}

public static void severe(String message) {
err(format(message, "severe"));
/**
* Helper method to format a stack trace for logging
*
* @param e the exception to extract the stack trace from
* @return a stack trace formatted identically to {@link Exception#printStackTrace()}
*/
private static String formatStackTrace(final Exception e) {
StringBuilder sb = new StringBuilder(e.getClass().getCanonicalName() + ": " + e.getMessage());
for(StackTraceElement ste : e.getStackTrace()) {
sb.append("\n\tat ").append(ste.toString());
}
return sb.toString();
}

/**
* Formats the message to be printed to log
* @param message To be logged
* @param type The type of message: info, warning, or severe
* @return Formatted message
*
* @param message to be logged
* @param level the logging level
* @return formatted message
*/
private static String format(String message, String type) {
Date date = new Date();
StackTraceElement ste = Thread.currentThread().getStackTrace()[3];
String[] arr = ste.getClassName().split("\\.");
String classDetails = arr[arr.length - 1] + ":" + ste.getMethodName();
String dateString = dateFormat.format(date);
if (type.equalsIgnoreCase("info")) {
return "[" + dateString + "] " + type.toUpperCase() + " " + message;
} else {
return "[" + dateString + "] " + type.toUpperCase() + " (" + classDetails + ") " + message;
}
private static String format(final String message, final Level level) {
return '[' + DATE_FORMAT.format(new Date()) + "] " + level + ' ' + message;
}

/**
* Prints to standard out
* TODO: Use ENVIRONMENT to determine whether to print
* Prints to standard out and logs.
*
* @param message to output
*/
private static void out(String message) {
private static void out(final String message) {
System.out.println(message);
if(Main.ENVIRONMENT == Main.Environment.PRODUCTION) {
log(message);
}
}

/**
* Prints to standard error
* TODO: Use ENVIRONMENT to determine whether to print
* Prints to standard error and logs.
*
* @param message to output
*/
private static void err(String message) {
private static void err(final String message) {
System.err.println(message);
if(Main.ENVIRONMENT == Main.Environment.PRODUCTION) {
log(message);
}
}

/**
* Helper method to log messages to a logfile.
*
* @param message to be logged
*/
private static void log(final String message) {
// TODO implement log file
}
}
32 changes: 6 additions & 26 deletions src/main/java/chat/haver/server/Main.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
package chat.haver.server;

import java.net.UnknownHostException;

public class Main {
@Deprecated
public static final boolean DEBUG = true; // To be removed and replaced with Environment enum
public static final Environment ENVIRONMENT = Environment.DEVELOPMENT;

private static String host = "127.0.0.1";
private static int port = 8080;

public enum Environment {
DEVELOPMENT("development"),
TESTING("testing"),
PRODUCTION("production");

public final String env;

Environment(final String env) {
this.env = env;
}

@Override
public String toString() {
return env;
}
}
public enum Environment {PRODUCTION, DEVELOPMENT}

/**
* Empty private constructor to prevent creation of Main.
Expand All @@ -39,13 +20,12 @@ private Main() {
* @param args Command line arguments.
*/
public static void main(final String[] args) {
parseArgs(args);
try {
Router router = new Router(host, port);
router.start();
Logger.info("Hosting new server on: " + router.getAddress());
} catch (UnknownHostException e) {
Logger.severe("Invalid host: " + host + ':' + port);
parseArgs(args);
new Router(host, port).start();
Logger.info("Hosting new server on: " + host + ':' + port);
} catch (Exception e) {
Logger.severe(e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/chat/haver/server/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void addClient(final WebSocket conn, final Client client) {
*/
private String generateName() {
if(freeNames.size() == 0) { // TODO Actually handle this situation somehow
Logger.severe("ROOM IS OVER MAXIMUM LIMIT, RIP IN KILL");
Logger.severe(new Exception("ROOM IS OVER MAXIMUM LIMIT, RIP IN KILL"));
System.exit(-2011);
}
int index = RANDOM.nextInt(freeNames.size());
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/chat/haver/server/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public void onClose(final WebSocket conn, final int code, final String reason, f

@Override
public void onError(final WebSocket conn, final Exception ex) {
Logger.severe("Websocket error: " + ex.getMessage());
Logger.printStackTrace(ex);
Logger.severe(ex);
if (conn != null) {
// some errors like port binding failed may not be assignable to a specific websocket
conn.close();
Expand Down

0 comments on commit 546b5a9

Please sign in to comment.