Ere*_*ren 1 java xml logback maven
我在Maven中创建了一个使用logback的测试webapp.这是我的logback.xml配置文件:
<configuration scan="true">
<!-- For more information: http://logback.qos.ch/manual/configuration.html -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>D:/Oracle/user_projects/domains/base_domain/servers/AdminServer/logs/myapp.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>D:/Oracle/user_projects/domains/base_domain/servers/AdminServer/logs/archive/myapp.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 31 days' worth of history -->
<maxHistory>31</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date [%level] [%thread] [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我已将此文件复制到两个src/main/resources和src/test/resources
当我跑步时mvn test,一切正常.测试日志消息将写入控制台以及myapp.log文件:
public class TestLogService extends TestCase {
public TestLogService( String name ) {
super( name );
}
public void testLogMessage() throws Exception {
LogService.debug( "Hello from {}!", "TestLogService" );
}
}
Run Code Online (Sandbox Code Playgroud)
之后,我运行mvn install并将生成的war文件(其下有logback.xml WEB-INF/classes)部署到我的本地WebLogic服务器.完成后,我点击了一个测试servlet,它应该只将一个语句记录到日志文件中:
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TestServlet() {
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
LogService.debug( "Hello from {}!", "doGet()" );
response.getWriter().append("Served at: ").append(request.getContextPath());
}
}
Run Code Online (Sandbox Code Playgroud)
但是,除了"服务于:/ myapp"输出外,没有任何反应!没有任何内容写入日志文件,也没有写入控制台.我甚至没有在控制台上收到任何错误消息.这就好像logback无声地失败或其他东西.
LogService类只是回调调用的包装器:
public class LogService {
private static Logger logger = LoggerFactory.getLogger("com.myapp");
public static void debug( String message ) {
logger.debug( message );
}
public static void debug( String message, Object... argArray ) {
logger.debug( message, argArray );
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试logback.configurationFile在我的Java启动选项中设置属性,并在此处详细设置它(以编程方式设置logback.xml路径).
但是我得到了两种方法完全相同的结果:mvn test记录消息就好了,但是当我在localhost上运行测试servlet时没有任何反应.
知道问题是什么吗?
附加信息
我向LogService添加了一些调试语句:
System.out.println("[LogService.debug] isDebugEnabled = " + logger.isDebugEnabled());
System.out.println("[LogService.debug] getName = " + logger.getName());
System.out.println("[LogService.debug] ROOT_LOGGER_NAME = " + Logger.ROOT_LOGGER_NAME);
System.out.println("[LogService.debug] toString = " + logger.toString());
Run Code Online (Sandbox Code Playgroud)
现在mvn test,点击TestServlet给出不同的输出:
mvn test says: TestServlet says:
----------------------------------------------- -----------------------------------------------
[LogService.debug] isDebugEnabled = true [LogService.debug] isDebugEnabled = false
[LogService.debug] getName = com.myapp [LogService.debug] getName = com.myapp
[LogService.debug] ROOT_LOGGER_NAME = ROOT [LogService.debug] ROOT_LOGGER_NAME = ROOT
[LogService.debug] toString = Logger[com.myapp] [LogService.debug] toString = org.slf4j.impl.JDK14LoggerAdapter(com.myapp)
Run Code Online (Sandbox Code Playgroud)
这至少解释了为什么我没有看到TestServlet的调试日志语句.现在我只需弄明白为什么.
小智 5
听起来您需要覆盖WebLogic中的默认SLF4J实现.为此,请将以下代码添加到weblogic.xml文件中:
<wls:container-descriptor>
<wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes>
</wls:container-descriptor>
Run Code Online (Sandbox Code Playgroud)
这将优先考虑您随其他必要的logback库提供的SLF4J实现.
关于配置文件位置,您将要将logback.xml文件放在域根目录中,该文件应类似于:"../ user_projects/domain/$ domain_name".
我希望这有帮助.
| 归档时间: |
|
| 查看次数: |
1935 次 |
| 最近记录: |