Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change velocity logging to console

I'm trying to integrate velocity with an existing log4j.xml configuration and am hitting a wall. I can't seem to get it to use the console appender - no matter what I've tried it keeps sending out to velocity.log.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration
    xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender
        name="consoleAppender"
        class="org.apache.log4j.ConsoleAppender">
        <layout
            class="org.apache.log4j.PatternLayout">
            <param
                name="ConversionPattern"
                value="%d | %5p | %m%n" />
        </layout>
    </appender>

    <logger
        name="runtime.log.logsystem.log4j.category">
        <level
            value="info" />
        <appender-ref
            ref="consoleAppender" />
    </logger>

    <root>
        <priority
            value="info" />
        <appender-ref
            ref="consoleAppender" />
    </root>

</log4j:configuration>

And the java code:

Velocity.setProperty( "runtime.log.logsystem.class", "org.apache.velocity.runtime.log.Log4JLogChute" );

Does anyone know how to make this work properly?

TIA

like image 957
javamonkey79 Avatar asked Jan 19 '11 17:01

javamonkey79


2 Answers

I got it to work by adding the following property:

Velocity.setProperty( "runtime.log.logsystem.log4j.logger", "foo" );

And changing this:

<logger
    name="runtime.log.logsystem.log4j.category">
    <level
        value="info" />
    <appender-ref
        ref="consoleAppender" />
</logger>

to this:

<logger
    name="foo">
    <level
        value="info" />
    <appender-ref
        ref="consoleAppender" />
</logger>

Hope this helps someone else.


EDIT #1:

Finally it could be done by adding the following property:

Velocity.setProperty( "runtime.log.logsystem.log4j.logger", "root" );

or if velocity.properties is used

runtime.log.logsystem.log4j.logger = root

I was then able to change my log4j.xml file back to how I had it, this effectively changed velocity from logging to it's default velocity.log to where my root logger was configed - one line...go figure :)

like image 92
javamonkey79 Avatar answered Oct 16 '22 19:10

javamonkey79


Had to dive in debug to make this work with spring.

The caveat is to set overrideLogging to false. That prevents spring from overriding velocity logger with org.springframework.ui.velocity.CommonsLoggingLogSystem.

<bean id="velocity" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="overrideLogging" value="false"/>
    <property name="velocityProperties">
        <props>
            <prop key="runtime.log.logsystem.class">org.apache.velocity.runtime.log.Log4JLogChute</prop>
            <prop key="runtime.log.logsystem.log4j.logger">velocity</prop>
            <!--...-->
    </property>
</bean>
like image 42
Vadzim Avatar answered Oct 16 '22 20:10

Vadzim