change velocity logging to console

2019-04-06 16:05发布

问题:

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

回答1:

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 :)



回答2:

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>


回答3:

Are you sure that your log4j.xml is the logging configuration file picked up by log4j? It's possible that another JAR on the classpath is including it's own bundled log4j config file.

Launch your application with -Dlog4j.debug to have log4j spit out information about which file it is loading the configuration from, to verify the intended file is being used.