[英]LOGBACK: Writing the log to a file in the classpath
I've recently started with Logback and was going through the official documentation. 我最近开始使用Logback,正在查看官方文档。 While implementing it in one of my projects, I was wondering if there was a way to write the logs to a file on the classpath or somewhere within the target folder/logs/myapp.log?
在我的一个项目中实现它时,我想知道是否可以将日志写入类路径中或目标文件夹/logs/myapp.log中某处的文件中? I could not find much information on the official documentation (yes, I've read variable substitution).
我在官方文档中找不到太多信息(是的,我已经阅读了变量替换)。
Here's my logback.xml: 这是我的logback.xml:
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>clientLog.log</file>
<append>true</append>
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder
by default -->
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
Now I want the clientLog.log file to be created inside the target folder (MAVEN project). 现在,我希望在目标文件夹(MAVEN项目)中创建clientLog.log文件。 Any pointers will be appreciated.
任何指针将不胜感激。
I doubt that you will find a standard mechanism to write to a file on the application's classpath. 我怀疑您会找到一种标准的机制来写入应用程序的类路径上的文件。 The stuff on the classpath is typically read-only.
类路径上的内容通常是只读的。
Similarly you won't find a mechanism that explicitly supports a log in some location in the build environment (eg a "target" directory). 同样,您不会在构建环境中的某个位置(例如“目标”目录)中找到明确支持日志的机制。 Applications typically don't run in the build environment.
应用程序通常不在构建环境中运行。
So the most practical solution is probably to have two separate logging configuration files; 因此,最实用的解决方案可能是拥有两个单独的日志记录配置文件; ie one for your development platform and another for your deployment platform.
即,一个用于您的开发平台,另一个用于您的部署平台。 Use different log file pathnames in the different logging config files.
在不同的日志记录配置文件中使用不同的日志文件路径名。
If the relative pathname target/logs/clientLog.log
is not working, then the chances are that something is changing the "current directory" when the your application (eg the tomcat server) is launching. 如果相对路径名
target/logs/clientLog.log
不起作用,则可能是在启动应用程序(例如tomcat服务器)时,某些东西正在更改“当前目录”。 But (AFAIK) that is normal for Tomcat. 但是(AFAIK)这对于Tomcat是正常的。
If I understood your question then I guess you can achieve your task by setting your path as an environment variable. 如果我理解您的问题,那么我想您可以通过将路径设置为环境变量来完成您的任务。 Here are the things you have to do :
这是您要做的事情:
<property name="LOGDIR" value="${app.workdir}/log" />
<File>${LOGDIR}/${FILE_NAME}-${bySecond}.log</File>
Here your file will get created under the $LOGDIR directory. 在这里,您的文件将在$ LOGDIR目录下创建。 Now the question is how to set "LOGDIR" value.
现在的问题是如何设置“ LOGDIR”值。
In your JAVA application, SET app.workdir as the System variable i,e 在您的JAVA应用程序中,将SET app.workdir设置为系统变量i,e
System.setProperty("app.workdir", "YOUR_TARGET_PATH")
System.setProperty(“ app.workdir”,“ YOUR_TARGET_PATH”)
Let me know if you got confused. 如果您感到困惑,请告诉我。
if you use mac please use the absolute path like ${catalina.base}/userLog.%d{yyyy-MM-dd}.log under tomcat 如果您使用的是Mac,请使用tomcat下的$ {catalina.base} /userLog.%d {yyyy-MM-dd} .log这样的绝对路径
it won't work with the relative path like ../userLog.%d{yyyy-MM-dd}.log so i think it is always good to use some variable 它不会与../userLog.%d{yyyy-MM-dd}.log之类的相对路径一起使用,因此我认为使用一些变量总是很好的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.