问题描述
我正在为Glassfish服务器开发Java Web应用程序.我在设置File Appender的路径时遇到问题.我想使用在web.xml中定义的变量.
I am developing Java web application for Glassfish server. I have problem with setting path for File appender. I would like to use variable which is defined in web.xml.
Web.xml:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>app.myApp-client.home</param-value>
</context-param>
Logback.xml(在src/main/resurces中)
Logback.xml (in src/main/resurces)
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${app.myApp-client.home}/WEB-INF/app-log/client.log</file>
<encoder>
<pattern>%date %level [%file:%line] %msg%n</pattern>
</encoder>
</appender>
将我的应用程序部署在名称为
After deploy my application on Glassfish log file with name
已创建.为什么参数app.myApp-client.home未定义?有没有更好的选择将应用程序路径放置到文件追加器?
is created. Why parameter app.myApp-client.home is undefined? Is there any better option for put application path to file appender?
谢谢.
推荐答案
可以将变量设置为值通过JNDI获得.对于您的情况,您应该写:
It is possible to set variables to values obtained via JNDI. In your case, you would write:
<insertFromJNDI env-entry-name="java:comp/env/webAppRootKey" as="webAppRootKey" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${webAppRootKey}/WEB-INF/app-log/client.log</file>
<encoder>
<pattern>%date %level [%file:%line] %msg%n</pattern>
</encoder>
</appender>
以上内容应该可以很好地工作.此外,您可能还会发现为设置默认值很有用. >.
The above should work nicely. In addition, you might also find it useful to set a default value for ${webAppRootKey}
.
这篇关于文件附加程序中来自web.xml的logback值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!