What do I need to change to have the logs get written on the log file using Logback?
dependencies {
implementation 'ch.qos.logback:logback-classic:1.2.3',
'ch.qos.logback:logback-core:1.2.3'
api 'org.slf4j:slf4j-api:1.7.30'
}
<configuration debug="true">
<appender name="FILE" class ="ch.qos.logback.core.FileAppender">
<file>logs\logfile.log</file>
<append>true</append>
<immediateFlush>false</immediateFlush>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<Target>System.out</Target>
<encoder>
<pattern> .....</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE"/>
<appender-ref ref="STDOUT"/>
</root>
</configuration>
In Java Class:
private static final Logger log = LoggerFactory.getLogger(MyClass.class);
public static void main(String[] args){
log.debug("hello");
}
The file logfile.log
gets created under logs folder but it's empty and Console does show the log.
Probably you missed the logback-core dependency.
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.10</version>
</dependency>
or
dependencies {
...
implementation 'ch.qos.logback:logback-core:1.2.10'
...
}
In addition to the other two you already have.
Other thing to do is to update to the latest versions of each dependency and see if the problem still occurs.
Reference: https://sematext.com/blog/logback-tutorial/ or https://www.baeldung.com/logback