Log4j:

添加依赖:

  1. <!-- 整合Log4j-->
  2. <dependency>
  3. <groupId>log4j</groupId>
  4. <artifactId>log4j</artifactId>
  5. <version>1.2.17</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.slf4j</groupId>
  9. <artifactId>slf4j-api</artifactId>
  10. <version>1.7.25</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.slf4j</groupId>
  14. <artifactId>slf4j-log4j12</artifactId>
  15. <version>1.7.25</version>
  16. <scope>test</scope>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.slf4j</groupId>
  20. <artifactId>slf4j-simple</artifactId>
  21. <version>1.7.25</version>
  22. <scope>test</scope>
  23. </dependency>

创建配置文件:

  1. log4j.rootLogger=DEBUG, stdout
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  4. log4j.appender.stdout.layout.ConversionPattern=[%p] [%C]{1} - %m%n

运行任意NIO服务即可查看Log4j配置生效情况
image.png
Log4j配置已生效

添加日志:

在handler中添加Netty内置的 LoggingHandler 即可,参考心跳检测WebSocket章节