Search code examples
javalogginglog4jjcabi

jcabi is not logging in a file


I am trying to log using jcabi-log as described in link at http://www.yegor256.com/2014/05/23/avoid-java-static-logger.html.

I have a method runThisAdvise() which is getting called from main method. The text in Logger.info "I am here" is not printing in the log1.out file. Can someone please help me to resolve this issue?

import com.jcabi.log.Logger;

public class StaticLogger {
    private int c;
    private int a=5;
    private int b=2;

    public int runThisAdvise() {
        c=a+b;
        Logger.info(this,"I am here");
        System.out.println("blah" +c);
        return c;
     }
}

log4j.properties file

log4j.rootLogger = DEBUG, FILE,CONSOLE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=D:\\log1.out
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.Threshold=debug
log4j.appender.FILE.Append=false
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern= %r [%t] %-5p %c %x - %m%n

Solution

  • For anyone who has similar issue check your log4j configuration and properties file. Here are the steps to make sure you have your jcabi-log4j-slf4j configured correctly.

    1) Add following dependencies to maven:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.12</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>com.jcabi</groupId>
        <artifactId>jcabi-log</artifactId>
        <version>0.16</version>
        </dependency>
    

    2) Make sure log4j.properties file is at correct location i.e src/main/resources

    3) After above two steps, the following code should work:

    package test.jcabi.jcabiProject;
    
    import com.jcabi.log.Logger;
    
    public class JcabiLogger {
        public static void main (String args[]) {
            Logger.info(jcabiLogger.class,"This is jcabi test");
        }
    }