Log4j2 WebLookup not getting resolved

南楼画角 提交于 2019-12-22 10:01:56

问题


I have the following log4j2 configuration

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE">
    <Appenders>
        <RollingRandomAccessFile name="SQLTiming" fileName="${web:rootDir}/log/SQLTiming.log"
            filePattern="${web:rootDir}/log/SQLTiming-%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{2} - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingRandomAccessFile>
        <Console name="CONSOLE">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{2} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="jdbc.sqltiming" level="info" additivity="false">
            <AppenderRef ref="SQLTiming" />
        </Logger>
        <Root level="error">
            <AppenderRef ref="CONSOLE" />
        </Root>
    </Loggers>
</Configuration>

When I try to get a logger via slf4j

    protected static Logger logger = LoggerFactory.getLogger(DbConn.class);

Log4j fails creating an appender. I've looked at the log4j2 source, and it tries to create the file C:\Program Files\eclipse\${web:rootDir}\log\SQLTiming.log (which it fails to do), so it seems that log4j didn't process the lookup.

I am running the application in Tomcat 7.0.4, and using the syntax suggested by http://logging.apache.org/log4j/2.x/manual/lookups.html . I have tried using $${web:rootDir} (with 2 $ marks) which resulted in one of the $-s being removed, but the lookup still didn't succeed. In fact, none of the other lookups succeeded (environment variables for example). Log4j version is 2.0 beta9.

Has anybody else seen anything similar to this ? Am I missing something ?


回答1:


For servlet 3.0 container you must configure log4jContextName in web.xml as below:

<context-param>
    <param-name>log4jContextName</param-name>
    <param-value>log4jContext</param-value>
</context-param>



回答2:


Are you running in Tomcat standalone, or are you running in an Eclipse plugin? If Log4J cannot find a value for the lookup it will create a file that has the pattern in the name (which is what you're seeing). So the lookup is failing. The fact that the file it tries to create is under the Eclipse installation dir tells me that the process that you're running is Eclipse, not Tomcat. Which plugin are you using?

It could be that when running Tomcat as an Eclipse plugin, not all ${web:...} environment variables are set correctly. I would expect things to work correctly when you run with the above settings in Tomcat standalone, can you verify this?

I'm not sure the Log4J team can do anything about this, but you can try raising this issue at https://issues.apache.org/jira/browse/LOG4J2 . You can also try contacting the author of the Eclipse Tomcat plugin.




回答3:


The solution was really following the steps described at

http://logging.apache.org/log4j/2.x/manual/webapp.html

(My app was running in a servlet 2.5 container)

I just somehow missed this piece of documentation (which is excellent by the way)



来源:https://stackoverflow.com/questions/19003114/log4j2-weblookup-not-getting-resolved

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!