logback : does not creates log file
Asked Answered
T

10

10

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 even tried 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.

Trial answered 29/4, 2016 at 9:55 Comment(2)
Is the file getting created on the directory you mentioned? if not check the permission of the user under which your application is running has access to the directory where the log file needs to be written.Panayiotis
I'm wokring with root user, so I should not have any problem of permission,Trial
S
12

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.

Swain answered 15/11, 2016 at 12:25 Comment(1)
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.Interesting
K
6

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
Kneepad answered 29/4, 2016 at 10:11 Comment(8)
I tried to use your configuration, but stil not working :/Trial
@Trial I have update my answer. Please check and let me knowKneepad
I tried your solution ,but still not wokring. I tried even log4j but no result :/. i always log to console, but i can't log to fileTrial
@Trial just replace your logback.xml with my provided.Kneepad
I did, I changed only the package name and location file (because Im working with centos system). and still the same resultTrial
I removed logback.xml, and I still see log content in my console, I think that my application uses the logbak configuration of apache storm. but I am not sure, because this is not normalTrial
@Trial console log will always come. you just check that if your console log is exists on debug.log which is told in logback.xml. If it comes, then OK.Kneepad
SkyWalker , debug.log is not generated, i can see only console logTrial
O
1

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

Offertory answered 11/5, 2017 at 16:32 Comment(0)
T
1

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.

Towboat answered 11/10, 2019 at 11:29 Comment(0)
L
1

create a repository and include it as a source folder and put the logback.xml file in it. To include the repository into source folder, click on the repository-> Build Path -> Use as Source Folder

Lukash answered 7/8, 2020 at 8:46 Comment(0)
B
1

look at the stdout when your app starts : logback gives you hints about what's happening. your console appender can be created successfully (letting your think that your logback.xml is correct) while file appender being in error. Look at the ERROR and WARN you see, most of the time you'll see something.

FYI relative path works quite well, there is no reason why you should put absolute path.

Budde answered 19/9, 2023 at 13:38 Comment(1)
Great advice! In my case I had the message: ERROR in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - Compression is not supported in prudent mode. AbortingPigeonhole
B
0

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>
Billionaire answered 12/5, 2019 at 20:2 Comment(0)
W
0

I was missing the dependency ch.qos.logback logback-classic

Wesley answered 14/1, 2021 at 12:55 Comment(0)
H
0

In my case the file was not created for a new appender because the value/pattern of

<fileNamePattern>

under

<rollingPolicy>

tag was not unique. Then the log file for the new appender wasn't created.

Hillyer answered 15/3, 2023 at 14:47 Comment(0)
F
0

Don't forget to add this property to the application.properties file.

logging.config=/opt/mychat/conf/logback.xml

My example configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS" value="/log/mychat" />
<appender name="Console"
          class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1}): %msg%n%throwable
        </Pattern>
    </layout>
</appender>

<appender name="RollingFile"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOGS}/mychat.log</file>
    <encoder
            class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>%d %p %C{1} [%t] %m%n</Pattern>
    </encoder>

    <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOGS}/archived/mychat-%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

<root level="info">
    <appender-ref ref="RollingFile" />
    <appender-ref ref="Console" />
</root>

<logger name="com.fsgc" level="trace" additivity="false">
    <appender-ref ref="RollingFile" />
    <appender-ref ref="Console" />
</logger>
Favien answered 23/4, 2023 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.