服务端SDK可以设置自定义日志处理器,可自行过滤不需要打印的日志

This commit is contained in:
xia jun 2024-10-18 14:03:37 +08:00
parent 7175585d37
commit eed7ffb747
10 changed files with 114 additions and 9 deletions

View File

@ -152,6 +152,12 @@ https://www.yuque.com/yuanfangxiyang/ma4ytb/vvy3iz/edit#nnzKN
3.修改部分问题 3.修改部分问题
-------------------------------------------------------------------------------------------
版本:4.2.8/时间:2024-10-18
1.服务端SDK可以设置自定义日志处理器可自行过滤不需要打印的日志
## Maven Gradle ## Maven Gradle
服务端sdk引用 服务端sdk引用
@ -161,7 +167,7 @@ https://www.yuque.com/yuanfangxiyang/ma4ytb/vvy3iz/edit#nnzKN
<dependency> <dependency>
<groupId>com.farsunset</groupId> <groupId>com.farsunset</groupId>
<artifactId>cim-server-sdk-netty</artifactId> <artifactId>cim-server-sdk-netty</artifactId>
<version>4.2.7</version> <version>4.2.8</version>
</dependency> </dependency>
``` ```

View File

@ -17,6 +17,7 @@
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<cim.server.sdk.version>4.2.8</cim.server.sdk.version>
<netty.version>4.1.79.Final</netty.version> <netty.version>4.1.79.Final</netty.version>
<protobuf.version>3.21.5</protobuf.version> <protobuf.version>3.21.5</protobuf.version>
<mysql.jdbc.version>8.0.30</mysql.jdbc.version> <mysql.jdbc.version>8.0.30</mysql.jdbc.version>
@ -67,7 +68,7 @@
<dependency> <dependency>
<groupId>com.farsunset</groupId> <groupId>com.farsunset</groupId>
<artifactId>cim-server-sdk-netty</artifactId> <artifactId>cim-server-sdk-netty</artifactId>
<version>4.2.7</version> <version>${cim.server.sdk.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>

View File

@ -0,0 +1,65 @@
package com.farsunset.cim.component.logger;
import com.farsunset.cim.handler.LoggingHandler;
import com.farsunset.cim.model.Ping;
import com.farsunset.cim.model.Pong;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import org.springframework.stereotype.Component;
/**
* 自定义 CIM事件日志打印重写方法时注意需要有以下2种之一
* 1调用对应的ctx.fireXX(msg)方法;
* 2调用supper
*/
@ChannelHandler.Sharable
@Component
public class CIMEventLogger extends LoggingHandler {
/**
* 不打印客户端发送的Pong日志
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof Pong){
ctx.fireChannelRead(msg);
return;
}
super.channelRead(ctx,msg);
}
/**
* 不打印服务端发送的Ping日志
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof Ping){
ctx.write(msg, promise);
return;
}
super.write(ctx,msg,promise);
}
/**
* 不打长连接空闲事件日志
* @param ctx
* @param evt
*/
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
ctx.fireUserEventTriggered(evt);
}
}

View File

@ -26,6 +26,7 @@ import com.farsunset.cim.acceptor.WebsocketAcceptor;
import com.farsunset.cim.acceptor.config.SocketConfig; import com.farsunset.cim.acceptor.config.SocketConfig;
import com.farsunset.cim.acceptor.config.WebsocketConfig; import com.farsunset.cim.acceptor.config.WebsocketConfig;
import com.farsunset.cim.component.handler.annotation.CIMHandler; import com.farsunset.cim.component.handler.annotation.CIMHandler;
import com.farsunset.cim.component.logger.CIMEventLogger;
import com.farsunset.cim.component.predicate.AuthPredicate; import com.farsunset.cim.component.predicate.AuthPredicate;
import com.farsunset.cim.config.properties.APNsProperties; import com.farsunset.cim.config.properties.APNsProperties;
import com.farsunset.cim.config.properties.CIMAppSocketProperties; import com.farsunset.cim.config.properties.CIMAppSocketProperties;
@ -76,7 +77,7 @@ public class CIMConfig implements CIMRequestHandler, ApplicationListener<Applica
@Bean(destroyMethod = "destroy",initMethod = "bind") @Bean(destroyMethod = "destroy",initMethod = "bind")
@ConditionalOnProperty(name = {"cim.websocket.enable"},matchIfMissing = true) @ConditionalOnProperty(name = {"cim.websocket.enable"},matchIfMissing = true)
public WebsocketAcceptor websocketAcceptor(CIMWebsocketProperties properties, AuthPredicate authPredicate) { public WebsocketAcceptor websocketAcceptor(CIMWebsocketProperties properties, AuthPredicate authPredicate, CIMEventLogger cimEventLogger) {
WebsocketConfig config = new WebsocketConfig(); WebsocketConfig config = new WebsocketConfig();
config.setAuthPredicate(authPredicate); config.setAuthPredicate(authPredicate);
config.setPath(properties.getPath()); config.setPath(properties.getPath());
@ -88,13 +89,13 @@ public class CIMConfig implements CIMRequestHandler, ApplicationListener<Applica
config.setWriteIdle(properties.getWriteIdle()); config.setWriteIdle(properties.getWriteIdle());
config.setReadIdle(properties.getReadIdle()); config.setReadIdle(properties.getReadIdle());
config.setMaxPongTimeout(properties.getMaxPongTimeout()); config.setMaxPongTimeout(properties.getMaxPongTimeout());
config.setLoggingHandler(cimEventLogger);
return new WebsocketAcceptor(config); return new WebsocketAcceptor(config);
} }
@Bean(destroyMethod = "destroy",initMethod = "bind") @Bean(destroyMethod = "destroy",initMethod = "bind")
@ConditionalOnProperty(name = {"cim.app.enable"},matchIfMissing = true) @ConditionalOnProperty(name = {"cim.app.enable"},matchIfMissing = true)
public AppSocketAcceptor appSocketAcceptor(CIMAppSocketProperties properties) { public AppSocketAcceptor appSocketAcceptor(CIMAppSocketProperties properties, CIMEventLogger cimEventLogger) {
SocketConfig config = new SocketConfig(); SocketConfig config = new SocketConfig();
config.setPort(properties.getPort()); config.setPort(properties.getPort());
@ -104,6 +105,7 @@ public class CIMConfig implements CIMRequestHandler, ApplicationListener<Applica
config.setWriteIdle(properties.getWriteIdle()); config.setWriteIdle(properties.getWriteIdle());
config.setReadIdle(properties.getReadIdle()); config.setReadIdle(properties.getReadIdle());
config.setMaxPongTimeout(properties.getMaxPongTimeout()); config.setMaxPongTimeout(properties.getMaxPongTimeout());
config.setLoggingHandler(cimEventLogger);
return new AppSocketAcceptor(config); return new AppSocketAcceptor(config);
} }

View File

@ -6,7 +6,7 @@
<groupId>com.farsunset</groupId> <groupId>com.farsunset</groupId>
<artifactId>cim-server-sdk-netty</artifactId> <artifactId>cim-server-sdk-netty</artifactId>
<version>4.2.7</version> <version>4.2.8</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name> <name>${project.groupId}:${project.artifactId}</name>

View File

@ -63,7 +63,7 @@ public class AppSocketAcceptor extends NioSocketAcceptor {
public void initChannel(SocketChannel ch){ public void initChannel(SocketChannel ch){
ch.pipeline().addLast(new AppMessageDecoder()); ch.pipeline().addLast(new AppMessageDecoder());
ch.pipeline().addLast(new AppMessageEncoder()); ch.pipeline().addLast(new AppMessageEncoder());
ch.pipeline().addLast(loggingHandler); ch.pipeline().addLast(socketConfig.getLoggingHandler() == null ? defaultLoggingHandler : socketConfig.getLoggingHandler() );
ch.pipeline().addLast(new IdleStateHandler(socketConfig.getReadIdle().getSeconds(), socketConfig.getWriteIdle().getSeconds(), 0, TimeUnit.SECONDS)); ch.pipeline().addLast(new IdleStateHandler(socketConfig.getReadIdle().getSeconds(), socketConfig.getWriteIdle().getSeconds(), 0, TimeUnit.SECONDS));
ch.pipeline().addLast(AppSocketAcceptor.this); ch.pipeline().addLast(AppSocketAcceptor.this);
} }

View File

@ -44,7 +44,7 @@ abstract class NioSocketAcceptor extends SimpleChannelInboundHandler<SentBody>{
protected final Logger logger = LoggerFactory.getLogger(getClass()); protected final Logger logger = LoggerFactory.getLogger(getClass());
protected final ChannelHandler loggingHandler = new LoggingHandler(); protected final ChannelHandler defaultLoggingHandler = new LoggingHandler();
protected final SocketConfig socketConfig; protected final SocketConfig socketConfig;

View File

@ -107,7 +107,7 @@ public class WebsocketAcceptor extends NioSocketAcceptor {
ch.pipeline().addLast(new WebMessageEncoder()); ch.pipeline().addLast(new WebMessageEncoder());
} }
ch.pipeline().addLast(new IdleStateHandler(config.getReadIdle().getSeconds(), config.getWriteIdle().getSeconds(), 0, TimeUnit.SECONDS)); ch.pipeline().addLast(new IdleStateHandler(config.getReadIdle().getSeconds(), config.getWriteIdle().getSeconds(), 0, TimeUnit.SECONDS));
ch.pipeline().addLast(loggingHandler); ch.pipeline().addLast(socketConfig.getLoggingHandler() == null ? defaultLoggingHandler : socketConfig.getLoggingHandler() );
ch.pipeline().addLast(WebsocketAcceptor.this); ch.pipeline().addLast(WebsocketAcceptor.this);
ch.pipeline().addLast(illegalRequestHandler); ch.pipeline().addLast(illegalRequestHandler);
} }

View File

@ -22,6 +22,7 @@
package com.farsunset.cim.acceptor.config; package com.farsunset.cim.acceptor.config;
import com.farsunset.cim.handler.CIMRequestHandler; import com.farsunset.cim.handler.CIMRequestHandler;
import com.farsunset.cim.handler.LoggingHandler;
import java.time.Duration; import java.time.Duration;
@ -67,6 +68,13 @@ public class SocketConfig {
*/ */
private int maxPongTimeout = 1; private int maxPongTimeout = 1;
/**
* 自定义日志打印处理器可不设置
*/
private LoggingHandler loggingHandler;
public Integer getPort() { public Integer getPort() {
return port == null || port <= 0 ? DEFAULT_PORT : port; return port == null || port <= 0 ? DEFAULT_PORT : port;
} }
@ -87,6 +95,14 @@ public class SocketConfig {
return enable; return enable;
} }
public void setLoggingHandler(LoggingHandler loggingHandler) {
this.loggingHandler = loggingHandler;
}
public LoggingHandler getLoggingHandler() {
return loggingHandler;
}
public void setEnable(boolean enable) { public void setEnable(boolean enable) {
this.enable = enable; this.enable = enable;
} }

View File

@ -23,6 +23,7 @@ package com.farsunset.cim.acceptor.config;
import com.farsunset.cim.auth.AuthPredicateInfo; import com.farsunset.cim.auth.AuthPredicateInfo;
import com.farsunset.cim.constant.WebsocketProtocol; import com.farsunset.cim.constant.WebsocketProtocol;
import com.farsunset.cim.handler.LoggingHandler;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -52,6 +53,12 @@ public class WebsocketConfig extends SocketConfig{
*/ */
private Predicate<AuthPredicateInfo> authPredicate; private Predicate<AuthPredicateInfo> authPredicate;
/**
* 自定义日志打印处理器可不设置
*/
private LoggingHandler loggingHandler;
@Override @Override
public Integer getPort() { public Integer getPort() {
@ -81,4 +88,12 @@ public class WebsocketConfig extends SocketConfig{
public Predicate<AuthPredicateInfo> getAuthPredicate() { public Predicate<AuthPredicateInfo> getAuthPredicate() {
return authPredicate; return authPredicate;
} }
public LoggingHandler getLoggingHandler() {
return loggingHandler;
}
public void setLoggingHandler(LoggingHandler loggingHandler) {
this.loggingHandler = loggingHandler;
}
} }