Skip to content

Commit 1a958d0

Browse files
committed
Merge pull request marceloverdijk#25 from davidmc24/optional-slf4j
Logging: replace required Commons Logging with optional SLF4J (fallback to java util logging)
2 parents 4f52125 + 8d2043d commit 1a958d0

File tree

10 files changed

+142
-23
lines changed

10 files changed

+142
-23
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ Non-Maven users should download the latest version and add it to the project's c
4242

4343
+ <a href="http://commons.apache.org/io/">Apache Commons IO 2.4</a>
4444
+ <a href="http://commons.apache.org/lang/">Apache Commons Lang 3.1</a>
45-
+ <a href="http://commons.apache.org/logging/">Apache Commons Logging 1.1.1</a>
4645
+ <a href="http://www.mozilla.org/rhino/">Rhino: JavaScript for Java 1.7R4</a>
4746

47+
If [SLF4J](http://www.slf4j.org/) is present in the classpath, it will be used for logging.
4848

4949
Compatibility
5050
-------------

pom.xml

+12-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
<artifactId>commons-io</artifactId>
2727
<version>2.4</version>
2828
</dependency>
29-
<dependency>
30-
<groupId>commons-logging</groupId>
31-
<artifactId>commons-logging</artifactId>
32-
<version>1.1.1</version>
33-
</dependency>
3429
<dependency>
3530
<groupId>junit</groupId>
3631
<artifactId>junit</artifactId>
@@ -71,6 +66,18 @@
7166
<artifactId>rhino</artifactId>
7267
<version>1.7R4</version>
7368
</dependency>
69+
<dependency>
70+
<groupId>org.slf4j</groupId>
71+
<artifactId>slf4j-api</artifactId>
72+
<version>1.7.2</version>
73+
<optional>true</optional>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.slf4j</groupId>
77+
<artifactId>slf4j-simple</artifactId>
78+
<version>1.7.2</version>
79+
<scope>testRuntime</scope>
80+
</dependency>
7481
</dependencies>
7582

7683
<build>

src/main/java/org/lesscss/LessCompiler.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import java.util.Collections;
2323
import java.util.List;
2424
import org.apache.commons.io.FileUtils;
25-
import org.apache.commons.logging.Log;
26-
import org.apache.commons.logging.LogFactory;
25+
import org.lesscss.logging.LessLogger;
26+
import org.lesscss.logging.LessLoggerFactory;
2727
import org.mozilla.javascript.Context;
2828
import org.mozilla.javascript.Function;
2929
import org.mozilla.javascript.JavaScriptException;
@@ -59,9 +59,9 @@
5959
public class LessCompiler {
6060

6161
private static final String COMPILE_STRING = "function doIt(input, compress) { var result; var parser = new less.Parser(); parser.parse(input, function(e, tree) { if (e instanceof Object) { throw e; } ; result = tree.toCSS({compress: compress}); }); return result; }";
62-
63-
private static final Log log = LogFactory.getLog(LessCompiler.class);
64-
62+
63+
private static final LessLogger logger = LessLoggerFactory.getLogger(LessCompiler.class);
64+
6565
private URL envJs = LessCompiler.class.getClassLoader().getResource("META-INF/env.rhino.js");
6666
private URL lessJs = LessCompiler.class.getClassLoader().getResource("META-INF/less.js");
6767
private List<URL> customJs = Collections.emptyList();
@@ -221,6 +221,7 @@ public synchronized void init() {
221221
global.init(cx);
222222

223223
scope = cx.initStandardObjects(global);
224+
scope.put("logger", scope, Context.toObject(logger, scope));
224225

225226
List<URL> jsUrls = new ArrayList<URL>(2 + customJs.size());
226227
jsUrls.add(envJs);
@@ -239,14 +240,14 @@ public synchronized void init() {
239240
}
240241
catch (Exception e) {
241242
String message = "Failed to initialize LESS compiler.";
242-
log.error(message, e);
243+
logger.error(message, e);
243244
throw new IllegalStateException(message, e);
244245
}finally{
245246
Context.exit();
246247
}
247248

248-
if (log.isDebugEnabled()) {
249-
log.debug("Finished initialization of LESS compiler in " + (System.currentTimeMillis() - start) + " ms.");
249+
if (logger.isDebugEnabled()) {
250+
logger.debug("Finished initialization of LESS compiler in " + (System.currentTimeMillis() - start) + " ms.");
250251
}
251252
}
252253

@@ -269,8 +270,8 @@ public String compile(String input) throws LessException {
269270
Context cx = Context.enter();
270271
Object result = doIt.call(cx, scope, null, new Object[]{input, compress});
271272

272-
if (log.isDebugEnabled()) {
273-
log.debug("Finished compilation of LESS source in " + (System.currentTimeMillis() - start) + " ms.");
273+
if (logger.isDebugEnabled()) {
274+
logger.debug("Finished compilation of LESS source in " + (System.currentTimeMillis() - start) + " ms.");
274275
}
275276

276277
return result.toString();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.lesscss.logging;
2+
3+
import java.util.logging.Level;
4+
import java.util.logging.Logger;
5+
6+
class JULILessLoggerProvider implements LessLoggerProvider {
7+
public LessLogger getLogger(Class<?> clazz) {
8+
return new JULILessLogger(Logger.getLogger(clazz.getName()));
9+
}
10+
11+
private static class JULILessLogger implements LessLogger {
12+
private final Logger logger;
13+
14+
private JULILessLogger(Logger logger) {
15+
this.logger = logger;
16+
}
17+
18+
public boolean isDebugEnabled() {
19+
return logger.isLoggable(Level.FINE);
20+
}
21+
22+
public boolean isInfoEnabled() {
23+
return logger.isLoggable(Level.INFO);
24+
}
25+
26+
public void debug(String msg) {
27+
logger.fine(msg);
28+
}
29+
30+
public void info(String msg) {
31+
logger.info(msg);
32+
}
33+
34+
public void error(String msg, Throwable t) {
35+
logger.log(Level.SEVERE, msg, t);
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.lesscss.logging;
2+
3+
public interface LessLogger {
4+
boolean isDebugEnabled();
5+
6+
boolean isInfoEnabled();
7+
8+
void debug(String msg);
9+
10+
void info(String msg);
11+
12+
void error(String msg, Throwable t);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.lesscss.logging;
2+
3+
public class LessLoggerFactory {
4+
private static LessLoggerFactory instance = new LessLoggerFactory();
5+
private LessLoggerProvider loggerProvider;
6+
7+
private LessLoggerFactory() {
8+
try {
9+
Class.forName("org.slf4j.Logger");
10+
loggerProvider = new SLF4JLessLoggerProvider();
11+
} catch(ClassNotFoundException ex) {
12+
loggerProvider = new JULILessLoggerProvider();
13+
}
14+
}
15+
16+
public static LessLogger getLogger(Class<?> clazz) {
17+
return instance.loggerProvider.getLogger(clazz);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.lesscss.logging;
2+
3+
interface LessLoggerProvider {
4+
LessLogger getLogger(Class<?> clazz);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.lesscss.logging;
2+
3+
class SLF4JLessLoggerProvider implements LessLoggerProvider {
4+
public LessLogger getLogger(Class<?> clazz) {
5+
return new SLF4JLessLogger(org.slf4j.LoggerFactory.getLogger(clazz));
6+
}
7+
8+
private static class SLF4JLessLogger implements LessLogger {
9+
private final org.slf4j.Logger logger;
10+
11+
private SLF4JLessLogger(org.slf4j.Logger logger) {
12+
this.logger = logger;
13+
}
14+
15+
public boolean isDebugEnabled() {
16+
return logger.isDebugEnabled();
17+
}
18+
19+
public boolean isInfoEnabled() {
20+
return logger.isInfoEnabled();
21+
}
22+
23+
public void debug(String msg) {
24+
logger.debug(msg);
25+
}
26+
27+
public void info(String msg) {
28+
logger.info(msg);
29+
}
30+
31+
public void error(String msg, Throwable t) {
32+
logger.error(msg, t);
33+
}
34+
}
35+
}

src/main/resources/META-INF/env.rhino.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Override the print function so that the messages go to commons logging
1+
// Override the print function so that the messages go to the configured loggerthe configured logger
22
print = function(message) {
3-
Packages.org.apache.commons.logging.LogFactory.getLog('rhino').debug(message);
3+
logger.debug(message);
44
};
55

66
/*

src/test/java/org/lesscss/LessCompilerTest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import static org.junit.Assert.assertEquals;
1818
import static org.junit.Assert.assertTrue;
19+
import static org.mockito.Matchers.anyObject;
1920
import static org.mockito.Matchers.anyString;
2021
import static org.mockito.Mockito.verify;
2122
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@@ -34,10 +35,10 @@
3435
import java.util.List;
3536
import org.apache.commons.io.FileUtils;
3637
import org.apache.commons.lang3.reflect.FieldUtils;
37-
import org.apache.commons.logging.Log;
3838
import org.junit.Before;
3939
import org.junit.Test;
4040
import org.junit.runner.RunWith;
41+
import org.lesscss.logging.LessLogger;
4142
import org.mockito.Mock;
4243
import org.mozilla.javascript.Context;
4344
import org.mozilla.javascript.Function;
@@ -59,7 +60,7 @@ public class LessCompilerTest {
5960

6061
private LessCompiler lessCompiler;
6162

62-
@Mock private Log log;
63+
@Mock private LessLogger logger;
6364

6465
@Mock private Context cx;
6566
@Mock private Global global;
@@ -89,8 +90,8 @@ public class LessCompilerTest {
8990
public void setUp() throws Exception {
9091
lessCompiler = new LessCompiler();
9192

92-
when(log.isDebugEnabled()).thenReturn(false);
93-
FieldUtils.writeField(lessCompiler, "log", log, true);
93+
when(logger.isDebugEnabled()).thenReturn(false);
94+
FieldUtils.writeField(lessCompiler, "logger", logger, true);
9495
}
9596

9697
@Test
@@ -201,7 +202,7 @@ public void testInitThrowsIllegalStateExceptionWhenNotAbleToInitilize() throws E
201202

202203
verify(envJsFile).openConnection();
203204

204-
verify(log).error(anyString());
205+
verify(logger).error(anyString(), (Throwable) anyObject());
205206
}
206207

207208
@Test

0 commit comments

Comments
 (0)