Skip to content

Commit 5b1cbf0

Browse files
committed
Log WebSocket connection issues at DEBUG level
WebSocket clients going away is an expected and common occurance. Logging at ERROR level on failure to close a connection or on failures to write data to a WebSocket sessions has a high potential for false positives with very little to do. This change lowers the log level for a number of log messages that fit this category. This should be helped by the effort already spent for 4.1 to ensure logging at DEBUG level doesn't produce excessive amounts of logging. Issue: SPR-12155
1 parent 07ffb6f commit 5b1cbf0

File tree

7 files changed

+29
-18
lines changed

7 files changed

+29
-18
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,7 +52,9 @@ public void afterConnectionEstablished(WebSocketSession session) {
5252
}
5353

5454
public static void tryCloseWithError(WebSocketSession session, Throwable exception, Log logger) {
55-
logger.error("Closing due to exception for " + session, exception);
55+
if (logger.isDebugEnabled()) {
56+
logger.debug("Closing due to exception for " + session, exception);
57+
}
5658
if (session.isOpen()) {
5759
try {
5860
session.close(CloseStatus.SERVER_ERROR);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public void handleMessage(WebSocketSession session, WebSocketMessage<?> message)
5757

5858
@Override
5959
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
60-
if (logger.isErrorEnabled()) {
61-
logger.error("Transport error in " + session, exception);
60+
if (logger.isDebugEnabled()) {
61+
logger.debug("Transport error in " + session, exception);
6262
}
6363
super.handleTransportError(session, exception);
6464
}

spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ else if (StompCommand.UNSUBSCRIBE.equals(headerAccessor.getCommand())) {
255255
}
256256
}
257257
catch (Throwable ex) {
258-
logger.error("Failed to send STOMP message from client to application MessageChannel" +
258+
logger.error("Failed to send client message to application via MessageChannel" +
259259
" in session " + session.getId() + ". Sending STOMP ERROR to client.", ex);
260260
sendErrorMessage(session, ex);
261261

@@ -280,7 +280,8 @@ protected void sendErrorMessage(WebSocketSession session, Throwable error) {
280280
session.sendMessage(new TextMessage(bytes));
281281
}
282282
catch (Throwable ex) {
283-
logger.error("Failed to send STOMP ERROR to client.", ex);
283+
// Could be part of normal workflow (e.g. browser tab closed)
284+
logger.debug("Failed to send STOMP ERROR to client.", ex);
284285
}
285286
}
286287

@@ -330,7 +331,8 @@ else if (StompCommand.CONNECTED.equals(command)) {
330331
throw ex;
331332
}
332333
catch (Throwable ex) {
333-
logger.error("Failed to send WebSocket message to client in session " + session.getId() + ".", ex);
334+
// Could be part of normal workflow (e.g. browser tab closed)
335+
logger.debug("Failed to send WebSocket message to client in session " + session.getId() + ".", ex);
334336
command = StompCommand.ERROR;
335337
}
336338
finally {

spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,20 @@ public void handleMessage(Message<?> message) throws MessagingException {
333333
}
334334
catch (SessionLimitExceededException ex) {
335335
try {
336-
logger.error("Terminating '" + session + "'", ex);
336+
if (logger.isDebugEnabled()) {
337+
logger.debug("Terminating '" + session + "'", ex);
338+
}
337339
this.stats.incrementLimitExceededCount();
338340
clearSession(session, ex.getStatus()); // clear first, session may be unresponsive
339341
session.close(ex.getStatus());
340342
}
341343
catch (Exception secondException) {
342-
logger.error("Failure while closing session " + sessionId + ".", secondException);
344+
logger.debug("Failure while closing session " + sessionId + ".", secondException);
343345
}
344346
}
345347
catch (Exception e) {
346-
logger.error("Failed to send message to client in " + session + ": " + message, e);
348+
// Could be part of normal workflow (e.g. browser tab closed)
349+
logger.debug("Failed to send message to client in " + session + ": " + message, e);
347350
}
348351
}
349352

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ protected void handleRawWebSocketRequest(ServerHttpRequest request, ServerHttpRe
178178
catch (Throwable ex) {
179179
failure = new HandshakeFailureException("Uncaught failure for request " + request.getURI(), ex);
180180
}
181-
finally {
181+
finally {
182182
if (failure != null) {
183183
chain.applyAfterHandshake(request, response, failure);
184184
throw failure;
@@ -303,7 +303,8 @@ public void run() {
303303
}
304304
}
305305
catch (Throwable ex) {
306-
logger.error("Failed to close " + session, ex);
306+
// Could be part of normal workflow (e.g. browser tab closed)
307+
logger.debug("Failed to close " + session, ex);
307308
}
308309
}
309310
if (logger.isDebugEnabled() && !removedSessionIds.isEmpty()) {

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ protected void resetRequest() {
323323
control.complete();
324324
}
325325
catch (Throwable ex) {
326-
logger.error("Failed to complete request: " + ex.getMessage());
326+
// Could be part of normal workflow (e.g. browser tab closed)
327+
logger.debug("Failed to complete request: " + ex.getMessage());
327328
}
328329
}
329330
}

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public final void close(CloseStatus status) throws IOException {
291291
this.handler.afterConnectionClosed(this, status);
292292
}
293293
catch (Throwable ex) {
294-
logger.error("Error from WebSocketHandler.afterConnectionClosed in " + this, ex);
294+
logger.debug("Error from WebSocketHandler.afterConnectionClosed in " + this, ex);
295295
}
296296
}
297297
}
@@ -307,7 +307,9 @@ public final void close(CloseStatus status) throws IOException {
307307
* Close due to error arising from SockJS transport handling.
308308
*/
309309
public void tryCloseWithSockJsTransportError(Throwable error, CloseStatus closeStatus) {
310-
logger.error("Closing due to transport error for " + this);
310+
if (logger.isDebugEnabled()) {
311+
logger.debug("Closing due to transport error for " + this);
312+
}
311313
try {
312314
delegateError(error);
313315
}
@@ -318,7 +320,7 @@ public void tryCloseWithSockJsTransportError(Throwable error, CloseStatus closeS
318320
close(closeStatus);
319321
}
320322
catch (Throwable closeException) {
321-
logger.error("Failure while closing " + this, closeException);
323+
logger.debug("Failure while closing " + this, closeException);
322324
}
323325
}
324326

@@ -370,7 +372,7 @@ else if (disconnectedClientLogger.isDebugEnabled()) {
370372
}
371373
}
372374
else {
373-
logger.error("Terminating connection after failure to send message to client.", failure);
375+
logger.debug("Terminating connection after failure to send message to client.", failure);
374376
}
375377
}
376378

@@ -421,7 +423,7 @@ protected void cancelHeartbeat() {
421423
}
422424
}
423425
catch (Throwable ex) {
424-
logger.error("Failure while cancelling heartbeat in session " + getId(), ex);
426+
logger.debug("Failure while cancelling heartbeat in session " + getId(), ex);
425427
}
426428
}
427429

0 commit comments

Comments
 (0)