我在 Maven 中创建了一个使用 logback 的测试网络应用程序。这是我的 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>
我已将此文件复制到 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" );
}
}
之后,我运行 mvn install
并将生成的 war 文件(在 WEB-INF/classes
下有 logback.xml)部署到我的本地 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());
}
}
但是,除了“Served at:/myapp”输出之外,没有任何反应!没有任何内容写入日志文件,也没有写入控制台。我什至没有在控制台上收到任何错误消息。就好像 logback 正在悄无声息地失败一样。
LogService 类只是 logback 调用的包装器:
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 );
}
}
我尝试在我的 Java 启动选项中设置 logback.configurationFile
属性,并以编程方式设置它,详见此处 ( Setting logback.xml path programmatically)。
但是我用这两种方法得到了完全相同的结果:mvn test
很好地记录了消息,但是当我在本地主机上运行测试 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());
现在 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)
所以这至少解释了为什么我看不到来自 TestServlet 的调试日志语句。现在我只需要弄清楚原因。
最佳答案
听起来您需要覆盖 WebLogic 中的默认 SLF4J 实现。为此,请将以下代码添加到您的 weblogic.xml 文件中:
<wls:container-descriptor>
<wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes>
</wls:container-descriptor>
这将优先考虑您与其他必要的 logback 库一起提供的 SLF4J 实现。
关于配置文件位置,您需要将 logback.xml 文件放在您的域根目录中,它应该类似于:"../user_projects/域/$domain_name"。
希望对您有所帮助。
关于java - logback.xml 文件未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37735890/