Skip to content
This repository has been archived by the owner on Aug 6, 2018. It is now read-only.

Commit

Permalink
Merge pull request #118 from UCY-Team/logging-round-trip
Browse files Browse the repository at this point in the history
#74 More magic
  • Loading branch information
Mattihew committed May 11, 2017
2 parents 42589fb + 8b08d44 commit 532cc41
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 27 deletions.
64 changes: 48 additions & 16 deletions ymca/src/main/java/endpoints/LoggingEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Level;

import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
Expand All @@ -14,6 +15,8 @@
import javax.websocket.Session;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import models.LogMessage;

Expand All @@ -37,25 +40,52 @@ public void onOpen(final Session session, final EndpointConfig config)
session.addMessageHandler(new MessageHandler.Whole<String>()
{
@Override
public void onMessage(final String text)
public void onMessage(final String jsonString)
{
LoggingEndpoint.this.sessionId = text;
Set<LoggingEndpoint> set = LoggingEndpoint.endpoints.get(LoggingEndpoint.this.sessionId);

if (null == set)
{
set = new HashSet<>();
LoggingEndpoint.endpoints.put(LoggingEndpoint.this.sessionId, set);
}

set.add(LoggingEndpoint.this);

final SortedSet<LogMessage> messageQueue = LoggingEndpoint.messages.get(text);
JSONObject jsonObject = new JSONObject(jsonString);

if (null != messageQueue && !messageQueue.isEmpty())
if (jsonObject.has("httpSessionId"))
{
LoggingEndpoint.logToClient(LoggingEndpoint.this, new JSONArray(messageQueue).toString());
LoggingEndpoint.this.sessionId = jsonObject.getString("httpSessionId");
Set<LoggingEndpoint> set = LoggingEndpoint.endpoints.get(LoggingEndpoint.this.sessionId);

if (null == set)
{
set = new HashSet<>();
LoggingEndpoint.endpoints.put(LoggingEndpoint.this.sessionId, set);
}

set.add(LoggingEndpoint.this);

final SortedSet<LogMessage> messageQueue = LoggingEndpoint.messages.get(LoggingEndpoint.this.sessionId);

if (null != messageQueue && !messageQueue.isEmpty())
{
LoggingEndpoint.logToClient(LoggingEndpoint.this, new JSONArray(messageQueue).toString());
}
}
else
{
try
{
jsonObject.put("sessionId", LoggingEndpoint.this.sessionId);
LogMessage logMessage = new LogMessage(jsonObject);
queueMessage(logMessage);

for (LoggingEndpoint endpoint : LoggingEndpoint.endpoints.get(LoggingEndpoint.this.sessionId))
{
if (!LoggingEndpoint.this.equals(endpoint))
{
logToClient(endpoint, logMessage.toJSONString());
}
}
}
catch (JSONException ex)
{
log(new LogMessage(Level.WARNING, ex.getMessage()));
}
}

}
});
}
Expand Down Expand Up @@ -125,8 +155,10 @@ public static void log(final LogMessage logMessage, final boolean queue)
}
else
{
System.err.println(String.format("No LoggingEndpoints regestered for session '%s'", sessionId));
System.err.println(String.format("No LoggingEndpoints registered for session '%s'", sessionId));
System.err.println(logMessage);
LoggingEndpoint.log(new LogMessage(Level.WARNING,
String.format("No LoggingEndpoints registered for session '%s'", sessionId)));
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions ymca/src/main/java/models/LogMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.commons.lang3.builder.CompareToBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;

Expand Down Expand Up @@ -70,6 +71,14 @@ public LogMessage(final String sessionId, final Level level, final String messag
this(sessionId, level, message, TimeFactory.currentTimeMillis());
}

public LogMessage(final JSONObject jsonObject) throws JSONException
{
this.sessionId = jsonObject.optString("sessionId", null);
this.level = Level.parse(jsonObject.optString("level", "FINE"));
this.message = jsonObject.getString("message");
this.timestamp = jsonObject.optLong("timestamp", TimeFactory.currentTimeMillis());
}

/**
* Constructor
* Ideally other constructors should be used
Expand Down
25 changes: 15 additions & 10 deletions ymca/src/main/webapp/js/LoggingEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ var loggingEndpoint = {
websocket.onopen = function()
{
//Sets up the logger instance with the correct session ID
loggingEndpoint.sendId(sessionId);
loggingEndpoint.log({level:'FINE', message:'Connection to server log opened'});
websocket.send('{"httpSessionId":"' + sessionId + '"}');
};

websocket.onmessage = function(message)
Expand All @@ -21,19 +20,25 @@ var loggingEndpoint = {
{
for (i in jsonMessage)
{
loggingEndpoint.log(jsonMessage[i]);
loggingEndpoint.log(jsonMessage[i], true);
}
}
else
{
loggingEndpoint.log(jsonMessage);
loggingEndpoint.log(jsonMessage, true);
}
};
},

log : function(logRecord)
log : function(logRecord, localOnly)
{
logRecord.level = logRecord.level.toUpperCase();

if (!logRecord.timestamp)
{
logRecord.timestamp = Date.now();
}

var func;
if(typeof outputLog === "object")
{
Expand All @@ -57,17 +62,17 @@ var loggingEndpoint = {
func = console.log;
}

if (!localOnly)
{
websocket.send(JSON.stringify(logRecord));
}

func(logRecord.level + " : " + logRecord.message);
},

closeConnect : function()
{
websocket.close();
},

sendId : function(id)
{
websocket.send(id);
}
}

Expand Down
2 changes: 1 addition & 1 deletion ymca/src/main/webapp/js/outputLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var outputLog =
logElements.forEach(function(log)
{
var levelClass;
var date = logRecord.timestamp ? new Date(logRecord.timestamp) : new Date();
var date = new Date(logRecord.timestamp);
var timeHTML = '<span class="timestamp"><time datetime=' + date.toISOString() + ' title="' + date.toISOString() + '">' + date.toLocaleTimeString() + '</time> : </span>';
var iconHTML = '<span class="glyphicon';

Expand Down

0 comments on commit 532cc41

Please sign in to comment.