我有一个带有logback作为日志记录功能的J2EE Web应用程序 并使用Tomcat 7作为服务器。 我现在正在使用logback ConsoleAppender,所以我可以去 /var/log/tomcat7/catalina.out检查日志。
现在,我想将日志移动到一个文件中。我将使用RollingFileAppender来保存日志。
我不想使用绝对路径,如:
<appender name="FILE" class="xxx.RollingFileAppender">
<file>/var/log/testFile1.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
我不想使用系统环境变量,例如:
<appender name="FILE" class="xxx.RollingFileAppender">
<file>{LOG_LOCATION}\testFile2.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
如果我只在标签中写下日志文件的名称,例如:
<appender name="FILE" class="xxx.RollingFileAppender">
<file>testFile3.log</file>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
我会收到错误:
21:29:45,265 |-ERROR in ch.qos.logback.core.FileAppender[TEST_LOG] - openFile(testFile3.log,true) call failed. java.io.FileNotFoundException: testFile3.log (Permission denied)
at java.io.FileNotFoundException: testFile3.log (Permission denied)
at at java.io.FileOutputStream.open(Native Method)
at at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at at ch.qos.logback.core.recovery.ResilientFileOutputStream.<init>(ResilientFileOutputStream.java:28)
at at ch.qos.logback.core.FileAppender.openFile(FileAppender.java:149)
at at ch.qos.logback.core.FileAppender.start(FileAppender.java:108)
at at ch.qos.logback.core.joran.action.AppenderAction.end(AppenderAction.java:96)
at at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:317)
at at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:196)
at at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:182)
at at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:62)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:149)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:135)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:99)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:49)
at at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75)
at at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:148)
at at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:85)
at at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55)
at at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
at at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:107)
at at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:295)
at at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:269)
at at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:156)
at at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
at at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:657)
at at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:282)
at at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)
at at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
at at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)
at at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)
at at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
at at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:958)
at at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1599)
at at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at at java.lang.Thread.run(Thread.java:744)
我是否可以仅使用其名称创建日志文件? 如果是这样,Tomcat 7服务器中的logback日志文件的默认位置在哪里?
答案 0 :(得分:-1)
您需要相对路径。指定<file>./testFile3.log</file>
通过运行一次来检查创建它的位置。
请注意,这可能与用于启动应用程序的位置有关。