mirror of
https://gitee.com/farsunset/cim.git
synced 2025-07-23 16:26:42 +08:00
服务端SDK可以设置自定义日志处理器,可自行过滤不需要打印的日志
This commit is contained in:
parent
7175585d37
commit
eed7ffb747
@ -152,6 +152,12 @@ https://www.yuque.com/yuanfangxiyang/ma4ytb/vvy3iz/edit#nnzKN
|
||||
3.修改部分问题
|
||||
|
||||
|
||||
-------------------------------------------------------------------------------------------
|
||||
版本:4.2.8/时间:2024-10-18
|
||||
|
||||
1.服务端SDK可以设置自定义日志处理器,可自行过滤不需要打印的日志
|
||||
|
||||
|
||||
## Maven Gradle
|
||||
|
||||
服务端sdk引用
|
||||
@ -161,7 +167,7 @@ https://www.yuque.com/yuanfangxiyang/ma4ytb/vvy3iz/edit#nnzKN
|
||||
<dependency>
|
||||
<groupId>com.farsunset</groupId>
|
||||
<artifactId>cim-server-sdk-netty</artifactId>
|
||||
<version>4.2.7</version>
|
||||
<version>4.2.8</version>
|
||||
</dependency>
|
||||
|
||||
```
|
||||
|
@ -17,6 +17,7 @@
|
||||
|
||||
<properties>
|
||||
<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>
|
||||
<protobuf.version>3.21.5</protobuf.version>
|
||||
<mysql.jdbc.version>8.0.30</mysql.jdbc.version>
|
||||
@ -67,7 +68,7 @@
|
||||
<dependency>
|
||||
<groupId>com.farsunset</groupId>
|
||||
<artifactId>cim-server-sdk-netty</artifactId>
|
||||
<version>4.2.7</version>
|
||||
<version>${cim.server.sdk.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -26,6 +26,7 @@ import com.farsunset.cim.acceptor.WebsocketAcceptor;
|
||||
import com.farsunset.cim.acceptor.config.SocketConfig;
|
||||
import com.farsunset.cim.acceptor.config.WebsocketConfig;
|
||||
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.config.properties.APNsProperties;
|
||||
import com.farsunset.cim.config.properties.CIMAppSocketProperties;
|
||||
@ -76,7 +77,7 @@ public class CIMConfig implements CIMRequestHandler, ApplicationListener<Applica
|
||||
|
||||
@Bean(destroyMethod = "destroy",initMethod = "bind")
|
||||
@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();
|
||||
config.setAuthPredicate(authPredicate);
|
||||
config.setPath(properties.getPath());
|
||||
@ -88,13 +89,13 @@ public class CIMConfig implements CIMRequestHandler, ApplicationListener<Applica
|
||||
config.setWriteIdle(properties.getWriteIdle());
|
||||
config.setReadIdle(properties.getReadIdle());
|
||||
config.setMaxPongTimeout(properties.getMaxPongTimeout());
|
||||
|
||||
config.setLoggingHandler(cimEventLogger);
|
||||
return new WebsocketAcceptor(config);
|
||||
}
|
||||
|
||||
@Bean(destroyMethod = "destroy",initMethod = "bind")
|
||||
@ConditionalOnProperty(name = {"cim.app.enable"},matchIfMissing = true)
|
||||
public AppSocketAcceptor appSocketAcceptor(CIMAppSocketProperties properties) {
|
||||
public AppSocketAcceptor appSocketAcceptor(CIMAppSocketProperties properties, CIMEventLogger cimEventLogger) {
|
||||
|
||||
SocketConfig config = new SocketConfig();
|
||||
config.setPort(properties.getPort());
|
||||
@ -104,6 +105,7 @@ public class CIMConfig implements CIMRequestHandler, ApplicationListener<Applica
|
||||
config.setWriteIdle(properties.getWriteIdle());
|
||||
config.setReadIdle(properties.getReadIdle());
|
||||
config.setMaxPongTimeout(properties.getMaxPongTimeout());
|
||||
config.setLoggingHandler(cimEventLogger);
|
||||
|
||||
return new AppSocketAcceptor(config);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.farsunset</groupId>
|
||||
<artifactId>cim-server-sdk-netty</artifactId>
|
||||
<version>4.2.7</version>
|
||||
<version>4.2.8</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.groupId}:${project.artifactId}</name>
|
||||
|
@ -63,7 +63,7 @@ public class AppSocketAcceptor extends NioSocketAcceptor {
|
||||
public void initChannel(SocketChannel ch){
|
||||
ch.pipeline().addLast(new AppMessageDecoder());
|
||||
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(AppSocketAcceptor.this);
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ abstract class NioSocketAcceptor extends SimpleChannelInboundHandler<SentBody>{
|
||||
|
||||
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
protected final ChannelHandler loggingHandler = new LoggingHandler();
|
||||
protected final ChannelHandler defaultLoggingHandler = new LoggingHandler();
|
||||
|
||||
protected final SocketConfig socketConfig;
|
||||
|
||||
|
@ -107,7 +107,7 @@ public class WebsocketAcceptor extends NioSocketAcceptor {
|
||||
ch.pipeline().addLast(new WebMessageEncoder());
|
||||
}
|
||||
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(illegalRequestHandler);
|
||||
}
|
||||
|
@ -22,6 +22,7 @@
|
||||
package com.farsunset.cim.acceptor.config;
|
||||
|
||||
import com.farsunset.cim.handler.CIMRequestHandler;
|
||||
import com.farsunset.cim.handler.LoggingHandler;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
@ -67,6 +68,13 @@ public class SocketConfig {
|
||||
*/
|
||||
private int maxPongTimeout = 1;
|
||||
|
||||
/**
|
||||
* 自定义日志打印处理器,可不设置
|
||||
*/
|
||||
|
||||
private LoggingHandler loggingHandler;
|
||||
|
||||
|
||||
public Integer getPort() {
|
||||
return port == null || port <= 0 ? DEFAULT_PORT : port;
|
||||
}
|
||||
@ -87,6 +95,14 @@ public class SocketConfig {
|
||||
return enable;
|
||||
}
|
||||
|
||||
public void setLoggingHandler(LoggingHandler loggingHandler) {
|
||||
this.loggingHandler = loggingHandler;
|
||||
}
|
||||
|
||||
public LoggingHandler getLoggingHandler() {
|
||||
return loggingHandler;
|
||||
}
|
||||
|
||||
public void setEnable(boolean enable) {
|
||||
this.enable = enable;
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ package com.farsunset.cim.acceptor.config;
|
||||
|
||||
import com.farsunset.cim.auth.AuthPredicateInfo;
|
||||
import com.farsunset.cim.constant.WebsocketProtocol;
|
||||
import com.farsunset.cim.handler.LoggingHandler;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@ -52,6 +53,12 @@ public class WebsocketConfig extends SocketConfig{
|
||||
*/
|
||||
private Predicate<AuthPredicateInfo> authPredicate;
|
||||
|
||||
/**
|
||||
* 自定义日志打印处理器,可不设置
|
||||
*/
|
||||
|
||||
private LoggingHandler loggingHandler;
|
||||
|
||||
|
||||
@Override
|
||||
public Integer getPort() {
|
||||
@ -81,4 +88,12 @@ public class WebsocketConfig extends SocketConfig{
|
||||
public Predicate<AuthPredicateInfo> getAuthPredicate() {
|
||||
return authPredicate;
|
||||
}
|
||||
|
||||
public LoggingHandler getLoggingHandler() {
|
||||
return loggingHandler;
|
||||
}
|
||||
|
||||
public void setLoggingHandler(LoggingHandler loggingHandler) {
|
||||
this.loggingHandler = loggingHandler;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user