我正在尝试使用我的Web应用程序设置Logback。我的logback.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds">
<appender name="tests_file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>tests.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>tests.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="application_file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>application.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>application.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="tests" level="DEBUG">
<appender-ref ref="tests_file" />
</logger>
<logger name="application" level="INFO">
<appender-ref ref="application_file" />
</logger>
</configuration>
此文件位于WEB-INF / classes。
我有一个带有以下构造函数的servlet:
public class FrontController extends HttpServlet {
private Logger logger;
public FrontController() {
// Initialise the logger
logger = (Logger)LoggerFactory.getLogger("application");
logger.info("Starting application");
}
}
当我在Tomcat中启动servlet时,没有任何内容写入我在logback.xml中声明的application.log文件。 LoggerFactory是不是找不到xml文件,还是我的xml文件错了?
由于
答案 0 :(得分:1)
好吧,当它打开时,相对路径很糟糕。因此,我没有在文件条目中使用简单的文件名,而是使用了以下行:
<file>${user.dir}/logs/application.log</file>
现在servlet日志记录写入../logs/application.log下的eclipse安装文件夹。一切都很好。