velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apetre...@apache.org
Subject svn commit: r1043112 - in /velocity/engine/trunk: velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/ velocity-engine-slf4j/ velocity-engine-slf4j/src/main/java/org/ velocity-engine-slf4j/src/main/java/org/apache/ velocity-eng...
Date Tue, 07 Dec 2010 16:17:34 GMT
Author: apetrelli
Date: Tue Dec  7 16:17:33 2010
New Revision: 1043112

URL: http://svn.apache.org/viewvc?rev=1043112&view=rev
Log:
VELOCITY-621
Created Slf4jLogChute and added test code.

Added:
    velocity/engine/trunk/velocity-engine-slf4j/src/main/java/org/
    velocity/engine/trunk/velocity-engine-slf4j/src/main/java/org/apache/
    velocity/engine/trunk/velocity-engine-slf4j/src/main/java/org/apache/velocity/
    velocity/engine/trunk/velocity-engine-slf4j/src/main/java/org/apache/velocity/slf4j/
    velocity/engine/trunk/velocity-engine-slf4j/src/main/java/org/apache/velocity/slf4j/Slf4jLogChute.java
    velocity/engine/trunk/velocity-engine-slf4j/src/test/java/org/
    velocity/engine/trunk/velocity-engine-slf4j/src/test/java/org/apache/
    velocity/engine/trunk/velocity-engine-slf4j/src/test/java/org/apache/velocity/
    velocity/engine/trunk/velocity-engine-slf4j/src/test/java/org/apache/velocity/slf4j/
    velocity/engine/trunk/velocity-engine-slf4j/src/test/java/org/apache/velocity/slf4j/Slf4jLogChuteTest.java
Modified:
    velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties
    velocity/engine/trunk/velocity-engine-slf4j/   (props changed)
    velocity/engine/trunk/velocity-engine-slf4j/pom.xml

Modified: velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties?rev=1043112&r1=1043111&r2=1043112&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/resources/org/apache/velocity/runtime/defaults/velocity.properties
Tue Dec  7 16:17:33 2010
@@ -20,10 +20,10 @@
 # ----------------------------------------------------------------------------
 
 # ----------------------------------------------------------------------------
-#  default LogChute to use: default: Log4JLogChute, CommonsLogLogChute, ServletLogChute,
JdkLogChute
+#  default LogChute to use: default: Log4JLogChute, Slf4jLogChute, CommonsLogLogChute, ServletLogChute,
JdkLogChute
 # ----------------------------------------------------------------------------
 
-runtime.log.logsystem.class = org.apache.velocity.runtime.log.Log4JLogChute,org.apache.velocity.runtime.log.CommonsLogLogChute,org.apache.velocity.runtime.log.ServletLogChute,org.apache.velocity.runtime.log.JdkLogChute
+runtime.log.logsystem.class = org.apache.velocity.runtime.log.Log4JLogChute,org.apache.velocity.slf4j.Slf4jLogChute,org.apache.velocity.runtime.log.CommonsLogLogChute,org.apache.velocity.runtime.log.ServletLogChute,org.apache.velocity.runtime.log.JdkLogChute
 
 # ---------------------------------------------------------------------------
 # This is the location of the Velocity Runtime log.

Propchange: velocity/engine/trunk/velocity-engine-slf4j/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Tue Dec  7 16:17:33 2010
@@ -1 +1,2 @@
 target
+.*

Modified: velocity/engine/trunk/velocity-engine-slf4j/pom.xml
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-slf4j/pom.xml?rev=1043112&r1=1043111&r2=1043112&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-slf4j/pom.xml (original)
+++ velocity/engine/trunk/velocity-engine-slf4j/pom.xml Tue Dec  7 16:17:33 2010
@@ -11,4 +11,55 @@
     <version>2.0.0-SNAPSHOT</version>
     <name>Apache Velocity Engine - SLF4J support</name>
     <description>Support for SLF4J logging system.</description>
+    <dependencies>
+    	<dependency>
+    		<groupId>org.slf4j</groupId>
+    		<artifactId>slf4j-api</artifactId>
+    		<version>1.6.1</version>
+    	</dependency>
+    	<dependency>
+    		<groupId>org.slf4j</groupId>
+    		<artifactId>slf4j-jdk14</artifactId>
+    		<version>1.6.1</version>
+            <optional>true</optional>
+    	</dependency>
+    	<dependency>
+    		<groupId>org.apache.velocity</groupId>
+    		<artifactId>velocity-engine-core</artifactId>
+    		<version>2.0.0-SNAPSHOT</version>
+    	</dependency>
+    	<dependency>
+    		<groupId>junit</groupId>
+    		<artifactId>junit</artifactId>
+    		<version>4.8.2</version>
+    		<scope>test</scope>
+    	</dependency>
+    	<dependency>
+    		<groupId>org.powermock</groupId>
+    		<artifactId>powermock-module-junit4</artifactId>
+    		<version>1.4.6</version>
+    		<scope>test</scope>
+    	</dependency>
+    	<dependency>
+    		<groupId>org.powermock</groupId>
+    		<artifactId>powermock-api-easymock</artifactId>
+    		<version>1.4.6</version>
+    		<scope>test</scope>
+    	</dependency>
+    	<dependency>
+    		<groupId>cglib</groupId>
+    		<artifactId>cglib</artifactId>
+    		<version>2.2</version>
+    		<scope>test</scope>
+    	</dependency>
+    	<dependency>
+    		<groupId>org.easymock</groupId>
+    		<artifactId>easymock</artifactId>
+    		<version>3.0</version>
+    		<scope>test</scope>
+    	</dependency>
+    </dependencies>
+    <properties>
+    	<powermock.version>1.4.6</powermock.version>
+    </properties>
 </project>
\ No newline at end of file

Added: velocity/engine/trunk/velocity-engine-slf4j/src/main/java/org/apache/velocity/slf4j/Slf4jLogChute.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-slf4j/src/main/java/org/apache/velocity/slf4j/Slf4jLogChute.java?rev=1043112&view=auto
==============================================================================
--- velocity/engine/trunk/velocity-engine-slf4j/src/main/java/org/apache/velocity/slf4j/Slf4jLogChute.java
(added)
+++ velocity/engine/trunk/velocity-engine-slf4j/src/main/java/org/apache/velocity/slf4j/Slf4jLogChute.java
Tue Dec  7 16:17:33 2010
@@ -0,0 +1,151 @@
+package org.apache.velocity.slf4j;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.velocity.runtime.RuntimeServices;
+import org.apache.velocity.runtime.log.LogChute;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Redirects Velocity's LogChute messages to SLF4J.
+ *
+ * <p>To use, first set up SLF4J, then tell Velocity to use
+ * this class for logging by adding the following to your velocity.properties:
+ *
+ * <code>
+ * runtime.log.logsystem.class = org.apache.velocity.slf4j.Slf4jLogChute
+ * </code>
+ * </p>
+ *
+ * <p>You may also set this property to specify what log/name Velocity's
+ * messages should be logged to (example below is default).
+ * <code>
+ * runtime.log.logsystem.slf4j.name = org.apache.velocity
+ * </code>
+ * </p>
+ *
+ * @since 2.0
+ * @version $Id$
+ */
+public class Slf4jLogChute implements LogChute
+{
+
+    /** Property key for specifying the name for the log instance */
+    public static final String LOGCHUTE_SLF4J_NAME =
+        "runtime.log.logsystem.slf4j.name";
+
+    /** Default name for the commons-logging instance */
+    public static final String DEFAULT_LOG_NAME = "org.apache.velocity";
+
+
+    /** the commons-logging Log instance */
+    protected Logger log;
+
+
+    /********** LogChute methods *************/
+
+    public void init(RuntimeServices rs)
+    {
+        String name =
+            (String)rs.getProperty(LOGCHUTE_SLF4J_NAME);
+
+        if (name == null)
+        {
+            name = DEFAULT_LOG_NAME;
+        }
+        log = LoggerFactory.getLogger(name);
+        log(LogChute.DEBUG_ID, "Slf4jLogChute name is '" + name + "'");
+    }
+
+    /**
+     * Send a log message from Velocity.
+     */
+    public void log(int level, String message)
+    {
+        switch (level)
+        {
+            case LogChute.WARN_ID:
+                log.warn(message);
+                break;
+            case LogChute.INFO_ID:
+                log.info(message);
+                break;
+            case LogChute.TRACE_ID:
+                log.trace(message);
+                break;
+            case LogChute.ERROR_ID:
+                log.error(message);
+                break;
+            case LogChute.DEBUG_ID:
+            default:
+                log.debug(message);
+                break;
+        }
+    }
+
+    /**
+     * Send a log message from Velocity with an error.
+     */
+    public void log(int level, String message, Throwable t)
+    {
+        switch (level)
+        {
+            case LogChute.WARN_ID:
+                log.warn(message, t);
+                break;
+            case LogChute.INFO_ID:
+                log.info(message, t);
+                break;
+            case LogChute.TRACE_ID:
+                log.trace(message, t);
+                break;
+            case LogChute.ERROR_ID:
+                log.error(message, t);
+                break;
+            case LogChute.DEBUG_ID:
+            default:
+                log.debug(message, t);
+                break;
+        }
+    }
+
+    /**
+     * Checks whether the specified log level is enabled.
+     */
+    public boolean isLevelEnabled(int level)
+    {
+        switch (level)
+        {
+            case LogChute.DEBUG_ID:
+                return log.isDebugEnabled();
+            case LogChute.INFO_ID:
+                return log.isInfoEnabled();
+            case LogChute.TRACE_ID:
+                return log.isTraceEnabled();
+            case LogChute.WARN_ID:
+                return log.isWarnEnabled();
+            case LogChute.ERROR_ID:
+                return log.isErrorEnabled();
+            default:
+                return true;
+        }
+    }
+}

Added: velocity/engine/trunk/velocity-engine-slf4j/src/test/java/org/apache/velocity/slf4j/Slf4jLogChuteTest.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-slf4j/src/test/java/org/apache/velocity/slf4j/Slf4jLogChuteTest.java?rev=1043112&view=auto
==============================================================================
--- velocity/engine/trunk/velocity-engine-slf4j/src/test/java/org/apache/velocity/slf4j/Slf4jLogChuteTest.java
(added)
+++ velocity/engine/trunk/velocity-engine-slf4j/src/test/java/org/apache/velocity/slf4j/Slf4jLogChuteTest.java
Tue Dec  7 16:17:33 2010
@@ -0,0 +1,101 @@
+/**
+ *
+ */
+package org.apache.velocity.slf4j;
+
+import static org.easymock.EasyMock.expect;
+import static org.powermock.api.easymock.PowerMock.*;
+import static org.junit.Assert.*;
+
+import org.apache.velocity.runtime.log.LogChute;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.reflect.Whitebox;
+import org.slf4j.Logger;
+
+/**
+ * Tests {@link Slf4jLogChute}.
+ *
+ * @version $Id$
+ */
+@RunWith(PowerMockRunner.class)
+@SuppressStaticInitializationFor("org.apache.velocity.Slf4jLogChute")
+public class Slf4jLogChuteTest {
+
+	/**
+	 * Test method for {@link org.apache.velocity.slf4j.Slf4jLogChute#log(int, java.lang.String)}.
+	 */
+	@Test
+	public void testLogIntString() {
+		Logger logger = createNiceMock(Logger.class);
+		Slf4jLogChute chute = new Slf4jLogChute();
+		Whitebox.setInternalState(chute, Logger.class, logger);
+
+		logger.trace("trace message");
+		logger.debug("debug message");
+		logger.info("info message");
+		logger.warn("warn message");
+		logger.error("error message");
+
+		replay(logger);
+		chute.log(LogChute.TRACE_ID, "trace message");
+		chute.log(LogChute.DEBUG_ID, "debug message");
+		chute.log(LogChute.INFO_ID, "info message");
+		chute.log(LogChute.WARN_ID, "warn message");
+		chute.log(LogChute.ERROR_ID, "error message");
+		verify(logger);
+	}
+
+	/**
+	 * Test method for {@link org.apache.velocity.slf4j.Slf4jLogChute#log(int, java.lang.String,
java.lang.Throwable)}.
+	 */
+	@Test
+	public void testLogIntStringThrowable() {
+		Logger logger = createNiceMock(Logger.class);
+		Slf4jLogChute chute = new Slf4jLogChute();
+		Whitebox.setInternalState(chute, Logger.class, logger);
+
+		Throwable t = new Throwable("test throwable");
+
+		logger.trace("trace message", t);
+		logger.debug("debug message", t);
+		logger.info("info message", t);
+		logger.warn("warn message", t);
+		logger.error("error message", t);
+
+		replay(logger);
+		chute.log(LogChute.TRACE_ID, "trace message", t);
+		chute.log(LogChute.DEBUG_ID, "debug message", t);
+		chute.log(LogChute.INFO_ID, "info message", t);
+		chute.log(LogChute.WARN_ID, "warn message", t);
+		chute.log(LogChute.ERROR_ID, "error message", t);
+		verify(logger);
+	}
+
+	/**
+	 * Test method for {@link org.apache.velocity.slf4j.Slf4jLogChute#isLevelEnabled(int)}.
+	 */
+	@Test
+	public void testIsLevelEnabled() {
+		Logger logger = createNiceMock(Logger.class);
+		Slf4jLogChute chute = new Slf4jLogChute();
+		Whitebox.setInternalState(chute, Logger.class, logger);
+
+		expect(logger.isTraceEnabled()).andReturn(false);
+		expect(logger.isDebugEnabled()).andReturn(false);
+		expect(logger.isInfoEnabled()).andReturn(true);
+		expect(logger.isWarnEnabled()).andReturn(true);
+		expect(logger.isErrorEnabled()).andReturn(true);
+
+		replay(logger);
+		assertFalse(chute.isLevelEnabled(LogChute.TRACE_ID));
+		assertFalse(chute.isLevelEnabled(LogChute.DEBUG_ID));
+		assertTrue(chute.isLevelEnabled(LogChute.INFO_ID));
+		assertTrue(chute.isLevelEnabled(LogChute.WARN_ID));
+		assertTrue(chute.isLevelEnabled(LogChute.ERROR_ID));
+		verify(logger);
+	}
+
+}



Mime
View raw message