1

I am trying to use log4j to log it in the file here's the code

    protected static Logger logger = Logger.getLogger(Application.class);
    private static final String DIRECTORY = "/Users/me/Desktop";
    private static final String EXTENSION = ".log";

    protected void setupLogger(String fileName) {
        SimpleLayout layout = new SimpleLayout();
        FileAppender appender = new FileAppender(layout, DIRECTORY + "/logs/" + fileName + EXTENSION, false);
        logger.addAppender(appender);
        logger.setLevel((Level) Level.DEBUG);
    }

and here's the pom that i use http://pastebin.com/vXdFtzSU

The stacktrace that I am getting is here

Error:(40, 28) java: incompatible types: org.apache.log4j.FileAppender cannot be converted to org.apache.log4j.Appender

I am trying to follow this answer configure log4j to log to custom file at runtime

Community
  • 1
  • 1
Kevin King
  • 557
  • 1
  • 9
  • 25
  • can you show your import statement – Raghu Jul 26 '16 at 07:32
  • import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.SimpleLayout; import org.apache.log4j.FileAppender; – Kevin King Jul 26 '16 at 07:34
  • Try to find out dependency tree from below command mvn dependency:tree , and post it here. There is a version conflict of log4j jars.i think you need to exclude log4j from other artifact dependency. – Aaditya Raj Jul 26 '16 at 07:38
  • I am also using spring and thymeleaf. here's my full pom http://pastebin.com/vXdFtzSU – Kevin King Jul 26 '16 at 07:41
  • Spring dependency uses log4j internally . Run mvn dependency:tree command and share. – Aaditya Raj Jul 26 '16 at 07:43
  • @AadityaRaj yeah, I just noticed that now. here's my dependency tree http://pastebin.com/6wJQBDHT – Kevin King Jul 26 '16 at 07:48

3 Answers3

2

Hi try changin your maven dependencies add this dependency:

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

and change spring boot dependencies to exclude logging dependencies :

      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
          <exclusions>
            <exclusion>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
          </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
          <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

then add try/catch statement :

    protected void setupLogger(String fileName) {

        try {
            SimpleLayout layout = new SimpleLayout();
            FileAppender appender;
            appender = new FileAppender(layout, DIRECTORY + "/logs/" +        fileName + EXTENSION, false); 
            logger.addAppender(appender);
            logger.setLevel((Level) Level.DEBUG);
        } catch (IOException e) { 
            e.printStackTrace();
        }
    }
  • Now the file is saving on the path that I specified but I am getting stacktrace from setFile http://pastebin.com/FZ6nmTeY – Kevin King Jul 27 '16 at 03:07
  • check if the log file existe in the specified path "/users/me/Desktop/logs/filename.log" or try to use the absolute path ex : "C:/Users/me/logs/filename.log" – ZIANE Mohamed Jul 27 '16 at 07:23
  • i'm using mac and i did try to use the absolute path – Kevin King Jul 27 '16 at 07:53
  • It's only a matter of path, just check if is right, check also the permission, check if the path already existe. can you post the full path of your log file !! – ZIANE Mohamed Jul 27 '16 at 08:17
0

It almost looks like you are using a different version of the libraray at runtime than at compile-time. If the types were truly incompatible that would generate a compiler error. If you are running your program in a special environment like Tomcat etc. check if the same version of log4j is installed there.

kalsowerus
  • 998
  • 8
  • 23
0

Why not use log4j.properties It's a very simple configuration print to file.

log4j.logger.register=INFO,R7
log4j.appender.R7=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R7.DatePattern='.'yyyyMMdd
log4j.appender.R7.File=/appLogs/address/logFile.log
log4j.appender.R7.layout=org.apache.log4j.PatternLayout
log4j.appender.R7.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} | %m%n


private static final Logger logger = Logger.getLogger("register");

logger.info("print to file and console");
learning
  • 21
  • 3
  • I already tried that but it is not reading my properties. here's the properties http://pastebin.com/Y8JKCuTY maybe because of spring – Kevin King Jul 26 '16 at 07:54