Paho Java Client日志调试(mqtt)

参考官网:https://wiki.eclipse.org/Paho/Log_and_Debug_in_the_Java_client

使用paho的包并发发送MQTT消息时总是报错,于是想打开日志调试。

1. 首先在项目里新建一个日志配置文件jsr47min.properties

org.eclipse.paho.client.mqttv3.handlers=java.util.logging.MemoryHandler
org.eclipse.paho.client.mqttv3.level=ALL
# It is possible to set more granular trace on a per class basis e.g.
#org.eclipse.paho.client.mqttv3.internal.ClientComms.level=ALL

java.util.logging.MemoryHandler.level=ALL
java.util.logging.MemoryHandler.size=10000
java.util.logging.MemoryHandler.push=ALL
java.util.logging.MemoryHandler.target=java.util.logging.FileHandler
#java.util.logging.MemoryHandler.target=java.util.logging.ConsoleHandler

java.util.logging.FileHandler.level=ALL

# Naming style for the output file:
# (The output file is placed in the directory
# defined by the "user.home" System property.)
# See java.util.logging for more options 
java.util.logging.FileHandler.pattern=%h/paho%u.log

# Limiting size of output file in bytes:
java.util.logging.FileHandler.limit=200000

# Number of output files to cycle through, by appending an
# integer to the base file name:
java.util.logging.FileHandler.count=1000

# Style of output (Simple or XML):
java.util.logging.FileHandler.formatter=org.eclipse.paho.client.mqttv3.logging.SimpleLogFormatter

由于并发,日志量较大,很容易错过日志,所以我将保留文件改为了1000个
输出日志级别为ALL

2. 启动时指定日志配置文件路径

如果是普通的java程序,则在启动时加上-Djava.util.logging.config.file=[jsr47min.properties的路径]即可。
我的程序是运行在tomcat上,故此处介绍下tomcat如何配置这个参数。

  • windows下eclipse配置方式:

(1) 在eclipse中打开tomcat并点击配置
这里写图片描述
(2)在VM arguments里加入-Djava.util.logging.config.file=[jsr47min.properties的路径]
这里写图片描述
(3)然后直接在eclipse中启动tomcat即可。

  • linux下tomcat配置方式

    启动tomcat是用tomcat路径下的bin/startup.sh,实际这个脚本是在调用catalina.sh。
    观察catalina.sh,里面有介绍如何配置java.util.logging.config.file
    这里写图片描述
    而默认的配置是这样的
    这里写图片描述
    这个LOGGING_MANAGER也会影响到我们接下来要配置的日志打印
    所以我们要在在setenv.sh中添加如下配置:

LOGGING_CONFIG="-Djava.util.logging.config.file=[jsr47min.properties的路径]"
LOGGING_MANAGER=""

配置好后通过startup.sh启动即可。

3. 查看日志文件

根据配置文件上的配置java.util.logging.FileHandler.pattern=%h/paho%u.log
日志文件默认会用用户home目录下。

猜你喜欢

转载自blog.csdn.net/lblblblblzdx/article/details/81136922