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

View File

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

View File

@ -1,7 +1,6 @@
package com.hotlcc.wechat4j.util; package com.hotlcc.wechat4j.util;
import org.slf4j.Logger; import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -12,9 +11,8 @@ import java.util.Properties;
* *
* @author Allen * @author Allen
*/ */
@Slf4j
public final class PropertiesUtil { public final class PropertiesUtil {
private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
private PropertiesUtil() { private PropertiesUtil() {
} }
@ -38,7 +36,7 @@ public final class PropertiesUtil {
is = PropertiesUtil.class.getClassLoader().getResourceAsStream(path); is = PropertiesUtil.class.getClassLoader().getResourceAsStream(path);
prop.load(is); prop.load(is);
} catch (Exception e) { } catch (Exception e) {
logger.error("Loading properties file \"" + path + "\" error.", e); log.error("Loading properties file \"" + path + "\" error.", e);
} finally { } finally {
if (is != null) { if (is != null) {
try { 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.BitMatrix;
import com.google.zxing.common.HybridBinarizer; import com.google.zxing.common.HybridBinarizer;
import com.hotlcc.wechat4j.enums.OperatingSystemEnum; import com.hotlcc.wechat4j.enums.OperatingSystemEnum;
import org.slf4j.Logger; import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@ -23,9 +22,8 @@ import java.io.IOException;
* @author Allen * @author Allen
*/ */
@SuppressWarnings({"unused", "WeakerAccess"}) @SuppressWarnings({"unused", "WeakerAccess"})
@Slf4j
public final class QRCodeUtil { public final class QRCodeUtil {
private static Logger logger = LoggerFactory.getLogger(QRCodeUtil.class);
private QRCodeUtil() { private QRCodeUtil() {
} }
@ -252,7 +250,7 @@ public final class QRCodeUtil {
fos.flush(); fos.flush();
return tmp; return tmp;
} catch (IOException e) { } catch (IOException e) {
logger.error("二维码写入临时文件异常", e); log.error("二维码写入临时文件异常", e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (fos != null) { 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.BaseRequest;
import com.hotlcc.wechat4j.model.MediaMessage; import com.hotlcc.wechat4j.model.MediaMessage;
import com.hotlcc.wechat4j.model.WxMessage; import com.hotlcc.wechat4j.model.WxMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.*; import org.apache.http.*;
import org.apache.http.client.HttpClient; 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.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.json.XML; import org.json.XML;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.stringtemplate.v4.ST; import org.stringtemplate.v4.ST;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -37,12 +36,11 @@ import java.util.regex.Pattern;
* @author Allen * @author Allen
*/ */
@SuppressWarnings({"Duplicates", "unused"}) @SuppressWarnings({"Duplicates", "unused"})
@Slf4j
public final class WebWeixinApiUtil { public final class WebWeixinApiUtil {
private WebWeixinApiUtil() { private WebWeixinApiUtil() {
} }
private static Logger logger = LoggerFactory.getLogger(WebWeixinApiUtil.class);
/** /**
* 预编译正则匹配 * 预编译正则匹配
*/ */
@ -108,7 +106,7 @@ public final class WebWeixinApiUtil {
return result; return result;
} catch (Exception e) { } catch (Exception e) {
logger.error("获取uuid异常", e); log.error("获取uuid异常", e);
return null; return null;
} }
} }
@ -144,7 +142,7 @@ public final class WebWeixinApiUtil {
return data; return data;
} catch (Exception e) { } catch (Exception e) {
logger.error("获取二维码异常", e); log.error("获取二维码异常", e);
return null; return null;
} }
} }
@ -216,7 +214,7 @@ public final class WebWeixinApiUtil {
return result; return result;
} catch (Exception e) { } catch (Exception e) {
logger.error("获取跳转uri异常", e); log.error("获取跳转uri异常", e);
return null; return null;
} }
} }
@ -250,7 +248,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(XML.toJSONObject(res).toString()).getJSONObject("error"); return JSONObject.parseObject(XML.toJSONObject(res).toString()).getJSONObject("error");
} catch (Exception e) { } catch (Exception e) {
logger.error("获取登录认证码异常", e); log.error("获取登录认证码异常", e);
return null; return null;
} }
} }
@ -287,7 +285,7 @@ public final class WebWeixinApiUtil {
httpClient.execute(httpPost); httpClient.execute(httpPost);
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("退出登录异常", e); log.error("退出登录异常", e);
} }
} }
@ -322,7 +320,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res); return JSONObject.parseObject(res);
} catch (Exception e) { } catch (Exception e) {
logger.error("push登录异常", e); log.error("push登录异常", e);
return null; return null;
} }
} }
@ -366,7 +364,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res); return JSONObject.parseObject(res);
} catch (Exception e) { } catch (Exception e) {
logger.error("获取初始化数据异常", e); log.error("获取初始化数据异常", e);
return null; return null;
} }
} }
@ -415,7 +413,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res); return JSONObject.parseObject(res);
} catch (Exception e) { } catch (Exception e) {
logger.error("开启消息状态通知异常", e); log.error("开启消息状态通知异常", e);
return null; return null;
} }
} }
@ -474,7 +472,7 @@ public final class WebWeixinApiUtil {
return result; return result;
} catch (Exception e) { } catch (Exception e) {
logger.error("服务端状态同步异常", e); log.error("服务端状态同步异常", e);
return null; return null;
} }
} }
@ -513,7 +511,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res); return JSONObject.parseObject(res);
} catch (Exception e) { } catch (Exception e) {
logger.error("获取全部联系人列表异常", e); log.error("获取全部联系人列表异常", e);
return null; return null;
} }
} }
@ -561,7 +559,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res); return JSONObject.parseObject(res);
} catch (Exception e) { } catch (Exception e) {
logger.error("批量获取指定联系人信息异常", e); log.error("批量获取指定联系人信息异常", e);
return null; return null;
} }
} }
@ -609,7 +607,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res); return JSONObject.parseObject(res);
} catch (Exception e) { } catch (Exception e) {
logger.error("从服务端同步新数据异常", e); log.error("从服务端同步新数据异常", e);
return null; return null;
} }
} }
@ -656,7 +654,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res); return JSONObject.parseObject(res);
} catch (Exception e) { } catch (Exception e) {
logger.error("发送消息异常", e); log.error("发送消息异常", e);
return null; return null;
} }
} }
@ -693,7 +691,6 @@ public final class WebWeixinApiUtil {
long millis = System.currentTimeMillis(); long millis = System.currentTimeMillis();
int mediaLength = mediaData.length; int mediaLength = mediaData.length;
// String mimeType = contentType.getMimeType();
HttpPost httpPost = new HttpPost(url); HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-type", ContentType.MULTIPART_FORM_DATA.toString()); httpPost.setHeader("Content-type", ContentType.MULTIPART_FORM_DATA.toString());
@ -722,14 +719,8 @@ public final class WebWeixinApiUtil {
HttpEntity paramEntity = MultipartEntityBuilder.create() HttpEntity paramEntity = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .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("chunks", String.valueOf(chunks))
.addTextBody("chunk", String.valueOf(chunk)) .addTextBody("chunk", String.valueOf(chunk))
// .addTextBody("mediatype", WechatUtil.getMediatype(mimeType), ContentType.TEXT_PLAIN)
.addTextBody("uploadmediarequest", uploadmediarequest.toJSONString(), ContentType.TEXT_PLAIN) .addTextBody("uploadmediarequest", uploadmediarequest.toJSONString(), ContentType.TEXT_PLAIN)
.addTextBody("webwx_data_ticket", dataTicket, ContentType.TEXT_PLAIN) .addTextBody("webwx_data_ticket", dataTicket, ContentType.TEXT_PLAIN)
.addTextBody("pass_ticket", passticket, ContentType.TEXT_PLAIN) .addTextBody("pass_ticket", passticket, ContentType.TEXT_PLAIN)
@ -763,7 +754,7 @@ public final class WebWeixinApiUtil {
return result; return result;
} catch (Exception e) { } catch (Exception e) {
logger.error("上传媒体文件异常", e); log.error("上传媒体文件异常", e);
return null; return null;
} }
} }
@ -810,7 +801,7 @@ public final class WebWeixinApiUtil {
return JSONObject.parseObject(res); return JSONObject.parseObject(res);
} catch (Exception e) { } catch (Exception e) {
logger.error("发送图片消息异常", e); log.error("发送图片消息异常", e);
return null; return null;
} }
} }