Skip to content

Commit 86de416

Browse files
committed
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
1 parent fac2d80 commit 86de416

File tree

14 files changed

+107
-100
lines changed

14 files changed

+107
-100
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/broker/SimpleBrokerMessageHandler.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,6 @@ protected void handleMessageInternal(Message<?> message) {
135135
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
136136

137137
if (!checkDestinationPrefix(destination)) {
138-
if (logger.isTraceEnabled()) {
139-
logger.trace("No match on destination in " + message);
140-
}
141138
return;
142139
}
143140

@@ -173,11 +170,6 @@ else if (SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
173170
}
174171
this.subscriptionRegistry.unregisterSubscription(message);
175172
}
176-
else {
177-
if (logger.isTraceEnabled()) {
178-
logger.trace("Unsupported message type in " + message);
179-
}
180-
}
181173
}
182174

183175
private void initHeaders(SimpMessageHeaderAccessor accessor) {

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,6 @@ else if (accessor instanceof SimpMessageHeaderAccessor) {
454454

455455
String destination = stompAccessor.getDestination();
456456
if ((command != null) && command.requiresDestination() && !checkDestinationPrefix(destination)) {
457-
if (logger.isTraceEnabled()) {
458-
logger.trace("No match on destination. Ignoring " + message);
459-
}
460457
return;
461458
}
462459

spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationMessageHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ public void handleMessage(Message<?> message) throws MessagingException {
171171
}
172172
Set<String> destinations = result.getTargetDestinations();
173173
if (destinations.isEmpty()) {
174-
if (logger.isDebugEnabled()) {
175-
logger.debug("Use destination not resolved (no active sessions?): " + message);
174+
if (logger.isTraceEnabled()) {
175+
logger.trace("No user destinations for " + message);
176176
}
177177
return;
178178
}
@@ -185,7 +185,7 @@ public void handleMessage(Message<?> message) throws MessagingException {
185185
}
186186
for (String destination : destinations) {
187187
if (logger.isTraceEnabled()) {
188-
logger.trace("Sending message with resolved user destination: " + message);
188+
logger.trace("Sending " + message);
189189
}
190190
this.brokerMessagingTemplate.send(destination, message);
191191
}

spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,12 @@ public final void close(CloseStatus status) throws IOException {
144144

145145
@Override
146146
public String toString() {
147-
return this.getClass().getSimpleName() + "[id=" + getId() + "]";
147+
if (this.nativeSession != null) {
148+
return getClass().getSimpleName() + "[id=" + getId() + ", uri=" + getUri() + "]";
149+
}
150+
else {
151+
return getClass().getSimpleName() + "[nativeSession=null]";
152+
}
148153
}
149154

150155
}

spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@
4646
*/
4747
public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
4848

49+
private String id;
50+
51+
private URI uri;
52+
4953
private HttpHeaders headers;
5054

55+
private String acceptedProtocol;
56+
5157
private List<WebSocketExtension> extensions;
5258

5359
private Principal user;
5460

55-
private String acceptedProtocol;
56-
5761

5862
/**
5963
* Create a new {@link JettyWebSocketSession} instance.
@@ -81,51 +85,48 @@ public JettyWebSocketSession(Map<String, Object> attributes, Principal user) {
8185
@Override
8286
public String getId() {
8387
checkNativeSessionInitialized();
84-
return ObjectUtils.getIdentityHexString(getNativeSession());
88+
return this.id;
8589
}
8690

8791
@Override
8892
public URI getUri() {
8993
checkNativeSessionInitialized();
90-
return getNativeSession().getUpgradeRequest().getRequestURI();
94+
return this.uri;
9195
}
9296

9397
@Override
9498
public HttpHeaders getHandshakeHeaders() {
9599
checkNativeSessionInitialized();
96-
if (this.headers == null) {
97-
this.headers = new HttpHeaders();
98-
this.headers.putAll(getNativeSession().getUpgradeRequest().getHeaders());
99-
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
100-
}
101100
return this.headers;
102101
}
103102

104103
@Override
105-
public Principal getPrincipal() {
106-
if (this.user != null) {
107-
return this.user;
108-
}
104+
public String getAcceptedProtocol() {
109105
checkNativeSessionInitialized();
110-
return (isOpen() ? getNativeSession().getUpgradeRequest().getUserPrincipal() : null);
106+
return this.acceptedProtocol;
111107
}
112108

113109
@Override
114-
public InetSocketAddress getLocalAddress() {
110+
public List<WebSocketExtension> getExtensions() {
115111
checkNativeSessionInitialized();
116-
return getNativeSession().getLocalAddress();
112+
return this.extensions;
117113
}
118114

119115
@Override
120-
public InetSocketAddress getRemoteAddress() {
116+
public Principal getPrincipal() {
117+
return this.user;
118+
}
119+
120+
@Override
121+
public InetSocketAddress getLocalAddress() {
121122
checkNativeSessionInitialized();
122-
return getNativeSession().getRemoteAddress();
123+
return getNativeSession().getLocalAddress();
123124
}
124125

125126
@Override
126-
public String getAcceptedProtocol() {
127+
public InetSocketAddress getRemoteAddress() {
127128
checkNativeSessionInitialized();
128-
return this.acceptedProtocol;
129+
return getNativeSession().getRemoteAddress();
129130
}
130131

131132
@Override
@@ -152,19 +153,6 @@ public int getBinaryMessageSizeLimit() {
152153
return getNativeSession().getPolicy().getMaxBinaryMessageSize();
153154
}
154155

155-
@Override
156-
public List<WebSocketExtension> getExtensions() {
157-
checkNativeSessionInitialized();
158-
if(this.extensions == null) {
159-
List<ExtensionConfig> source = getNativeSession().getUpgradeResponse().getExtensions();
160-
this.extensions = new ArrayList<WebSocketExtension>(source.size());
161-
for(ExtensionConfig e : source) {
162-
this.extensions.add(new WebSocketExtension(e.getName(), e.getParameters()));
163-
}
164-
}
165-
return this.extensions;
166-
}
167-
168156
@Override
169157
public boolean isOpen() {
170158
return ((getNativeSession() != null) && getNativeSession().isOpen());
@@ -173,10 +161,25 @@ public boolean isOpen() {
173161
@Override
174162
public void initializeNativeSession(Session session) {
175163
super.initializeNativeSession(session);
164+
165+
this.id = ObjectUtils.getIdentityHexString(getNativeSession());
166+
this.uri = session.getUpgradeRequest().getRequestURI();
167+
168+
this.headers = new HttpHeaders();
169+
this.headers.putAll(getNativeSession().getUpgradeRequest().getHeaders());
170+
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
171+
172+
this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol();
173+
174+
List<ExtensionConfig> source = getNativeSession().getUpgradeResponse().getExtensions();
175+
this.extensions = new ArrayList<WebSocketExtension>(source.size());
176+
for(ExtensionConfig e : source) {
177+
this.extensions.add(new WebSocketExtension(e.getName(), e.getParameters()));
178+
}
179+
176180
if (this.user == null) {
177181
this.user = session.getUpgradeRequest().getUserPrincipal();
178182
}
179-
this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol();
180183
}
181184

182185
@Override

spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSession.java

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,21 @@
4747
*/
4848
public class StandardWebSocketSession extends AbstractWebSocketSession<Session> {
4949

50+
private String id;
51+
52+
private URI uri;
53+
5054
private final HttpHeaders handshakeHeaders;
5155

52-
private final InetSocketAddress localAddress;
56+
private String acceptedProtocol;
5357

54-
private final InetSocketAddress remoteAddress;
58+
private List<WebSocketExtension> extensions;
5559

5660
private Principal user;
5761

58-
private String acceptedProtocol;
62+
private final InetSocketAddress localAddress;
5963

60-
private List<WebSocketExtension> extensions;
64+
private final InetSocketAddress remoteAddress;
6165

6266

6367
/**
@@ -89,25 +93,24 @@ public StandardWebSocketSession(HttpHeaders headers, Map<String, Object> attribu
8993
InetSocketAddress localAddress, InetSocketAddress remoteAddress, Principal user) {
9094

9195
super(attributes);
92-
9396
headers = (headers != null) ? headers : new HttpHeaders();
9497
this.handshakeHeaders = HttpHeaders.readOnlyHttpHeaders(headers);
98+
this.user = user;
9599
this.localAddress = localAddress;
96100
this.remoteAddress = remoteAddress;
97-
this.user = user;
98101
}
99102

100103

101104
@Override
102105
public String getId() {
103106
checkNativeSessionInitialized();
104-
return getNativeSession().getId();
107+
return this.id;
105108
}
106109

107110
@Override
108111
public URI getUri() {
109112
checkNativeSessionInitialized();
110-
return getNativeSession().getRequestURI();
113+
return this.uri;
111114
}
112115

113116
@Override
@@ -116,12 +119,19 @@ public HttpHeaders getHandshakeHeaders() {
116119
}
117120

118121
@Override
119-
public Principal getPrincipal() {
120-
if (this.user != null) {
121-
return this.user;
122-
}
122+
public String getAcceptedProtocol() {
123123
checkNativeSessionInitialized();
124-
return (isOpen() ? getNativeSession().getUserPrincipal() : null);
124+
return this.acceptedProtocol;
125+
}
126+
127+
@Override
128+
public List<WebSocketExtension> getExtensions() {
129+
checkNativeSessionInitialized();
130+
return this.extensions;
131+
}
132+
133+
public Principal getPrincipal() {
134+
return this.user;
125135
}
126136

127137
@Override
@@ -134,12 +144,6 @@ public InetSocketAddress getRemoteAddress() {
134144
return this.remoteAddress;
135145
}
136146

137-
@Override
138-
public String getAcceptedProtocol() {
139-
checkNativeSessionInitialized();
140-
return this.acceptedProtocol;
141-
}
142-
143147
@Override
144148
public void setTextMessageSizeLimit(int messageSizeLimit) {
145149
checkNativeSessionInitialized();
@@ -164,19 +168,6 @@ public int getBinaryMessageSizeLimit() {
164168
return getNativeSession().getMaxBinaryMessageBufferSize();
165169
}
166170

167-
@Override
168-
public List<WebSocketExtension> getExtensions() {
169-
checkNativeSessionInitialized();
170-
if(this.extensions == null) {
171-
List<Extension> source = getNativeSession().getNegotiatedExtensions();
172-
this.extensions = new ArrayList<WebSocketExtension>(source.size());
173-
for(Extension e : source) {
174-
this.extensions.add(new StandardToWebSocketExtensionAdapter(e));
175-
}
176-
}
177-
return this.extensions;
178-
}
179-
180171
@Override
181172
public boolean isOpen() {
182173
return (getNativeSession() != null && getNativeSession().isOpen());
@@ -185,10 +176,21 @@ public boolean isOpen() {
185176
@Override
186177
public void initializeNativeSession(Session session) {
187178
super.initializeNativeSession(session);
179+
180+
this.id = session.getId();
181+
this.uri = session.getRequestURI();
182+
183+
this.acceptedProtocol = session.getNegotiatedSubprotocol();
184+
185+
List<Extension> source = getNativeSession().getNegotiatedExtensions();
186+
this.extensions = new ArrayList<WebSocketExtension>(source.size());
187+
for(Extension e : source) {
188+
this.extensions.add(new StandardToWebSocketExtensionAdapter(e));
189+
}
190+
188191
if(this.user == null) {
189192
this.user = session.getUserPrincipal();
190193
}
191-
this.acceptedProtocol = session.getNegotiatedSubprotocol();
192194
}
193195

194196
@Override

spring-websocket/src/main/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecorator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,10 @@ public void close(CloseStatus status) throws IOException {
168168
super.close(status);
169169
}
170170

171+
172+
@Override
173+
public String toString() {
174+
return getDelegate().toString();
175+
}
176+
171177
}

spring-websocket/src/main/java/org/springframework/web/socket/handler/LoggingWebSocketHandlerDecorator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public LoggingWebSocketHandlerDecorator(WebSocketHandler delegate) {
4141

4242
@Override
4343
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
44-
if (logger.isDebugEnabled()) {
45-
logger.debug("Connection established, " + session + ", uri=" + session.getUri());
44+
if (logger.isInfoEnabled()) {
45+
logger.info("Connection established " + session);
4646
}
4747
super.afterConnectionEstablished(session);
4848
}
@@ -58,15 +58,15 @@ public void handleMessage(WebSocketSession session, WebSocketMessage<?> message)
5858
@Override
5959
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
6060
if (logger.isErrorEnabled()) {
61-
logger.error("Transport error for " + session, exception);
61+
logger.error("Transport error in " + session, exception);
6262
}
6363
super.handleTransportError(session, exception);
6464
}
6565

6666
@Override
6767
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
68-
if (logger.isDebugEnabled()) {
69-
logger.debug("Connection closed for " + session + ", " + closeStatus);
68+
if (logger.isInfoEnabled()) {
69+
logger.info("Connection closed with " + closeStatus + " in " + session + ", ");
7070
}
7171
super.afterConnectionClosed(session, closeStatus);
7272
}

0 commit comments

Comments
 (0)