More WebSocket logging updates

Update WebSocketSession toString methods to include the handshake URI
and add id and URI fields to ensure they're available after close().

Log WebSocket session open and close events at INFO.

Remove trace messages for destinations that do not match.

Issue: SPR-11884
This commit is contained in:
Rossen Stoyanchev 2014-06-28 14:01:13 -04:00
parent fac2d80054
commit 86de416908
14 changed files with 109 additions and 102 deletions

View File

@ -135,9 +135,6 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers); String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
if (!checkDestinationPrefix(destination)) { if (!checkDestinationPrefix(destination)) {
if (logger.isTraceEnabled()) {
logger.trace("No match on destination in " + message);
}
return; return;
} }
@ -173,11 +170,6 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
} }
this.subscriptionRegistry.unregisterSubscription(message); this.subscriptionRegistry.unregisterSubscription(message);
} }
else {
if (logger.isTraceEnabled()) {
logger.trace("Unsupported message type in " + message);
}
}
} }
private void initHeaders(SimpMessageHeaderAccessor accessor) { private void initHeaders(SimpMessageHeaderAccessor accessor) {

View File

@ -454,9 +454,6 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler
String destination = stompAccessor.getDestination(); String destination = stompAccessor.getDestination();
if ((command != null) && command.requiresDestination() && !checkDestinationPrefix(destination)) { if ((command != null) && command.requiresDestination() && !checkDestinationPrefix(destination)) {
if (logger.isTraceEnabled()) {
logger.trace("No match on destination. Ignoring " + message);
}
return; return;
} }

View File

@ -171,8 +171,8 @@ public class UserDestinationMessageHandler implements MessageHandler, SmartLifec
} }
Set<String> destinations = result.getTargetDestinations(); Set<String> destinations = result.getTargetDestinations();
if (destinations.isEmpty()) { if (destinations.isEmpty()) {
if (logger.isDebugEnabled()) { if (logger.isTraceEnabled()) {
logger.debug("Use destination not resolved (no active sessions?): " + message); logger.trace("No user destinations for " + message);
} }
return; return;
} }
@ -185,7 +185,7 @@ public class UserDestinationMessageHandler implements MessageHandler, SmartLifec
} }
for (String destination : destinations) { for (String destination : destinations) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Sending message with resolved user destination: " + message); logger.trace("Sending " + message);
} }
this.brokerMessagingTemplate.send(destination, message); this.brokerMessagingTemplate.send(destination, message);
} }

View File

@ -144,7 +144,12 @@ public abstract class AbstractWebSocketSession<T> implements NativeWebSocketSess
@Override @Override
public String toString() { public String toString() {
return this.getClass().getSimpleName() + "[id=" + getId() + "]"; if (this.nativeSession != null) {
return getClass().getSimpleName() + "[id=" + getId() + ", uri=" + getUri() + "]";
}
else {
return getClass().getSimpleName() + "[nativeSession=null]";
}
} }
} }

View File

@ -46,14 +46,18 @@ import org.springframework.web.socket.adapter.AbstractWebSocketSession;
*/ */
public class JettyWebSocketSession extends AbstractWebSocketSession<Session> { public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
private String id;
private URI uri;
private HttpHeaders headers; private HttpHeaders headers;
private String acceptedProtocol;
private List<WebSocketExtension> extensions; private List<WebSocketExtension> extensions;
private Principal user; private Principal user;
private String acceptedProtocol;
/** /**
* Create a new {@link JettyWebSocketSession} instance. * Create a new {@link JettyWebSocketSession} instance.
@ -81,33 +85,36 @@ public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
@Override @Override
public String getId() { public String getId() {
checkNativeSessionInitialized(); checkNativeSessionInitialized();
return ObjectUtils.getIdentityHexString(getNativeSession()); return this.id;
} }
@Override @Override
public URI getUri() { public URI getUri() {
checkNativeSessionInitialized(); checkNativeSessionInitialized();
return getNativeSession().getUpgradeRequest().getRequestURI(); return this.uri;
} }
@Override @Override
public HttpHeaders getHandshakeHeaders() { public HttpHeaders getHandshakeHeaders() {
checkNativeSessionInitialized(); checkNativeSessionInitialized();
if (this.headers == null) {
this.headers = new HttpHeaders();
this.headers.putAll(getNativeSession().getUpgradeRequest().getHeaders());
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
}
return this.headers; return this.headers;
} }
@Override @Override
public Principal getPrincipal() { public String getAcceptedProtocol() {
if (this.user != null) {
return this.user;
}
checkNativeSessionInitialized(); checkNativeSessionInitialized();
return (isOpen() ? getNativeSession().getUpgradeRequest().getUserPrincipal() : null); return this.acceptedProtocol;
}
@Override
public List<WebSocketExtension> getExtensions() {
checkNativeSessionInitialized();
return this.extensions;
}
@Override
public Principal getPrincipal() {
return this.user;
} }
@Override @Override
@ -122,12 +129,6 @@ public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
return getNativeSession().getRemoteAddress(); return getNativeSession().getRemoteAddress();
} }
@Override
public String getAcceptedProtocol() {
checkNativeSessionInitialized();
return this.acceptedProtocol;
}
@Override @Override
public void setTextMessageSizeLimit(int messageSizeLimit) { public void setTextMessageSizeLimit(int messageSizeLimit) {
checkNativeSessionInitialized(); checkNativeSessionInitialized();
@ -152,19 +153,6 @@ public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
return getNativeSession().getPolicy().getMaxBinaryMessageSize(); return getNativeSession().getPolicy().getMaxBinaryMessageSize();
} }
@Override
public List<WebSocketExtension> getExtensions() {
checkNativeSessionInitialized();
if(this.extensions == null) {
List<ExtensionConfig> source = getNativeSession().getUpgradeResponse().getExtensions();
this.extensions = new ArrayList<WebSocketExtension>(source.size());
for(ExtensionConfig e : source) {
this.extensions.add(new WebSocketExtension(e.getName(), e.getParameters()));
}
}
return this.extensions;
}
@Override @Override
public boolean isOpen() { public boolean isOpen() {
return ((getNativeSession() != null) && getNativeSession().isOpen()); return ((getNativeSession() != null) && getNativeSession().isOpen());
@ -173,10 +161,25 @@ public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
@Override @Override
public void initializeNativeSession(Session session) { public void initializeNativeSession(Session session) {
super.initializeNativeSession(session); super.initializeNativeSession(session);
this.id = ObjectUtils.getIdentityHexString(getNativeSession());
this.uri = session.getUpgradeRequest().getRequestURI();
this.headers = new HttpHeaders();
this.headers.putAll(getNativeSession().getUpgradeRequest().getHeaders());
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol();
List<ExtensionConfig> source = getNativeSession().getUpgradeResponse().getExtensions();
this.extensions = new ArrayList<WebSocketExtension>(source.size());
for(ExtensionConfig e : source) {
this.extensions.add(new WebSocketExtension(e.getName(), e.getParameters()));
}
if (this.user == null) { if (this.user == null) {
this.user = session.getUpgradeRequest().getUserPrincipal(); this.user = session.getUpgradeRequest().getUserPrincipal();
} }
this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol();
} }
@Override @Override

View File

@ -47,18 +47,22 @@ import org.springframework.web.socket.adapter.AbstractWebSocketSession;
*/ */
public class StandardWebSocketSession extends AbstractWebSocketSession<Session> { public class StandardWebSocketSession extends AbstractWebSocketSession<Session> {
private String id;
private URI uri;
private final HttpHeaders handshakeHeaders; private final HttpHeaders handshakeHeaders;
private final InetSocketAddress localAddress;
private final InetSocketAddress remoteAddress;
private Principal user;
private String acceptedProtocol; private String acceptedProtocol;
private List<WebSocketExtension> extensions; private List<WebSocketExtension> extensions;
private Principal user;
private final InetSocketAddress localAddress;
private final InetSocketAddress remoteAddress;
/** /**
* Class constructor. * Class constructor.
@ -89,25 +93,24 @@ public class StandardWebSocketSession extends AbstractWebSocketSession<Session>
InetSocketAddress localAddress, InetSocketAddress remoteAddress, Principal user) { InetSocketAddress localAddress, InetSocketAddress remoteAddress, Principal user) {
super(attributes); super(attributes);
headers = (headers != null) ? headers : new HttpHeaders(); headers = (headers != null) ? headers : new HttpHeaders();
this.handshakeHeaders = HttpHeaders.readOnlyHttpHeaders(headers); this.handshakeHeaders = HttpHeaders.readOnlyHttpHeaders(headers);
this.user = user;
this.localAddress = localAddress; this.localAddress = localAddress;
this.remoteAddress = remoteAddress; this.remoteAddress = remoteAddress;
this.user = user;
} }
@Override @Override
public String getId() { public String getId() {
checkNativeSessionInitialized(); checkNativeSessionInitialized();
return getNativeSession().getId(); return this.id;
} }
@Override @Override
public URI getUri() { public URI getUri() {
checkNativeSessionInitialized(); checkNativeSessionInitialized();
return getNativeSession().getRequestURI(); return this.uri;
} }
@Override @Override
@ -116,12 +119,19 @@ public class StandardWebSocketSession extends AbstractWebSocketSession<Session>
} }
@Override @Override
public Principal getPrincipal() { public String getAcceptedProtocol() {
if (this.user != null) {
return this.user;
}
checkNativeSessionInitialized(); checkNativeSessionInitialized();
return (isOpen() ? getNativeSession().getUserPrincipal() : null); return this.acceptedProtocol;
}
@Override
public List<WebSocketExtension> getExtensions() {
checkNativeSessionInitialized();
return this.extensions;
}
public Principal getPrincipal() {
return this.user;
} }
@Override @Override
@ -134,12 +144,6 @@ public class StandardWebSocketSession extends AbstractWebSocketSession<Session>
return this.remoteAddress; return this.remoteAddress;
} }
@Override
public String getAcceptedProtocol() {
checkNativeSessionInitialized();
return this.acceptedProtocol;
}
@Override @Override
public void setTextMessageSizeLimit(int messageSizeLimit) { public void setTextMessageSizeLimit(int messageSizeLimit) {
checkNativeSessionInitialized(); checkNativeSessionInitialized();
@ -164,19 +168,6 @@ public class StandardWebSocketSession extends AbstractWebSocketSession<Session>
return getNativeSession().getMaxBinaryMessageBufferSize(); return getNativeSession().getMaxBinaryMessageBufferSize();
} }
@Override
public List<WebSocketExtension> getExtensions() {
checkNativeSessionInitialized();
if(this.extensions == null) {
List<Extension> source = getNativeSession().getNegotiatedExtensions();
this.extensions = new ArrayList<WebSocketExtension>(source.size());
for(Extension e : source) {
this.extensions.add(new StandardToWebSocketExtensionAdapter(e));
}
}
return this.extensions;
}
@Override @Override
public boolean isOpen() { public boolean isOpen() {
return (getNativeSession() != null && getNativeSession().isOpen()); return (getNativeSession() != null && getNativeSession().isOpen());
@ -185,10 +176,21 @@ public class StandardWebSocketSession extends AbstractWebSocketSession<Session>
@Override @Override
public void initializeNativeSession(Session session) { public void initializeNativeSession(Session session) {
super.initializeNativeSession(session); super.initializeNativeSession(session);
this.id = session.getId();
this.uri = session.getRequestURI();
this.acceptedProtocol = session.getNegotiatedSubprotocol();
List<Extension> source = getNativeSession().getNegotiatedExtensions();
this.extensions = new ArrayList<WebSocketExtension>(source.size());
for(Extension e : source) {
this.extensions.add(new StandardToWebSocketExtensionAdapter(e));
}
if(this.user == null) { if(this.user == null) {
this.user = session.getUserPrincipal(); this.user = session.getUserPrincipal();
} }
this.acceptedProtocol = session.getNegotiatedSubprotocol();
} }
@Override @Override

View File

@ -168,4 +168,10 @@ public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorat
super.close(status); super.close(status);
} }
@Override
public String toString() {
return getDelegate().toString();
}
} }

View File

@ -41,8 +41,8 @@ public class LoggingWebSocketHandlerDecorator extends WebSocketHandlerDecorator
@Override @Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception { public void afterConnectionEstablished(WebSocketSession session) throws Exception {
if (logger.isDebugEnabled()) { if (logger.isInfoEnabled()) {
logger.debug("Connection established, " + session + ", uri=" + session.getUri()); logger.info("Connection established " + session);
} }
super.afterConnectionEstablished(session); super.afterConnectionEstablished(session);
} }
@ -58,15 +58,15 @@ public class LoggingWebSocketHandlerDecorator extends WebSocketHandlerDecorator
@Override @Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
if (logger.isErrorEnabled()) { if (logger.isErrorEnabled()) {
logger.error("Transport error for " + session, exception); logger.error("Transport error in " + session, exception);
} }
super.handleTransportError(session, exception); super.handleTransportError(session, exception);
} }
@Override @Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
if (logger.isDebugEnabled()) { if (logger.isInfoEnabled()) {
logger.debug("Connection closed for " + session + ", " + closeStatus); logger.info("Connection closed with " + closeStatus + " in " + session + ", ");
} }
super.afterConnectionClosed(session, closeStatus); super.afterConnectionClosed(session, closeStatus);
} }

View File

@ -376,7 +376,8 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler,
} }
WebSocketSession session = holder.getSession(); WebSocketSession session = holder.getSession();
if (logger.isErrorEnabled()) { if (logger.isErrorEnabled()) {
logger.error("No messages received after " + timeSinceCreated + " ms. Closing " + holder + "."); logger.error("No messages received after " + timeSinceCreated + " ms. " +
"Closing " + holder.getSession() + ".");
} }
try { try {
session.close(CloseStatus.PROTOCOL_ERROR); session.close(CloseStatus.PROTOCOL_ERROR);
@ -446,12 +447,8 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler,
@Override @Override
public String toString() { public String toString() {
if (this.session instanceof ConcurrentWebSocketSessionDecorator) { return "WebSocketSessionHolder[=session=" + this.session + ", createTime=" +
return ((ConcurrentWebSocketSessionDecorator) this.session).getLastSession().toString(); this.createTime + ", hasHandledMessages=" + this.handledMessages + "]";
}
else {
return this.session.toString();
}
} }
} }

View File

@ -424,12 +424,9 @@ public abstract class AbstractSockJsSession implements SockJsSession {
} }
} }
@Override @Override
public String toString() { public String toString() {
long currentTime = System.currentTimeMillis(); return getClass().getSimpleName() + "[id=" + getId() + ", uri=" + getUri() + "]";
return getClass().getSimpleName() + "[id=" + this.id + ", state=" + this.state + ", sinceCreated=" +
(currentTime - this.timeCreated) + ", sinceLastActive=" + (currentTime - this.timeLastActive) + "]";
} }

View File

@ -233,4 +233,13 @@ public class WebSocketServerSockJsSession extends AbstractSockJsSession implemen
} }
} }
@Override
public String toString() {
if (getNativeSession() != null) {
return super.toString();
}
else {
return "WebSocketServerSockJsSession[id=" + getId() + ", uri=null]";
}
}
} }

View File

@ -101,7 +101,6 @@ public class JettyWebSocketSessionTests {
reset(nativeSession); reset(nativeSession);
assertNull(session.getPrincipal()); assertNull(session.getPrincipal());
verify(nativeSession).isOpen();
verifyNoMoreInteractions(nativeSession); verifyNoMoreInteractions(nativeSession);
} }

View File

@ -55,6 +55,7 @@ public class StandardWebSocketHandlerAdapterTests {
@Test @Test
public void onOpen() throws Throwable { public void onOpen() throws Throwable {
when(this.session.getId()).thenReturn("123");
this.adapter.onOpen(this.session, null); this.adapter.onOpen(this.session, null);
verify(this.webSocketHandler).afterConnectionEstablished(this.webSocketSession); verify(this.webSocketHandler).afterConnectionEstablished(this.webSocketSession);

View File

@ -86,7 +86,6 @@ public class StandardWebSocketSessionTests {
reset(nativeSession); reset(nativeSession);
assertNull(session.getPrincipal()); assertNull(session.getPrincipal());
verify(nativeSession).isOpen();
verifyNoMoreInteractions(nativeSession); verifyNoMoreInteractions(nativeSession);
} }