First you must know that i'm a beginner with logback so maybe i am making an error really stupid and not noticing it .Then maybe the problem might have been solved somewhere else but as long as i have searched i found nothing .
But i have a problem when i try to configure my logs file .
I want to have a repository on the root of my project called logs with all my logs ordered by time (and a limit size) . So i saw that logback is using what i want with SizeAndTimeBasedRollingPolicy
.
So after some research on google my config file ( logback.xml ) is :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="ROLLING"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>SG.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<!-- each file should be at most 100MB, keep 60 days worth of history,
but at most 20GB -->
<maxFileSize>100MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
<appender-ref ref="ROLLING" />
</root>
</configuration>
Also my pom.xml looks like that :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testLog</groupId>
<artifactId>testLog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.3.0-alpha4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.3.0-alpha4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-beta1</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
And of course i call the logger :
package testLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
private static Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
// TODO Auto-generated method stub
logger.debug("toto");
logger.info("ela ela");
logger.warn("ce je ne sais quoi ");
logger.error("ce dont du ciel qui la rend belle");
}
}
So now i can tell you my problem : No log file is generated . Do you have any idea what the problem could be ?
Thanks for your time .
I assume it should be at your project's root, to make sure it is generated, instead of:
<file>SG.log</file>
try to give an absolute path, such as
<file>/Users/username/logs/SG.log</file>
Make sure the directory arborescence is created. This way you know where to look for the file, and you can make sure it is actually created.
If it is well generated then do:
<file>logs/SG.log</file>
And it should go to your log directory (create the directory manually to be sure, I don't think logback would create it)
EDIT
Could you change your root logger to be
<root level="DEBUG">
<appender-ref ref="ROLLING" />
</root>
maybe one of the others STDOUT
and FILE
are hijacking the logs
EDIT2
The last think I can think of which are different from my configuration are these:
My logback.xml file starts this way
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration debug="true">
I set the class for the encoder
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%msg%n</Pattern>
</encoder>
Also "ce don du ciel qui la rend belle" :)