2
0
mirror of https://gitee.com/hotlcc/wechat4j.git synced 2025-06-09 03:54:08 +08:00

[update] 日志框架使用方式变更

This commit is contained in:
Allen 2019-04-16 16:51:55 +08:00 committed by lichangchun
parent 9302b2e68c
commit 11f3b99524
6 changed files with 48 additions and 67 deletions

View File

@ -7,6 +7,7 @@ import com.hotlcc.wechat4j.handler.ExitEventHandler;
import com.hotlcc.wechat4j.handler.ReceivedMsgHandler;
import com.hotlcc.wechat4j.model.*;
import com.hotlcc.wechat4j.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
@ -17,8 +18,6 @@ import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
@ -35,9 +34,8 @@ import java.util.concurrent.locks.ReentrantLock;
* @author Allen
*/
@SuppressWarnings({"Duplicates", "unused"})
@Slf4j
public class Wechat {
private static Logger logger = LoggerFactory.getLogger(Wechat.class);
private CookieStore cookieStore;
private HttpClient httpClient;
@ -657,7 +655,7 @@ public class Wechat {
try {
JSONObject result = WebWeixinApiUtil.syncCheck(httpClient, urlVersion, new BaseRequest(wxsid, skey, wxuin), getSyncKeyList(false));
logger.info("微信同步监听心跳返回数据:{}", result);
log.info("微信同步监听心跳返回数据:{}", result);
if (result == null) {
throw new RuntimeException("微信API调用异常");
} else {
@ -667,7 +665,7 @@ public class Wechat {
//人为退出
int retcode = result.getIntValue("retcode");
if (retcode != RetcodeEnum.RECODE_0.getCode()) {
logger.info("微信退出或从其它设备登录");
log.info("微信退出或从其它设备登录");
logout();
processExitEvent(ExitTypeEnum.REMOTE_EXIT, null);
return;
@ -676,16 +674,16 @@ public class Wechat {
int selector = result.getIntValue("selector");
processSelector(selector);
} catch (Exception e) {
logger.error("同步监听心跳异常", e);
log.error("同步监听心跳异常", e);
if (i == 0) {
logger.info("同步监听请求失败,正在重试...");
log.info("同步监听请求失败,正在重试...");
} else if (i > 0) {
logger.info("第{}次重试失败" + i);
log.info("第{}次重试失败" + i);
}
if (i >= time) {
logger.info("重复{}次仍然失败,退出微信", i);
log.info("重复{}次仍然失败,退出微信", i);
logout();
processExitEvent(ExitTypeEnum.ERROR_EXIT, e);
return;
@ -722,7 +720,7 @@ public class Wechat {
}
}
} catch (Exception e) {
logger.error("Exit event process error.", e);
log.error("Exit event process error.", e);
}
}
@ -742,13 +740,13 @@ public class Wechat {
break;
}
} catch (Exception e) {
logger.error("Exit event process error.", e);
log.error("Exit event process error.", e);
}
try {
handler.handleAllType(wechat, type, t);
} catch (Exception e) {
logger.error("Exit event process error.", e);
log.error("Exit event process error.", e);
}
}
@ -761,7 +759,7 @@ public class Wechat {
try {
SelectorEnum e = SelectorEnum.valueOf(selector);
if (e == null) {
logger.warn("Cannot process unknow selector {}", selector);
log.warn("Cannot process unknow selector {}", selector);
return;
}
@ -781,7 +779,7 @@ public class Wechat {
break;
}
} catch (Exception e) {
logger.error("Execute processSelector error.", e);
log.error("Execute processSelector error.", e);
}
}
@ -792,19 +790,19 @@ public class Wechat {
try {
JSONObject result = WebWeixinApiUtil.webWxSync(httpClient, urlVersion, passTicket, new BaseRequest(wxsid, skey, wxuin), syncKey);
if (result == null) {
logger.error("从服务端同步新数据异常");
log.error("从服务端同步新数据异常");
return;
}
JSONObject baseResponse = result.getJSONObject("BaseResponse");
if (baseResponse == null) {
logger.warn("同步接口返回数据格式错误");
log.warn("同步接口返回数据格式错误");
return;
}
int ret = baseResponse.getIntValue("Ret");
if (ret != RetcodeEnum.RECODE_0.getCode()) {
logger.warn("同步接口返回错误代码:{}", ret);
log.warn("同步接口返回错误代码:{}", ret);
return;
}
@ -820,7 +818,7 @@ public class Wechat {
syncKeyLock.unlock();
}
} catch (Exception e) {
logger.error("Execute webWxSync error.", e);
log.error("Execute webWxSync error.", e);
}
}
@ -836,10 +834,10 @@ public class Wechat {
}
int len = addMsgList.size();
logger.debug("收到{}条新消息", len);
log.debug("收到{}条新消息", len);
if (receivedMsgHandlers == null || receivedMsgHandlers.isEmpty()) {
logger.warn("收到{}条新消息,但没有配置消息处理器", len);
log.warn("收到{}条新消息,但没有配置消息处理器", len);
return;
}
@ -852,7 +850,7 @@ public class Wechat {
}
}
} catch (Exception e) {
logger.error("Execute processNewMsg error.", e);
log.error("Execute processNewMsg error.", e);
}
}
@ -860,7 +858,7 @@ public class Wechat {
try {
handler.handleAllType(wechat, msg);
} catch (Exception e) {
logger.error("Execute processNewMsg error.", e);
log.error("Execute processNewMsg error.", e);
}
}
}

View File

@ -1,16 +1,14 @@
package com.hotlcc.wechat4j.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
/**
* 通用工具类
*
* @author Allen
*/
@Slf4j
public final class CommonUtil {
private static Logger logger = LoggerFactory.getLogger(CommonUtil.class);
private CommonUtil() {
}

View File

@ -1,8 +1,7 @@
package com.hotlcc.wechat4j.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.activation.MimetypesFileTypeMap;
import java.io.ByteArrayOutputStream;
@ -15,9 +14,8 @@ import java.io.IOException;
*
* @author Allen
*/
@Slf4j
public final class FileUtil {
private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
private FileUtil() {
}

View File

@ -1,7 +1,6 @@
package com.hotlcc.wechat4j.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.InputStream;
@ -12,9 +11,8 @@ import java.util.Properties;
*
* @author Allen
*/
@Slf4j
public final class PropertiesUtil {
private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
private PropertiesUtil() {
}
@ -38,7 +36,7 @@ public final class PropertiesUtil {
is = PropertiesUtil.class.getClassLoader().getResourceAsStream(path);
prop.load(is);
} catch (Exception e) {
logger.error("Loading properties file \"" + path + "\" error.", e);
log.error("Loading properties file \"" + path + "\" error.", e);
} finally {
if (is != null) {
try {

View File

@ -7,8 +7,7 @@ import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.hotlcc.wechat4j.enums.OperatingSystemEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
@ -23,9 +22,8 @@ import java.io.IOException;
* @author Allen
*/
@SuppressWarnings({"unused", "WeakerAccess"})
@Slf4j
public final class QRCodeUtil {
private static Logger logger = LoggerFactory.getLogger(QRCodeUtil.class);
private QRCodeUtil() {
}
@ -252,7 +250,7 @@ public final class QRCodeUtil {
fos.flush();
return tmp;
} catch (IOException e) {
logger.error("二维码写入临时文件异常", e);
log.error("二维码写入临时文件异常", e);
throw new RuntimeException(e);
} finally {
if (fos != null) {

View File

@ -6,6 +6,7 @@ import com.hotlcc.wechat4j.enums.LoginTipEnum;
import com.hotlcc.wechat4j.model.BaseRequest;
import com.hotlcc.wechat4j.model.MediaMessage;
import com.hotlcc.wechat4j.model.WxMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
@ -20,8 +21,6 @@ import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.XML;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.stringtemplate.v4.ST;
import java.math.BigDecimal;
@ -37,12 +36,11 @@ import java.util.regex.Pattern;
* @author Allen
*/
@SuppressWarnings({"Duplicates", "unused"})
@Slf4j
public final class WebWeixinApiUtil {
private WebWeixinApiUtil() {
}
private static Logger logger = LoggerFactory.getLogger(WebWeixinApiUtil.class);
/**
* 预编译正则匹配
*/
@ -108,7 +106,7 @@ public final class WebWeixinApiUtil {
return result;
} catch (Exception e) {
logger.error("获取uuid异常", e);
log.error("获取uuid异常", e);
return null;
}
}
@ -144,7 +142,7 @@ public final class WebWeixinApiUtil {
return data;
} catch (Exception e) {
logger.error("获取二维码异常", e);
log.error("获取二维码异常", e);
return null;
}
}
@ -216,7 +214,7 @@ public final class WebWeixinApiUtil {
return result;
} catch (Exception e) {
logger.error("获取跳转uri异常", e);
log.error("获取跳转uri异常", e);
return null;
}
}
@ -250,7 +248,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(XML.toJSONObject(res).toString()).getJSONObject("error");
} catch (Exception e) {
logger.error("获取登录认证码异常", e);
log.error("获取登录认证码异常", e);
return null;
}
}
@ -287,7 +285,7 @@ public final class WebWeixinApiUtil {
httpClient.execute(httpPost);
}
} catch (Exception e) {
logger.error("退出登录异常", e);
log.error("退出登录异常", e);
}
}
@ -322,7 +320,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res);
} catch (Exception e) {
logger.error("push登录异常", e);
log.error("push登录异常", e);
return null;
}
}
@ -366,7 +364,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res);
} catch (Exception e) {
logger.error("获取初始化数据异常", e);
log.error("获取初始化数据异常", e);
return null;
}
}
@ -415,7 +413,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res);
} catch (Exception e) {
logger.error("开启消息状态通知异常", e);
log.error("开启消息状态通知异常", e);
return null;
}
}
@ -474,7 +472,7 @@ public final class WebWeixinApiUtil {
return result;
} catch (Exception e) {
logger.error("服务端状态同步异常", e);
log.error("服务端状态同步异常", e);
return null;
}
}
@ -513,7 +511,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res);
} catch (Exception e) {
logger.error("获取全部联系人列表异常", e);
log.error("获取全部联系人列表异常", e);
return null;
}
}
@ -561,7 +559,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res);
} catch (Exception e) {
logger.error("批量获取指定联系人信息异常", e);
log.error("批量获取指定联系人信息异常", e);
return null;
}
}
@ -609,7 +607,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res);
} catch (Exception e) {
logger.error("从服务端同步新数据异常", e);
log.error("从服务端同步新数据异常", e);
return null;
}
}
@ -656,7 +654,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res);
} catch (Exception e) {
logger.error("发送消息异常", e);
log.error("发送消息异常", e);
return null;
}
}
@ -693,7 +691,6 @@ public final class WebWeixinApiUtil {
long millis = System.currentTimeMillis();
int mediaLength = mediaData.length;
// String mimeType = contentType.getMimeType();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-type", ContentType.MULTIPART_FORM_DATA.toString());
@ -722,14 +719,8 @@ public final class WebWeixinApiUtil {
HttpEntity paramEntity = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
// .addTextBody("id", StringUtil.getUuid(), ContentType.TEXT_PLAIN)
// .addTextBody("name", mediaName, ContentType.TEXT_PLAIN)
// .addTextBody("type", mimeType, ContentType.TEXT_PLAIN)
// .addTextBody("lastModifieDate", String.valueOf(millis), ContentType.TEXT_PLAIN)
// .addTextBody("size", String.valueOf(mediaLength), ContentType.TEXT_PLAIN)
.addTextBody("chunks", String.valueOf(chunks))
.addTextBody("chunk", String.valueOf(chunk))
// .addTextBody("mediatype", WechatUtil.getMediatype(mimeType), ContentType.TEXT_PLAIN)
.addTextBody("uploadmediarequest", uploadmediarequest.toJSONString(), ContentType.TEXT_PLAIN)
.addTextBody("webwx_data_ticket", dataTicket, ContentType.TEXT_PLAIN)
.addTextBody("pass_ticket", passticket, ContentType.TEXT_PLAIN)
@ -763,7 +754,7 @@ public final class WebWeixinApiUtil {
return result;
} catch (Exception e) {
logger.error("上传媒体文件异常", e);
log.error("上传媒体文件异常", e);
return null;
}
}
@ -810,7 +801,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res);
} catch (Exception e) {
logger.error("发送图片消息异常", e);
log.error("发送图片消息异常", e);
return null;
}
}