Skip to content

Commit c2266bf

Browse files
committed
[broker-25] improve logging
1 parent b57c0f0 commit c2266bf

35 files changed

+279
-62
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ allprojects {
4343
projectReactorVersion = "3.3.0.RELEASE"
4444
byteBuddyVersion = "1.10.2"
4545
objenesisVersion = "3.1"
46+
gsonVersion = "2.8.6"
4647
}
4748

4849
dependencies {
@@ -52,6 +53,7 @@ allprojects {
5253
implementation "org.springframework.boot:spring-boot-starter:$springbootVersion"
5354
implementation "org.springframework.boot:spring-boot-starter-log4j2:$springbootVersion"
5455
implementation "io.projectreactor:reactor-core:$projectReactorVersion"
56+
implementation "com.google.code.gson:gson:$gsonVersion"
5557

5658
compileOnly "org.jetbrains:annotations:$annotationVersion"
5759
compileOnly "org.projectlombok:lombok:$lombokVersion"

src/main/java/com/ss/mqtt/broker/handler/packet/in/DisconnetInPacketHandler.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ss.mqtt.broker.handler.packet.in;
22

3+
import com.ss.mqtt.broker.model.reason.code.DisconnectReasonCode;
34
import com.ss.mqtt.broker.network.client.MqttClient.UnsafeMqttClient;
45
import com.ss.mqtt.broker.network.packet.in.DisconnectInPacket;
56
import lombok.extern.log4j.Log4j2;
@@ -10,7 +11,15 @@ public class DisconnetInPacketHandler extends AbstractPacketHandler<UnsafeMqttCl
1011

1112
@Override
1213
protected void handleImpl(@NotNull UnsafeMqttClient client, @NotNull DisconnectInPacket packet) {
13-
log.info("Disconnect client {} with packet {}", client, packet);
14+
15+
var reasonCode = packet.getReasonCode();
16+
17+
if (reasonCode == DisconnectReasonCode.NORMAL_DISCONNECTION) {
18+
log.info("Disconnect client {}", client);
19+
} else {
20+
log.error("Disconnect client {} by error reason {}", client, reasonCode);
21+
}
22+
1423
client.release().subscribe();
1524
}
1625
}

src/main/java/com/ss/mqtt/broker/handler/publish/out/Qos0PublishOutHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Qos0PublishOutHandler extends AbstractPublishOutHandler {
1111

1212
@Override
1313
protected @NotNull QoS getQoS() {
14-
return QoS.AT_MOST_ONCE_DELIVERY;
14+
return QoS.AT_MOST_ONCE;
1515
}
1616

1717
@Override

src/main/java/com/ss/mqtt/broker/handler/publish/out/Qos1PublishOutHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Qos1PublishOutHandler extends PersistentPublishOutHandler {
1212

1313
@Override
1414
protected @NotNull QoS getQoS() {
15-
return QoS.AT_LEAST_ONCE_DELIVERY;
15+
return QoS.AT_LEAST_ONCE;
1616
}
1717

1818
@Override

src/main/java/com/ss/mqtt/broker/handler/publish/out/Qos2PublishOutHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public final class Qos2PublishOutHandler extends PersistentPublishOutHandler {
1414

1515
@Override
1616
protected @NotNull QoS getQoS() {
17-
return QoS.EXACTLY_ONCE_DELIVERY;
17+
return QoS.EXACTLY_ONCE;
1818
}
1919

2020
@Override

src/main/java/com/ss/mqtt/broker/model/MqttPropertyConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public interface MqttPropertyConstants {
44

5-
QoS MAXIMUM_QOS_DEFAULT = QoS.EXACTLY_ONCE_DELIVERY;
5+
QoS MAXIMUM_QOS_DEFAULT = QoS.EXACTLY_ONCE;
66

77
int MAXIMUM_PROTOCOL_PACKET_SIZE = 256 * 1024 * 1024;
88
int MAXIMUM_PACKET_ID = 0xFFFF;

src/main/java/com/ss/mqtt/broker/model/QoS.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
@Getter
99
@RequiredArgsConstructor
1010
public enum QoS {
11-
AT_MOST_ONCE_DELIVERY(SubscribeAckReasonCode.GRANTED_QOS_0),
12-
AT_LEAST_ONCE_DELIVERY(SubscribeAckReasonCode.GRANTED_QOS_1),
13-
EXACTLY_ONCE_DELIVERY(SubscribeAckReasonCode.GRANTED_QOS_2),
11+
AT_MOST_ONCE(SubscribeAckReasonCode.GRANTED_QOS_0),
12+
AT_LEAST_ONCE(SubscribeAckReasonCode.GRANTED_QOS_1),
13+
EXACTLY_ONCE(SubscribeAckReasonCode.GRANTED_QOS_2),
1414
INVALID(SubscribeAckReasonCode.IMPLEMENTATION_SPECIFIC_ERROR);
1515

1616
private static final QoS[] VALUES = values();
1717

1818
public static @NotNull QoS of(int level) {
19-
if (level < 0 || level > EXACTLY_ONCE_DELIVERY.ordinal()) {
19+
if (level < 0 || level > EXACTLY_ONCE.ordinal()) {
2020
return INVALID;
2121
} else {
2222
return VALUES[level];

src/main/java/com/ss/mqtt/broker/model/SubscribeTopicFilter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import com.ss.mqtt.broker.model.topic.TopicFilter;
55
import lombok.Getter;
66
import lombok.RequiredArgsConstructor;
7-
import lombok.ToString;
87
import org.jetbrains.annotations.NotNull;
98

109
@Getter
11-
@ToString
1210
@EqualsAndHashCode
1311
@RequiredArgsConstructor
1412
public class SubscribeTopicFilter {
@@ -51,4 +49,10 @@ public SubscribeTopicFilter(@NotNull String topicFilter, @NotNull QoS qos) {
5149
public SubscribeTopicFilter(@NotNull TopicFilter topicFilter, @NotNull QoS qos) {
5250
this(topicFilter, qos, SubscribeRetainHandling.SEND, true, true);
5351
}
52+
53+
@Override
54+
public String toString() {
55+
return "SubscribeTopicFilter(" + "topicFilter=" + topicFilter.getRawTopic() + ", qos=" + qos + ", retainHandling=" +
56+
retainHandling + ", noLocal=" + noLocal + ", retainAsPublished=" + retainAsPublished + ')';
57+
}
5458
}

src/main/java/com/ss/mqtt/broker/model/topic/AbstractTopic.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import com.ss.rlib.common.util.StringUtils;
55
import lombok.EqualsAndHashCode;
66
import lombok.Getter;
7-
import lombok.ToString;
87
import org.jetbrains.annotations.NotNull;
98

109
@Getter
11-
@ToString(of = "rawTopic")
1210
@EqualsAndHashCode(of = "rawTopic")
1311
public abstract class AbstractTopic {
1412

@@ -48,4 +46,9 @@ static void checkTopic(@NotNull String topic) {
4846
int levelsCount() {
4947
return segments.length;
5048
}
49+
50+
@Override
51+
public String toString() {
52+
return rawTopic;
53+
}
5154
}

src/main/java/com/ss/mqtt/broker/network/client/AbstractMqttClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public AbstractMqttClient(@NotNull MqttConnection connection, @NotNull MqttClien
5858

5959
@Override
6060
public void handle(@NotNull MqttReadablePacket packet) {
61-
log.info("Handle received packet: {}", packet);
61+
log.info("Handle received packet: {} : {}", packet.getName(), packet);
6262

6363
var packetHandler = connection.getPacketHandlers()[packet.getPacketType()];
6464

0 commit comments

Comments
 (0)