0

I'm trying to log something to file in maven project. I've found "howto" here: Where does the slf4j log file get saved?

But it doesnt work (I cant find log file) for some reason, what's my mistake?

Here is my pom.xml:

    <!-- Logger -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.12</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.12</version>
    </dependency>

Here is my log4j.properties :

log4j.rootLogger=DEBUG, STDOUT, file

log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=mylogs.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%nF

Here is my app:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App {

    // Log
    private static final Logger log = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) {
        log.info("App started");

        Worker worker = new Worker();
        worker.doWork();
        log.debug("worker finished job");

        log.debug("Main ends here");

    }
}
Community
  • 1
  • 1
J. Leeroy
  • 450
  • 1
  • 5
  • 17
  • 2
    Which directory is the `log4j.properties` file in? Also, check the classpath. Does it contain both `jcl-over-slf4j` and `commons-logging`? If the answer is yes, then remove `commons-logging` using exclusions if you have to. My experience is that those two libraries don't play well together, and I've had a log file not show up as a result. – user944849 Apr 24 '15 at 14:19
  • `log4j.properties` is at `src\main\resources`, there is `commons-logging` and no `jcl-over-slf4j`. Deleted `commons-logging` for test, still no log file :( – J. Leeroy Apr 24 '15 at 14:30
  • Do you see logs in console? – yozh Apr 24 '15 at 15:46
  • 1
    Your project may contain a dependency that contains log4j.properties/xml file. This could override properties you supply. Had this one time. – yozh Apr 27 '15 at 17:40
  • @yozh I think, that could be the problem, but I'm pretty new to maven, what should I do? – J. Leeroy Apr 28 '15 at 06:26
  • First, try using different name for file with log4j configuration: rename it to e.g. `app-log4j.properties` and specify it at launch: `-Dlog4j.configuration=app-log4j.properties`. If it does work, then there is a conflict indeed. The best would be to remove log4j.properties from dependency but if it's not possible (f.e. 3d-party lib) then consider making outlined above change of log4j configuration file name permanent. – yozh Apr 28 '15 at 08:36
  • @yozh you where right, I've launched my app with `-Dlog4j.debug=true` and app really loaded log4j.xml from 3d party library. I forced my app to load my settings `props.load(new FileInputStream("settings\\log4j.properties"));`, so that solved the problem... So how do i mark your comment as correct answer? – J. Leeroy Apr 30 '15 at 09:17
  • I've edited my answer to incorporate the comment – yozh Apr 30 '15 at 12:33

1 Answers1

2

Your project may contain a dependency that contains log4j.properties/xml file. This could override properties you supply. In order to check for that, first, try using different name for file with log4j configuration: rename it to e.g. app-log4j.properties and specify it at launch: -Dlog4j.configuration=app-log4j.properties. If it does work, then there is a conflict indeed. The best would be to remove log4j.properties from dependency but if it's not possible (f.e. 3d-party lib) then consider making outlined above change of log4j configuration file name permanent.

In general, mylogs.log should be in the directory you launch your program from. For example if it's launched from an IDE (say, IDEA) then log file should be created in project directory. If you do it by yourself from command line from target/classes, log file will be written to that directory respectively.

yozh
  • 1,213
  • 2
  • 10
  • 18