设置输出日志

遇上配置目录不生效的情况,修改jar内的日志配置文件
例子
打开jar把里面的logback.xml的info改成debug,再复现这个错误,查看日志,把日志提交到Mycat团队或者作者
![L]{XYXELYF91YC@YZG2%79.png

或者启动的时候添加

  1. -Dlogback.configurationFile=xxxx\logback.xml
  1. java -jar -Dlogback.configurationFile=xxx\logback.xml xxxx\mycat2-1.21-release-jar-with-dependencies-2021-1-12-1.jar

-D参数一定要写在jar路径前面
另外也可以把这个参数添加在wrapper.conf里面

  1. wrapper.java.additional.10=-Dlogback.configurationFile=./conf/logback.xml

logback.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
  4. <encoder>
  5. <pattern>
  6. %d[%level]%c{100}.%M:%L%m%n
  7. </pattern>
  8. </encoder>
  9. </appender>
  10. <root level="info">
  11. <appender-ref ref="CONSOLE" />
  12. </root>
  13. </configuration>

此处的CONSOLE就是输出到控制台,Java Service Wrapper会把控制台的输出转到Wrapper.log里面,如果发现Wrapper.log过大就要把此处修改.

设置debug级别日志后,日志文件里面会出现debug前缀的日志

  1. 2022-02-22 16:52:01,316[DEBUG]io.vertx.core.logging.LoggerFactory.?:?Using io.vertx.core.logging.SLF4JLogDelegateFactory
  2. 2022-02-22 16:52:01,344[INFO]io.mycat.MycatCore.newMycatServer:214start VertxMycatServer
  3. 2022-02-22 16:52:02,015[DEBUG]io.netty.util.internal.logging.InternalLoggerFactory.useSlf4JLoggerFactory:63Using SLF4J as the default logging framework
  4. 2022-02-22 16:52:02,023[DEBUG]io.netty.util.internal.PlatformDependent0.explicitNoUnsafeCause0:460-Dio.netty.noUnsafe: false
  5. 2022-02-22 16:52:02,023[DEBUG]io.netty.util.internal.PlatformDependent0.javaVersion0:954Java version: 8

当然也包括查询的sql

  1. 2022-02-22 16:53:00,340[DEBUG]io.mycat.commands.MycatdbCommand.executeQuery:172SET NAMES utf8
  2. 2022-02-22 16:53:00,387[DEBUG]io.mycat.commands.MycatdbCommand.executeQuery:172SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP())

Mycat2随着发展使用了不同的日志框架

到1.20为止使用slf4j接口框架,其实现是simplelogger(过时)
它的设置参考如下
simplelogger.properties

  1. # SLF4J's SimpleLogger configuration file
  2. # Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err.
  3. # Default logging detail level for all instances of SimpleLogger.
  4. # Must be one of ("trace", "debug", "info", "warn", or "error").
  5. # If not specified, defaults to "info".
  6. org.slf4j.simpleLogger.defaultLogLevel=info
  7. # Logging detail level for a SimpleLogger instance named "xxxxx".
  8. # Must be one of ("trace", "debug", "info", "warn", or "error").
  9. # If not specified, the default logging detail level is used.
  10. #org.slf4j.simpleLogger.log.xxxxx=
  11. # Set to true if you want the current date and time to be included in output messages.
  12. # Default is false, and will output the number of milliseconds elapsed since startup.
  13. #org.slf4j.simpleLogger.showDateTime=false
  14. # The date and time format to be used in the output messages.
  15. # The pattern describing the date and time format is the same that is used in java.text.SimpleDateFormat.
  16. # If the format is not specified or is invalid, the default format is used.
  17. # The default format is yyyy-MM-dd HH:mm:ss:SSS Z.
  18. #org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS Z
  19. # Set to true if you want to output the current thread name.
  20. # Defaults to true.
  21. #org.slf4j.simpleLogger.showThreadName=true
  22. # Set to true if you want the Logger instance name to be included in output messages.
  23. # Defaults to true.
  24. #org.slf4j.simpleLogger.showLogName=true
  25. # Set to true if you want the last component of the name to be included in output messages.
  26. # Defaults to false.
  27. #org.slf4j.simpleLogger.showShortLogName=false
  1. simplelogger.properties
  2. org.slf4j.simpleLogger.defaultLogLevel=debug
  3. 遇上配置目录不生效的情况,修改jar内的simplelogger.properties文件
  4. java启动参数
  5. -Dorg.slf4j.simpleLogger.defaultLogLevel=debug
  6. 生产环境依据情况设置级别
  7. -Dorg.slf4j.simpleLogger.defaultLogLevel=info
  8. debuginfo级别,性能有两倍差异

在2021-10-15号发布的1.20使用了logback,并添加了kafka连接器(lingkang提交该功能,通过更换maven依赖实现),便于记录并分析生产日志
它的设置参考如下
logback.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <appender name="kafkaAppender" class="com.github.danielwegener.logback.kafka.KafkaAppender">
  4. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  5. <pattern>%message %n</pattern>
  6. <charset>utf8</charset>
  7. </encoder>
  8. <topic>applog-test</topic>
  9. <keyingStrategy class="com.github.danielwegener.logback.kafka.keying.NoKeyKeyingStrategy"/>
  10. <deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy"/>
  11. <producerConfig>bootstrap.servers=192.168.18.43:9092,192.168.18.44:9092</producerConfig>
  12. <producerConfig>linger.ms=1000</producerConfig>
  13. <producerConfig>acks=0</producerConfig>
  14. <producerConfig>client.id=localhost-time-collector-logback-relaxed</producerConfig>
  15. <producerConfig>max.block.ms=0</producerConfig>
  16. </appender>
  17. <logger name="org.apache.kafka.clients" level="error"></logger>
  18. <logger name="org.apache.kafka.clients.NetworkClient" level="error"></logger>
  19. <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
  20. <encoder>
  21. <pattern>
  22. %d[%level]%c{100}.%M:%L%m%n
  23. </pattern>
  24. </encoder>
  25. </appender>
  26. <root level="debug">
  27. <appender-ref ref="kafkaAppender"/>
  28. <appender-ref ref="CONSOLE" />
  29. </root>
  30. </configuration>

其中bootstrap.servers是kafka服务器的ip

不带kafka的配置如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
  4. <encoder>
  5. <pattern>
  6. %d[%level]%c{100}.%M:%L%m%n
  7. </pattern>
  8. </encoder>
  9. </appender>
  10. <root level="debug">
  11. <appender-ref ref="CONSOLE" />
  12. </root>
  13. </configuration>

配置关系

image.png

过滤日志

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
  4. <encoder>
  5. <pattern>
  6. %d[%level]%c{100}.%M:%L%m%n
  7. </pattern>
  8. </encoder>
  9. <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
  10. <!--匹配处理器-->
  11. <evaluator>
  12. <!-- 处理模式,默认为 ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
  13. <!-- 存在某个字符串则匹配成功 -->
  14. <expression>return (message.contains("Connection reset by peer") || message.contains("远程主机强迫"));</expression>
  15. </evaluator>
  16. <!--匹配则停止执行日志输出-->
  17. <OnMatch>DENY</OnMatch>
  18. <!--不匹配则往下执行-->
  19. <OnMismatch>ACCEPT</OnMismatch>
  20. </filter>
  21. </appender>
  22. <root level="info">
  23. <appender-ref ref="CONSOLE" />
  24. </root>
  25. </configuration>

日志会不输出
带有 “Connection reset by peer”或者 “远程主机强迫”的日志