issue #23 添加日志输出功能,使用了slf4j

This commit is contained in:
Daniel Qian
2015-01-21 14:43:34 +08:00
parent e53c921d17
commit 5ff2ab6436
4 changed files with 231 additions and 207 deletions

418
pom.xml
View File

@@ -1,219 +1,231 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<project <project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>me.chanjar</groupId> <groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId> <artifactId>weixin-java-parent</artifactId>
<version>1.0.7-SNAPSHOT</version> <version>1.0.7-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>WeiXin Java Tools - Parent</name> <name>WeiXin Java Tools - Parent</name>
<description>微信公众号、企业号上级POM</description> <description>微信公众号、企业号上级POM</description>
<url>https://github.com/chanjarster/weixin-java-tools</url>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<name>Daniel Qian</name>
<email>chanjarster@gmail.com</email>
</developer>
</developers>
<scm>
<connection>scm:git:https://github.com/chanjarster/weixin-java-tools.git</connection>
<developerConnection>scm:git:git@github.com:chanjarster/weixin-java-tools.git</developerConnection>
<url>https://github.com/chanjarster/weixin-java-tools</url> <url>https://github.com/chanjarster/weixin-java-tools</url>
</scm>
<licenses> <modules>
<license> <module>weixin-java-common</module>
<name>The Apache License, Version 2.0</name> <module>weixin-java-cp</module>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> <module>weixin-java-mp</module>
</license> </modules>
</licenses>
<developers> <properties>
<developer> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<name>Daniel Qian</name> <downloadJavadocs>true</downloadJavadocs>
<email>chanjarster@gmail.com</email> <downloadSources>true</downloadSources>
</developer> <httpclient.version>4.3.5</httpclient.version>
</developers> <slf4j.version>1.7.10</slf4j.version>
</properties>
<scm> <dependencies>
<connection>scm:git:https://github.com/chanjarster/weixin-java-tools.git</connection> <dependency>
<developerConnection>scm:git:git@github.com:chanjarster/weixin-java-tools.git</developerConnection> <groupId>org.slf4j</groupId>
<url>https://github.com/chanjarster/weixin-java-tools</url> <artifactId>slf4j-api</artifactId>
</scm> <version>${slf4j.version}</version>
</dependency>
<modules> <dependency>
<module>weixin-java-common</module> <groupId>org.slf4j</groupId>
<module>weixin-java-cp</module> <artifactId>slf4j-simple</artifactId>
<module>weixin-java-mp</module> <version>${slf4j.version}</version>
</modules> <scope>test</scope>
</dependency>
<properties> <dependency>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <groupId>org.apache.httpcomponents</groupId>
<downloadJavadocs>true</downloadJavadocs> <artifactId>fluent-hc</artifactId>
<downloadSources>true</downloadSources> <version>${httpclient.version}</version>
<httpclient.version>4.3.5</httpclient.version> </dependency>
</properties> <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>junit</groupId>
<artifactId>fluent-hc</artifactId> <artifactId>junit</artifactId>
<version>${httpclient.version}</version> <version>4.11</version>
</dependency> <scope>test</scope>
<dependency> </dependency>
<groupId>org.apache.httpcomponents</groupId> <dependency>
<artifactId>httpmime</artifactId> <groupId>com.google.inject</groupId>
<version>${httpclient.version}</version> <artifactId>guice</artifactId>
</dependency> <version>3.0</version>
<dependency> <scope>test</scope>
<groupId>com.google.code.gson</groupId> </dependency>
<artifactId>gson</artifactId> <dependency>
<version>2.2.2</version> <groupId>org.testng</groupId>
</dependency> <artifactId>testng</artifactId>
<dependency> <version>6.8.7</version>
<groupId>commons-codec</groupId> <scope>test</scope>
<artifactId>commons-codec</artifactId> </dependency>
<version>1.9</version> <dependency>
</dependency> <groupId>org.mockito</groupId>
<dependency> <artifactId>mockito-all</artifactId>
<groupId>commons-io</groupId> <version>1.9.5</version>
<artifactId>commons-io</artifactId> <scope>test</scope>
<version>2.4</version> </dependency>
</dependency> <dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.3.0.M0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.3.0.M0</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</dependencyManagement>
<dependencyManagement> <distributionManagement>
<dependencies> <snapshotRepository>
<dependency> <id>ossrh</id>
<groupId>junit</groupId> <url>https://oss.sonatype.org/content/repositories/snapshots</url>
<artifactId>junit</artifactId> </snapshotRepository>
<version>4.11</version> <repository>
<scope>test</scope> <id>ossrh</id>
</dependency> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
<dependency> </repository>
<groupId>com.google.inject</groupId> </distributionManagement>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.3.0.M0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.3.0.M0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<charset>UTF-8</charset>
<locale>zh_CN</locale>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
</plugins>
</pluginManagement>
<profiles>
<profile>
<id>release</id>
<build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.sonatype.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId> <artifactId>maven-source-plugin</artifactId>
<version>1.6.3</version> <version>2.2.1</version>
<extensions>true</extensions> <executions>
<configuration> <execution>
<serverId>ossrh</serverId> <id>attach-sources</id>
<nexusUrl>https://oss.sonatype.org/</nexusUrl> <goals>
<autoReleaseAfterClose>false</autoReleaseAfterClose> <goal>jar-no-fork</goal>
</configuration> </goals>
</plugin> </execution>
<plugin> </executions>
<groupId>org.apache.maven.plugins</groupId> </plugin>
<artifactId>maven-release-plugin</artifactId> <plugin>
<version>2.5</version> <groupId>org.apache.maven.plugins</groupId>
<configuration> <artifactId>maven-javadoc-plugin</artifactId>
<autoVersionSubmodules>true</autoVersionSubmodules> <version>2.9.1</version>
<useReleaseProfile>false</useReleaseProfile> <executions>
<releaseProfiles>release</releaseProfiles> <execution>
<goals>deploy</goals> <id>attach-javadocs</id>
</configuration> <goals>
</plugin> <goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<charset>UTF-8</charset>
<locale>zh_CN</locale>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
</profile>
</profiles>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>false</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>false</useReleaseProfile>
<releaseProfiles>release</releaseProfiles>
<goals>deploy</goals>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@@ -1,12 +1,16 @@
package me.chanjar.weixin.common.session; package me.chanjar.weixin.common.session;
import me.chanjar.weixin.common.util.res.StringManager; import me.chanjar.weixin.common.util.res.StringManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class SessionManagerImpl implements WxSessionManager, InternalSessionManager { public class SessionManagerImpl implements WxSessionManager, InternalSessionManager {
protected final Logger log = LoggerFactory.getLogger(SessionManagerImpl.class);
protected static final StringManager sm = protected static final StringManager sm =
StringManager.getManager(Constants.Package); StringManager.getManager(Constants.Package);
@@ -226,7 +230,7 @@ public class SessionManagerImpl implements WxSessionManager, InternalSessionMana
int expireHere = 0 ; int expireHere = 0 ;
if(log.isDebugEnabled()) if(log.isDebugEnabled())
log.debug("Start expire sessions " + getName() + " at " + timeNow + " sessioncount " + sessions.length); log.debug("Start expire sessions {} at {} sessioncount {}", getName(), timeNow, sessions.length);
for (int i = 0; i < sessions.length; i++) { for (int i = 0; i < sessions.length; i++) {
if (sessions[i]!=null && !sessions[i].isValid()) { if (sessions[i]!=null && !sessions[i].isValid()) {
expireHere++; expireHere++;
@@ -234,7 +238,7 @@ public class SessionManagerImpl implements WxSessionManager, InternalSessionMana
} }
long timeEnd = System.currentTimeMillis(); long timeEnd = System.currentTimeMillis();
if(log.isDebugEnabled()) if(log.isDebugEnabled())
log.debug("End expire sessions " + getName() + " processingTime " + (timeEnd - timeNow) + " expired sessions: " + expireHere); log.debug("End expire sessions {} processingTime {} expired sessions: {}", getName(), timeEnd - timeNow, expireHere);
processingTime += ( timeEnd - timeNow ); processingTime += ( timeEnd - timeNow );
} }

View File

@@ -34,6 +34,8 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -45,6 +47,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class WxCpServiceImpl implements WxCpService { public class WxCpServiceImpl implements WxCpService {
protected final Logger log = LoggerFactory.getLogger(WxCpServiceImpl.class);
/** /**
* 全局的是否正在刷新access token的锁 * 全局的是否正在刷新access token的锁
*/ */
@@ -380,7 +384,7 @@ public class WxCpServiceImpl implements WxCpService {
if (error.getErrorCode() == -1) { if (error.getErrorCode() == -1) {
int sleepMillis = retrySleepMillis * (1 << retryTimes); int sleepMillis = retrySleepMillis * (1 << retryTimes);
try { try {
System.out.println("微信系统繁忙," + sleepMillis + "ms后重试(第" + (retryTimes + 1) + "次)"); log.debug("微信系统繁忙,{}ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
Thread.sleep(sleepMillis); Thread.sleep(sleepMillis);
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
throw new RuntimeException(e1); throw new RuntimeException(e1);

View File

@@ -31,6 +31,8 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -42,6 +44,8 @@ import java.util.UUID;
public class WxMpServiceImpl implements WxMpService { public class WxMpServiceImpl implements WxMpService {
protected final Logger log = LoggerFactory.getLogger(WxMpServiceImpl.class);
/** /**
* 全局的是否正在刷新access token的锁 * 全局的是否正在刷新access token的锁
*/ */
@@ -462,7 +466,7 @@ public class WxMpServiceImpl implements WxMpService {
if (error.getErrorCode() == -1) { if (error.getErrorCode() == -1) {
int sleepMillis = retrySleepMillis * (1 << retryTimes); int sleepMillis = retrySleepMillis * (1 << retryTimes);
try { try {
System.out.println("微信系统繁忙," + sleepMillis + "ms后重试(第" + (retryTimes + 1) + "次)"); log.debug("微信系统繁忙,{}ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
Thread.sleep(sleepMillis); Thread.sleep(sleepMillis);
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
throw new RuntimeException(e1); throw new RuntimeException(e1);