STOMP and WebSocket messaging related logging updates

This change removes most logging at INFO level and also ensures the
amount of information logged at DEBUG level is useful, brief, and
not duplicated.

Also added is custom logging for STOMP frames to ensure very readable
and consise output.

Issue: SPR-11934
This commit is contained in:
Rossen Stoyanchev
2014-07-09 00:30:22 -04:00
parent ab4864da2a
commit 48236be4a2
65 changed files with 763 additions and 425 deletions

View File

@@ -110,12 +110,22 @@ public class SimpAttributesContextHolderTests {
}
@Test
public void setAttributesFromMessageWithMissingHeaders() {
public void setAttributesFromMessageWithMissingSessionId() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage(startsWith("Message does not contain SiMP session id or attributes"));
this.thrown.expectMessage(startsWith("No session id in"));
SimpAttributesContextHolder.setAttributesFromMessage(new GenericMessage<Object>(""));
}
@Test
public void setAttributesFromMessageWithMissingSessionAttributes() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage(startsWith("No session attributes in"));
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create();
headerAccessor.setSessionId("session1");
Message<?> message = MessageBuilder.createMessage("", headerAccessor.getMessageHeaders());
SimpAttributesContextHolder.setAttributesFromMessage(message);
}
@Test
public void currentAttributes() {
SimpAttributesContextHolder.setAttributes(this.simpAttributes);

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.simp;
import org.junit.Test;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
/**
* Unit tests for SimpMessageHeaderAccessor.
*
* @author Rossen Stoyanchev
*/
public class SimpMessageHeaderAccessorTests {
@Test
public void getShortLogMessage() {
assertEquals("MESSAGE session=null payload=p", SimpMessageHeaderAccessor.create().getShortLogMessage("p"));
}
@Test
public void getLogMessageWithValuesSet() {
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
accessor.setDestination("/destination");
accessor.setSubscriptionId("subscription");
accessor.setSessionId("session");
accessor.setUser(new TestPrincipal("user"));
accessor.setSessionAttributes(Collections.<String, Object>singletonMap("key", "value"));
assertEquals("MESSAGE destination=/destination subscriptionId=subscription " +
"session=session user=user attributes[1] payload=p", accessor.getShortLogMessage("p"));
}
@Test
public void getDetailedLogMessageWithValuesSet() {
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
accessor.setDestination("/destination");
accessor.setSubscriptionId("subscription");
accessor.setSessionId("session");
accessor.setUser(new TestPrincipal("user"));
accessor.setSessionAttributes(Collections.<String, Object>singletonMap("key", "value"));
accessor.setNativeHeader("nativeKey", "nativeValue");
assertEquals("MESSAGE destination=/destination subscriptionId=subscription " +
"session=session user=user attributes={key=value} nativeHeaders=" +
"{nativeKey=[nativeValue]} payload=p", accessor.getDetailedLogMessage("p"));
}
}

View File

@@ -109,7 +109,7 @@ public class SimpleBrokerMessageHandlerTests {
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT);
headers.setSessionId(sess1);
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).copyHeaders(headers.toMap()).build();
Message<byte[]> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
this.messageHandler.handleMessage(message);
this.messageHandler.handleMessage(createMessage("/foo", "message1"));
@@ -141,28 +141,23 @@ public class SimpleBrokerMessageHandlerTests {
protected Message<String> createSubscriptionMessage(String sessionId, String subcriptionId, String destination) {
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
headers.setSubscriptionId(subcriptionId);
headers.setDestination(destination);
headers.setSessionId(sessionId);
return MessageBuilder.withPayload("").copyHeaders(headers.toMap()).build();
return MessageBuilder.createMessage("", headers.getMessageHeaders());
}
protected Message<String> createConnectMessage(String sessionId) {
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
headers.setSessionId(sessionId);
return MessageBuilder.withPayload("").setHeaders(headers).build();
return MessageBuilder.createMessage("", headers.getMessageHeaders());
}
protected Message<String> createMessage(String destination, String payload) {
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
headers.setDestination(destination);
return MessageBuilder.withPayload(payload).copyHeaders(headers.toMap()).build();
return MessageBuilder.createMessage("", headers.getMessageHeaders());
}
protected boolean assertCapturedMessage(String sessionId, String subcriptionId, String destination) {

View File

@@ -166,7 +166,7 @@ public class MessageBrokerConfigurationTests {
headers.setSessionId("sess1");
headers.setSubscriptionId("subs1");
headers.setDestination("/foo");
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
Message<?> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
// subscribe
broker.handleMessage(message);
@@ -174,7 +174,7 @@ public class MessageBrokerConfigurationTests {
headers = StompHeaderAccessor.create(StompCommand.SEND);
headers.setSessionId("sess1");
headers.setDestination("/foo");
message = MessageBuilder.withPayload("bar".getBytes()).setHeaders(headers).build();
message = MessageBuilder.createMessage("bar".getBytes(), headers.getMessageHeaders());
// message
broker.handleMessage(message);
@@ -235,7 +235,7 @@ public class MessageBrokerConfigurationTests {
headers.setSessionId("sess1");
headers.setSessionAttributes(new ConcurrentHashMap<>());
headers.setDestination("/foo");
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
Message<?> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
messageHandler.handleMessage(message);
@@ -256,7 +256,7 @@ public class MessageBrokerConfigurationTests {
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
headers.setDestination("/user/joe/foo");
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
Message<?> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
messageHandler.handleMessage(message);

View File

@@ -17,6 +17,7 @@
package org.springframework.messaging.simp.stomp;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
@@ -31,6 +32,7 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.util.AlternativeJdkIdGenerator;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.MultiValueMap;
@@ -44,6 +46,7 @@ import static org.junit.Assert.*;
*/
public class StompHeaderAccessorTests {
private static final Charset UTF_8 = Charset.forName("UTF-8");
@Test
public void createWithCommand() {
@@ -240,4 +243,22 @@ public class StompHeaderAccessorTests {
assertSame(headerAccessor, MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class));
}
@Test
public void getShortLogMessage() {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
accessor.setDestination("/foo");
accessor.setContentType(MimeTypeUtils.APPLICATION_JSON);
accessor.setSessionId("123");
String actual = accessor.getShortLogMessage("payload".getBytes(Charset.forName("UTF-8")));
assertEquals("SEND /foo session=123 application/json payload=payload", actual);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 80; i++) {
sb.append("a");
}
final String payload = sb.toString() + " > 80";
actual = accessor.getShortLogMessage(payload.getBytes(UTF_8));
assertEquals("SEND /foo session=123 application/json payload=" + sb + "...(truncated)", actual);
}
}

View File

@@ -16,19 +16,23 @@
package org.springframework.messaging.support;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.UUID;
import org.hamcrest.CoreMatchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.util.IdGenerator;
import org.springframework.util.MimeTypeUtils;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.*;
/**
@@ -38,6 +42,8 @@ import static org.junit.Assert.*;
*/
public class MessageHeaderAccessorTests {
private static final Charset UTF_8 = Charset.forName("UTF-8");
@Rule
public final ExpectedException thrown = ExpectedException.none();
@@ -250,6 +256,77 @@ public class MessageHeaderAccessorTests {
assertNotNull(headers.getTimestamp());
}
@Test
public void getShortLogMessagePayload() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
accessor.setContentType(MimeTypeUtils.TEXT_PLAIN);
assertEquals("headers={contentType=text/plain} payload=p", accessor.getShortLogMessage("p"));
assertEquals("headers={contentType=text/plain} payload=p", accessor.getShortLogMessage("p".getBytes(UTF_8)));
assertEquals("headers={contentType=text/plain} payload=p", accessor.getShortLogMessage(new Object() {
@Override
public String toString() {
return "p";
}
}));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 80; i++) {
sb.append("a");
}
final String payload = sb.toString() + " > 80";
String actual = accessor.getShortLogMessage(payload);
assertEquals("headers={contentType=text/plain} payload=" + sb + "...(truncated)", actual);
actual = accessor.getShortLogMessage(payload.getBytes(UTF_8));
assertEquals("headers={contentType=text/plain} payload=" + sb + "...(truncated)", actual);
actual = accessor.getShortLogMessage(new Object() {
@Override
public String toString() {
return payload;
}
});
assertThat(actual, startsWith("headers={contentType=text/plain} payload=" + getClass().getName() + "$"));
}
@Test
public void getDetailedLogMessagePayload() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
accessor.setContentType(MimeTypeUtils.TEXT_PLAIN);
assertEquals("headers={contentType=text/plain} payload=p", accessor.getDetailedLogMessage("p"));
assertEquals("headers={contentType=text/plain} payload=p", accessor.getDetailedLogMessage("p".getBytes(UTF_8)));
assertEquals("headers={contentType=text/plain} payload=p", accessor.getDetailedLogMessage(new Object() {
@Override
public String toString() {
return "p";
}
}));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 80; i++) {
sb.append("a");
}
final String payload = sb.toString() + " > 80";
String actual = accessor.getDetailedLogMessage(payload);
assertEquals("headers={contentType=text/plain} payload=" + sb + " > 80", actual);
actual = accessor.getDetailedLogMessage(payload.getBytes(UTF_8));
assertEquals("headers={contentType=text/plain} payload=" + sb + " > 80", actual);
actual = accessor.getDetailedLogMessage(new Object() {
@Override
public String toString() {
return payload;
}
});
assertEquals("headers={contentType=text/plain} payload=" + sb + " > 80", actual);
}
public static class TestMessageHeaderAccessor extends MessageHeaderAccessor {