View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.socket.handler;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import org.springframework.web.socket.CloseStatus;
23  import org.springframework.web.socket.WebSocketHandler;
24  import org.springframework.web.socket.WebSocketMessage;
25  import org.springframework.web.socket.WebSocketSession;
26  
27  /**
28   * A {@link WebSocketHandlerDecorator} that adds logging to WebSocket lifecycle events.
29   *
30   * @author Rossen Stoyanchev
31   * @since 4.0
32   */
33  public class LoggingWebSocketHandlerDecorator extends WebSocketHandlerDecorator {
34  
35  	private static final Log logger = LogFactory.getLog(LoggingWebSocketHandlerDecorator.class);
36  
37  
38  	public LoggingWebSocketHandlerDecorator(WebSocketHandler delegate) {
39  		super(delegate);
40  	}
41  
42  
43  	@Override
44  	public void afterConnectionEstablished(WebSocketSession session) throws Exception {
45  		if (logger.isDebugEnabled()) {
46  			logger.debug("New "	+ session);
47  		}
48  		super.afterConnectionEstablished(session);
49  	}
50  
51  	@Override
52  	public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
53  		if (logger.isTraceEnabled()) {
54  			logger.trace("Handling " + message + " in " + session);
55  		}
56  		super.handleMessage(session, message);
57  	}
58  
59  	@Override
60  	public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
61  		if (logger.isDebugEnabled()) {
62  			logger.debug("Transport error in " + session, exception);
63  		}
64  		super.handleTransportError(session, exception);
65  	}
66  
67  	@Override
68  	public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
69  		if (logger.isDebugEnabled()) {
70  			logger.debug(session + " closed with " + closeStatus);
71  		}
72  		super.afterConnectionClosed(session, closeStatus);
73  	}
74  
75  }