logback : does not creates log file

前提是你 提交于 2019-12-22 04:47:19

问题


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,

my logback.xml file content

    <?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

I repeat again, I tried every solution that exists, but nothing is working


回答1:


Maybe the following link will help you.

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.

My test platform is an web application and this app is running under Apache Tomcat on Windows. Configuration and outputs:


<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

Sometimes the log file is not created, I think, the main reason of it is lack of create file permission of user/application.




回答2:


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

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.

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:

  1. logback.xml Example



回答3:


I also faced the same problem. In my case I was using the eclipse with 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 && deployed in tomcat) then the log files gets created inside the path relative to bin folder of tomcat




回答4:


In my case, it was actually someting I added in the pom.xml that ignores xml property file. After I added

<include>**/*.xml</include>  

there, it worked

<resource>
    <directory>src/main/resources/</directory>
    <includes>
      <include>**/*.json</include>          
      <include>**/*.properties</include>
    </includes>
  </resource>
</resources>



回答5:


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.



来源:https://stackoverflow.com/questions/36935230/logback-does-not-creates-log-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!