Java logback : 不创建日志文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36935230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
logback : does not creates log file
提问by h.zak
first of all: I tried every solution that exist, but nothing is working, so I don't want from anyone to say this question is duplicated
首先:我尝试了所有存在的解决方案,但没有任何效果,所以我不希望任何人说这个问题是重复的
I cannot log to the file using logback,but I can log to console without problems,
我无法使用logback登录到文件,但我可以毫无问题地登录到控制台,
my logback.xmlfile content
我的logback.xml文件内容
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- 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>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--See http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
<!--and http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy-->
<!--for further documentation-->
<append>true</append>
<File>/root/connector/logs/connector.log</File>
<encoder>
<!-- was: %d{yyyy-MM-dd HH:mm:ss}%5p [%t] (%F:%L) - %msg%n -->
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] \(%class{25}:%line\) - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- By setting the name to .gz here, we get free compression. -->
<fileNamePattern>/root/connector/logs/connector.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
I tried even to give all users the permission to write in the folder, but this doesn't work
我什至尝试授予所有用户在文件夹中写入的权限,但这不起作用
drwxrwxrwx. 2 nobody nobody 4096 Apr 29 08:24 logs
drwxrwxrwx。2 没人没人 4096 Apr 29 08:24 日志
I repeat again, I tried every solution that exists, but nothing is working
我再次重复,我尝试了所有存在的解决方案,但没有任何效果
回答by SkyWalker
Add this portion
添加这部分
<logger name="com.my.package" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</logger>
<!-- By default, the level of the root level is set to DEBUG -->
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
instead of
代替
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
Write your project package name instead of com.my.package
写下你的项目包名而不是 com.my.package
Hope it will solve your issue.
希望它能解决你的问题。
Update:
更新:
Send logs to File
将日志发送到文件
All logging will be redirected to a file c:/logs/debug.log
. Furthermore, this log file will be archived daily or the file size is larger than 10MB.
所有日志记录将被重定向到一个文件c:/logs/debug.log
。此外,此日志文件将每天存档或文件大小大于 10MB。
logback.xml
日志文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="c:/logs" />
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.mkyong.web" level="debug"
additivity="false">
<appender-ref ref="FILE-AUDIT" />
</logger>
<root level="error">
<appender-ref ref="FILE-AUDIT" />
</root>
</configuration>
Resource link:
资源链接:
回答by gbii
Maybe the following link will help you.
也许以下链接会对您有所帮助。
https://dzone.com/articles/do-not-use-relative-path
https://dzone.com/articles/do-not-use-relative-path
EDIT: This link says that "don't use relative path with logback". But I found an opportunity to test it. And I found some strange outputs.
编辑:此链接说“不要在 logback 中使用相对路径”。但是我找到了一个测试它的机会。我发现了一些奇怪的输出。
My test platform is an web application and this app is running under Apache Tomcat on Windows. Configuration and outputs:
我的测试平台是一个 Web 应用程序,这个应用程序在 Windows 上的 Apache Tomcat 下运行。 配置和输出:
<file>/logs/output.log</file>
--------------> Creates log file in C:\logs folder
<file>C:/logs/output.log</file>
-----------> Creates log file in C:\logs folder
<file>../logs/output.log</file>
-----------> Creates log file in tomcat logs folder
<file>logs/output.log</file>
---------------> Creates log file in tomcat bin\logs folder
<file>/logs/output.log</file>
--------------> 在 C:\logs 文件夹中<file>C:/logs/output.log</file>
创建日志文件 ----------->在 C:\logs 文件夹中
创建日志文件
<file>../logs/output.log</file>
----- ------> 在 tomcat 日志文件夹中
<file>logs/output.log</file>
创建日志文件 ---------------> 在 tomcat bin\logs 文件夹中创建日志文件
Sometimes the log file is not created, I think, the main reason of it is lack of create file permission of user/application.
有时没有创建日志文件,我认为,其主要原因是缺乏用户/应用程序的创建文件权限。
回答by sanju singh
I also faced the same problem. In my case I was using the eclipse with tomcat.
我也面临同样的问题。在我的情况下,我将 eclipse 与 tomcat 一起使用。
If(absolute path) then there is no problem, log files get created in the path specified.
如果(绝对路径)那么没有问题,日志文件会在指定的路径中创建。
else if (Relative path && working in eclipse) then your log files is created inside the path relative to the eclipse installation directory.
else if (Relative path && working in eclipse) 那么你的日志文件是在相对于 eclipse 安装目录的路径中创建的。
else if (Relative path && deployed in tomcat) then the log files gets created inside the path relative to bin folder of tomcat
else if (Relative path && deploy in tomcat) 然后在相对于 tomcat 的 bin 文件夹的路径中创建日志文件
回答by uudaddy
In my case, it was actually someting I added in the pom.xml that ignores xml property file. After I added
就我而言,它实际上是我在 pom.xml 中添加的一些忽略 xml 属性文件的内容。我添加后
<include>**/*.xml</include>
there, it worked
在那里,它起作用了
<resource>
<directory>src/main/resources/</directory>
<includes>
<include>**/*.json</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
回答by Dani
When I had this problem, I had to add the folder where the logback.log file was as a source folder. In eclipse just do right click on the folder -> Build Path-> Use as Source Folder.
当我遇到这个问题时,我不得不添加 logback.log 文件所在的文件夹作为源文件夹。在 Eclipse 中,只需右键单击文件夹 -> 构建路径 -> 用作源文件夹。