当前位置: 首页 > news >正文

netty 客户端发送消息服务端收到消息无法打印,springBoot配合 lombok使用@Slf4j

netty 客户端发送消息服务端收到消息无法打印,springBoot配合 lombok使用@Slf4j

服务端代码

@Slf4j
public class EventLoopServer {public static void main(String[] args) throws InterruptedException {new ServerBootstrap().group(new NioEventLoopGroup()).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<NioSocketChannel>() {@Overrideprotected void initChannel(NioSocketChannel ch) throws Exception {log.debug("connecting...");ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {log.debug("receive");ByteBuf byteBuf = msg instanceof ByteBuf ? ((ByteBuf) msg) : null;if (byteBuf != null) {byte[] buf = new byte[16];byteBuf.readBytes(buf, 0, byteBuf.readableBytes());log.debug(new String(buf));}}});}}).bind(8080).sync();log.debug("ok");}
}

客户端代码

@Slf4j
public class EventLoopClient {public static void main(String[] args) throws InterruptedException {Channel channel = new Bootstrap().group(new NioEventLoopGroup()).channel(NioSocketChannel.class).handler(new ChannelInitializer<NioSocketChannel>() {@Overrideprotected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {System.out.println("init...");nioSocketChannel.pipeline().addLast(new StringEncoder()); //将字符编码后发送}}).connect(new InetSocketAddress("localhost", 8080)).sync().channel();//channel.writeAndFlush(Unpooled.buffer().writeBytes("hello".getBytes(StandardCharsets.UTF_8)));channel.writeAndFlush("hello");System.out.println(channel);System.out.println("a");}
}

此时客户端发送成功,但是服务端没有打印信息,经过调试发现,原因是 springboot默认的日志级别是info,所以debug日志不打印,所以需要配置日志

引入Lombok依赖

        <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>

引入插件

 <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>

添加配置

#日志配置
logging:config: classpath:logback-spring.xml  #指定项目启动的时,读取logback-spring.xml日志配置文件,文件名最好不要使用logback.xmllevel: #配置指定包的路径下应用程序的日志记录和日志级别。root: infospringfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator: errororg.springframework: warnfile:path: D:\sinux_2023\project\phm\724_3\exe\logs #设置日志输出路径name: phm.log #设置日志文件的名称

此时会报错
在这里插入图片描述
需要编写配置文件

编写log4j.properties文件

log4j.rootLogger=DEBUG,CONSOLE,file
#log4j.rootLogger=ERROR,ROLLING_FILE######################################################################################
# Console Appender  \u65E5\u5FD7\u5728\u63A7\u5236\u8F93\u51FA\u914D\u7F6E
######################################################################################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=error
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n######################################################################################
# DailyRolling File  \u6BCF\u5929\u4EA7\u751F\u4E00\u4E2A\u65E5\u5FD7\u6587\u4EF6\uFF0C\u6587\u4EF6\u540D\u683C\u5F0F:log2009-09-11
######################################################################################
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=yyyy-MM-dd
log4j.appender.file.File=log.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=error
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%nlog4j.logger.com.opensymphony.xwork2=error  

此时就可以正常打印debug日志了
在这里插入图片描述

相关文章:

  • 辣椒青椒幼苗和杂草检测数据集VOC+YOLO格式706张2类别
  • 《AI大模型应知应会100篇》第59篇:Flowise:无代码搭建大模型应用
  • flutter使用命令生成BinarySize分析图
  • 专题二:二叉树的深度搜索(二叉树剪枝)
  • 《 C++ 点滴漫谈: 三十七 》左值?右值?完美转发?C++ 引用的真相超乎你想象!
  • GB/T 17626.5-2019浪涌抗扰度【研读笔记】
  • IOP|2025第二届机电一体化、机器人与控制系统国际会议(MRCS2025)
  • 在选择合适的实验室铁地板和铸铁试验平板,帮分析​
  • 【机器人】复现 UniGoal 具身导航 | 通用零样本目标导航 CVPR 2025
  • 2025年阿里云ACP大数据分析师认证模拟试题(附答案解析)
  • Android Framework
  • Python实例题:Django搭建简易博客
  • 力扣HOT100之二叉树:226. 翻转二叉树
  • arduinoIDE核心库更新导致的ESP32开发板神秘接口更换和三方库冲突
  • JVM——方法内联
  • zuoyyyeee
  • hdfs-客户端操作-文件上传
  • Java中String字符串
  • STM32实现循环队列
  • AutoDL租用服务器教程
  • 石家庄推动城市能级与民生福祉并进
  • “犍陀罗艺术与亚洲文明”在浙大对外展出
  • 印度最新发声:对所有敌对行动均予以反击和回应,不会升级冲突
  • 协会:坚决支持司法机关依法打击涉象棋行业的违法行为
  • 复旦大学文科杰出教授裘锡圭逝世,享年90岁
  • 国家主席习近平同普京总统共见记者