Browse Source

use slf4j for logging

pull/32/head
Jochen Berger 11 years ago
parent
commit
4e23372528
  1. 4
      json-path/pom.xml
  2. 10
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  3. 50
      json-path/src/main/java/com/jayway/jsonpath/internal/Log.java
  4. 22
      json-path/src/test/java/com/jayway/jsonpath/LogTest.java
  5. 15
      json-path/src/test/resources/logback-test.xml
  6. 28
      pom.xml

4
json-path/pom.xml

@ -34,6 +34,10 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>

10
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -16,14 +16,16 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.IOUtils;
import com.jayway.jsonpath.internal.Log;
import com.jayway.jsonpath.internal.PathToken;
import com.jayway.jsonpath.internal.PathTokenizer;
import com.jayway.jsonpath.internal.filter.PathTokenFilter;
import com.jayway.jsonpath.spi.HttpProviderFactory;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.JsonProviderFactory;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
@ -105,6 +107,8 @@ public class JsonPath {
private PathTokenizer tokenizer;
private LinkedList<Filter> filters;
private final Logger logger = LoggerFactory.getLogger(JsonPath.class);
private JsonPath(String jsonPath, Filter[] filters) {
@ -122,9 +126,7 @@ public class JsonPath {
this.tokenizer = new PathTokenizer(jsonPath);
if(Log.isDebugEnabled()){
Log.debug("New JsonPath:\n{}", this.tokenizer.toString());
}
logger.debug("New JsonPath:\n{}", this.tokenizer);
this.filters = new LinkedList<Filter>();
this.filters.addAll(asList(filters));

50
json-path/src/main/java/com/jayway/jsonpath/internal/Log.java

@ -1,50 +0,0 @@
package com.jayway.jsonpath.internal;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* User: kalle
* Date: 8/28/13
* Time: 10:23 AM
*/
public final class Log {
private Log() {
}
private static boolean enabled = false;
public static void enableDebug(){
enabled = true;
}
public static boolean isDebugEnabled(){
return enabled;
}
public static void debug(String msg, Object... args){
if(enabled){
int argCount = StringUtils.countMatches(msg, "{}");
if(!(argCount == args.length)){
throw new RuntimeException("Invalid debug statement.");
}
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
String cls = stackTraceElements[2].getClassName();
msg = msg.replaceFirst(Pattern.quote("{}"), "%s");
msg = String.format(msg, args);
System.out.println("DEBUG [" + Thread.currentThread().getName() + "] " + cls + " " + msg);
}
}
}

22
json-path/src/test/java/com/jayway/jsonpath/LogTest.java

@ -1,22 +0,0 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.Log;
import org.junit.Test;
/**
* User: kalle
* Date: 8/28/13
* Time: 10:40 AM
*/
public class LogTest {
@Test
public void logger_expands_templates() {
Log.enableDebug();
Log.debug("foo \n{}", "bar");
}
}

15
json-path/src/test/resources/logback-test.xml

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<contextName>myAppName</contextName>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-5level [%t] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.jayway.jsonpath" level="TRACE"/>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>

28
pom.xml

@ -57,7 +57,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.7.5</slf4j.version>
<logback.version>1.0.13</logback.version>
<junit.version>4.10</junit.version>
<commons-io.version>2.4</commons-io.version>
<commons-lang.version>3.1</commons-lang.version>
@ -125,7 +124,16 @@
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<systemPropertyVariables>
<org.slf4j.simpleLogger.defaultLogLevel>debug</org.slf4j.simpleLogger.defaultLogLevel>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
@ -232,11 +240,11 @@
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@ -268,13 +276,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>

Loading…
Cancel
Save