hivemqtt-client原本是一个java后台框架,集成到android并没有android的log打印,但是我们可以添加。
由于hivemqtt-client是封装的netty,所以我们想用netty的loggerHandler打印log,所以我们也需要修改源码。
1.git下载源码:https://github.com/hivemq/hivemq-mqtt-client.git ,修改源码如下,在MqttChannelInitializer类 添加LoggingHandler。
package com.hivemq.client.internal.mqtt.handler;
...
public class MqttChannelInitializer extends ChannelInboundHandlerAdapter {
...
private void initMqtt(@NotNull Channel channel) {
channel.pipeline()
.addLast("log", new LoggingHandler()) //添加netty的log打印
.addLast("encoder", this.encoder)
.addLast("auth", this.authHandler)
.addLast("connect", this.connectHandler)
.addLast("disconnect", this.disconnectHandler);
}
}
...
1.2然后重新编译出:hivemq-mqtt-client-1.3.4.jar即可,这个时候我们就直接依赖jar包了,但是由于hivemqtt-client不仅仅依赖netty还依赖其他的jar,所以我们需要添加其他的gradle build的依赖。
implementation(name: 'hivemq-mqtt-client-1.3.4', ext: 'jar')
//其他依赖
implementation("io.netty:netty-buffer:4.1.99.Final")
implementation("io.netty:netty-codec:4.1.99.Final")
implementation("io.netty:netty-common:4.1.99.Final")
implementation("io.netty:netty-handler:4.1.99.Final")
implementation("io.netty:netty-transport:4.1.99.Final")
implementation 'com.google.dagger:dagger:2.27'
annotationProcessor 'com.google.dagger:dagger-compiler:2.27'
implementation("org.jctools:jctools-core:2.1.2")
implementation("org.jetbrains:annotations:16.0.3")
2.android添加方法和log类如下:
//在Application或初始化的时候添加AndroidInternalLogger类。
@Override
public void onCreate() {
InternalLoggerFactory.setDefaultFactory(new InternalLoggerFactory() {
@Override
protected InternalLogger newInstance(String name) {
return new AndroidInternalLogger(name); //名字即为tag
}
});
...
}
添加AndroidInternalLogger类实现如下:
import android.util.Log;
import java.net.UnknownHostException;
import io.netty.util.internal.logging.InternalLogLevel;
import io.netty.util.internal.logging.InternalLogger;
/**
* @Author zhikong
* @Date 17:55
*/
public class AndroidInternalLogger implements InternalLogger {
private String TAG;
public AndroidInternalLogger(String name) {
TAG = name;
}
@Override
public String name() {
return TAG;
}
@Override
public boolean isTraceEnabled() {
return true;
}
@Override
public void trace(String msg) {
Log.v(TAG, msg);
}
@Override
public void trace(String format, Object arg) {
Log.v(TAG, String.format(format, arg));
}
@Override
public void trace(String format, Object argA, Object argB) {
Log.v(TAG, String.format(format, argA, argB));
}
@Override
public void trace(String format, Object... arguments) {
Log.v(TAG, String.format(format, arguments));
}
@Override
public void trace(String msg, Throwable t) {
Log.v(TAG, msg, t);
}
@Override
public void trace(Throwable t) {
Log.v(TAG, "trace", t);
}
@Override
public boolean isDebugEnabled() {
return true;
}
@Override
public void debug(String msg) {
Log.d(TAG, msg);
}
@Override
public void debug(String format, Object arg) {
Log.d(TAG, String.format(format, arg));
}
@Override
public void debug(String format, Object argA, Object argB) {
Log.d(TAG, String.format(format, argA, argB));
}
@Override
public void debug(String format, Object... arguments) {
Log.d(TAG, String.format(format, arguments));
}
@Override
public void debug(String msg, Throwable t) {
Log.d(TAG, msg, t);
}
@Override
public void debug(Throwable t) {
Log.d(TAG, "debug", t);
}
@Override
public boolean isInfoEnabled() {
return true;
}
@Override
public void info(String msg) {
Log.i(TAG, msg);
}
@Override
public void info(String format, Object arg) {
Log.i(TAG, String.format(format, arg));
}
@Override
public void info(String format, Object argA, Object argB) {
Log.i(TAG, String.format(format, argA, argB));
}
@Override
public void info(String format, Object... arguments) {
Log.i(TAG, String.format(format, arguments));
}
@Override
public void info(String msg, Throwable t) {
Log.i(TAG, msg, t);
}
@Override
public void info(Throwable t) {
Log.i(TAG, "info", t);
}
@Override
public boolean isWarnEnabled() {
return true;
}
@Override
public void warn(String msg) {
Log.w(TAG, msg);
}
@Override
public void warn(String format, Object arg) {
Log.w(TAG, String.format(format, arg));
}
@Override
public void warn(String format, Object argA, Object argB) {
Log.w(TAG, String.format(format, argA, argB));
}
@Override
public void warn(String format, Object... arguments) {
Log.w(TAG, String.format(format, arguments));
}
@Override
public void warn(String msg, Throwable t) {
Log.w(TAG, msg, t);
}
@Override
public void warn(Throwable t) {
Log.w(TAG, "warn", t);
}
@Override
public boolean isErrorEnabled() {
return true;
}
@Override
public void error(String msg) {
Log.e(TAG, msg);
}
@Override
public void error(String format, Object arg) {
Log.e(TAG, String.format(format, arg));
}
@Override
public void error(String format, Object argA, Object argB) {
Log.e(TAG, String.format(format, argA, argB));
}
@Override
public void error(String format, Object... arguments) {
Log.e(TAG, String.format(format, arguments));
}
@Override
public void error(String msg, Throwable t) {
if (t instanceof UnknownHostException) {
Log.e(TAG, msg + ":" + t.getMessage());
} else {
Log.e(TAG, msg, t);
}
}
@Override
public void error(Throwable t) {
Log.e(TAG, "error", t);
}
@Override
public boolean isEnabled(InternalLogLevel level) {
return true;
}
@Override
public void log(InternalLogLevel level, String msg) {
switch (level) {
case DEBUG:
Log.d(TAG, msg);
break;
case INFO:
Log.i(TAG, msg);
break;
case WARN:
Log.w(TAG, msg);
break;
case ERROR:
Log.e(TAG, msg);
break;
default:
Log.v(TAG, msg); // 默认情况下,如果没有匹配的级别,可以使用VERBOSE级别
}
}
@Override
public void log(InternalLogLevel level, String format, Object arg) {
switch (level) {
case DEBUG:
Log.d(TAG, String.format(format, arg));
break;
case INFO:
Log.i(TAG, String.format(format, arg));
break;
case WARN:
Log.w(TAG, String.format(format, arg));
break;
case ERROR:
Log.e(TAG, String.format(format, arg));
break;
default:
Log.v(TAG, String.format(format, arg)); // 默认情况下,如果没有匹配的级别,可以使用VERBOSE级别
}
}
@Override
public void log(InternalLogLevel level, String format, Object argA, Object argB) {
switch (level) {
case DEBUG:
Log.d(TAG, String.format(format, argA, argB));
break;
case INFO:
Log.i(TAG, String.format(format, argA, argB));
break;
case WARN:
Log.w(TAG, String.format(format, argA, argB));
break;
case ERROR:
Log.e(TAG, String.format(format, argA, argB));
break;
default:
Log.v(TAG, String.format(format, argA, argB)); // 默认情况下,如果没有匹配的级别,可以使用VERBOSE级别
}
}
@Override
public void log(InternalLogLevel level, String format, Object... arguments) {
switch (level) {
case DEBUG:
Log.d(TAG, String.format(format, arguments));
break;
case INFO:
Log.i(TAG, String.format(format, arguments));
break;
case WARN:
Log.w(TAG, String.format(format, arguments));
break;
case ERROR:
Log.e(TAG, String.format(format, arguments));
break;
default:
Log.v(TAG, String.format(format, arguments)); // 默认情况下,如果没有匹配的级别,可以使用VERBOSE级别
}
}
@Override
public void log(InternalLogLevel level, String msg, Throwable t) {
switch (level) {
case DEBUG:
Log.d(TAG, msg, t);
break;
case INFO:
Log.i(TAG, msg, t);
break;
case WARN:
Log.w(TAG, msg, t);
break;
case ERROR:
Log.e(TAG, msg, t);
break;
default:
Log.v(TAG, msg, t); // 默认情况下,如果没有匹配的级别,可以使用VERBOSE级别
}
}
@Override
public void log(InternalLogLevel level, Throwable t) {
switch (level) {
case DEBUG:
Log.d(TAG, "Exception", t);
break;
case INFO:
Log.i(TAG, "Exception", t);
break;
case WARN:
Log.w(TAG, "Exception", t);
break;
case ERROR:
Log.e(TAG, "Exception", t);
break;
default:
Log.v(TAG, "Exception", t); // 默认情况下,如果没有匹配的级别,可以使用VERBOSE级别
}
}
}
网友评论