6

I'm using slf4j over log4j.

     <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>

The log4j.properties is in WEB-INF folder and has the following content:

log4j.rootLogger=DEBUG, stdout, file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\logs\\log4j.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

On another enviroment with same version of Tomcat the same respository works just fine, logging to the file...

Misca
  • 459
  • 2
  • 11
  • 31
  • see if this question is useful for you : http://stackoverflow.com/questions/14544991/how-to-configure-slf4j-simple – Ravindra babu Aug 18 '15 at 07:19
  • Can you post your expanded project structure ? – Gyan Aug 19 '15 at 06:29
  • 1
    Have you tried adding -Dlog4j.debug to your JVM arguments to get log4j to print its startup information? Are your log statements showing up in System.out? Do they follow the format specified in your config file? If you change the format in the config file, do the statements in System.out change also? If they don't, log4j isn't finding your config file. – T.D. Smith Aug 22 '15 at 02:02
  • Have you tried out this simple helloworld http://stackoverflow.com/questions/4311026/how-to-get-slf4j-hello-world-working-with-log4j – Dockers Aug 24 '15 at 12:43

14 Answers14

4

You said that this same configuration file was working fine on a different environment. Can you give a little detail on the differences between the environments. e.g. are they both on Windows? The fact that is was working fine on another environment suggests that your configuration is fine, but there is some issue with your environment. Here are a few things to check:

  • Does the tomcat process has permissions to write to that file - i.e. are administrator rights required to write to D:\logs\log4j.log?

  • Make sure that there aren't any temporary files lying around tomcat's directories which may be preventing your changes from taking effect. To be sure, stop tomcat, delete the expanded war from %CATALINA_HOME%\webapps, delete the contents of %CATALINA_HOME%\temp and %CATALINA_HOME%\work

  • Check that you don't have any other log4j config files on your classpath, as these could be overriding your log4j file and preventing it from taking effect. To double check this, you could try temporarily removing your log4j.properties file to see if you get messages saying that the logging system is not initialised properly.

olambert
  • 1,075
  • 2
  • 7
  • 10
3

Perhaps your log4j.properties isn't being read at all. If it's not found, then log4j will use a default configuration and you might not even know about it. Try to pass this line

-Dlog4j.configuration=file:///D:/yourPathToFile/log4j.properties

as a VM Argument in your run configuration and see if it helps.

Arqan
  • 376
  • 1
  • 3
  • 14
  • +1 This fixed my problem and seems to be the best answer to the question (perhaps not the "cleanest" solution, but it definitely works). – Jared Mar 03 '20 at 06:06
1

For a Maven Based Project keep your log4j.properties in src/main/resources

karim mohsen
  • 2,164
  • 1
  • 15
  • 19
1

I had the same problem, putting the log4j.log in source work. I thought it was getting compiled in the EAR file, but was not. Assuming it was using default.

Nathan
  • 11
  • 1
0

Instead of:

log4j.appender.mainAppender.File=mainloggs.log

log4j.appender.mainAppender.Append=true

use:

log4j.appender.file.File=mainloggs.log

log4j.appender.file.Append=true
Daniel Boncioaga
  • 336
  • 1
  • 13
0

The issue might be coming when you won't add the log4j.properties file in your classpath. To fix the same follow the steps below:-

  1. Create a folder resources in project and put your log4j.properties in it.
  2. Go to project properties --> Java Build Path -->Click on Add Folder -->Check the resources folder -->Click ok and its done. It should solve your problem.
Goyal Vicky
  • 1,249
  • 16
  • 16
0

Your log4j.properties file should be in WEB-INF/classes, not WEB-INF. WEB-INF is not in the web application classpath, but WEB-INF/classes is.

heenenee
  • 19,914
  • 1
  • 60
  • 86
0

Try with

 log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
 log4j.appender.file.Append=true

If it not working. Please put your web.xml

Tupac
  • 647
  • 12
  • 37
0

I had the same problem. It used to work a while ago but not working now. I guess this is not a problem with log4j2 or slf4j(as was in my case) but instead with the relative path to the config file.

If you have updated java or update IDE or changed some system variable or relative path of the folders then your problem is similar to mine.

Possible fixes/workarounds -

  • Try including the log4j2 configuration file in the artifact while building it.
  • Alternatively you can get config from propertied file and hardcode the logging in code itself - This was what worked for me.
Tushar
  • 441
  • 4
  • 13
0

Try using the scheme when mentioning file paths on windows. Does not need to mention it this way on other platforms but on windows you need to. I have observed issues regarding file paths without scheme.

log4j.appender.file.File=file://d:/logs/log4j.log
Drona
  • 6,886
  • 1
  • 29
  • 35
0

So I had a similar issue with the .log file not being written to when writing a web app on tomcat. In the end I changed the file path to be relative with something like .\log.log. Then I noticed that when I ran the program using tomcat 7 I found the log file in the \bin directory instead of the \log directory inside my tomcat installation.

0
log4j.appender.file.File=${user.home}\\Logs\\mylogfile.log
biology.info
  • 3,500
  • 2
  • 28
  • 39
0

I had the same issue. Turns out, I have another web application inside Tomcat that contain another log4j.properties file. After removing this application, things are working as expected

duvo
  • 1,634
  • 2
  • 18
  • 30
0

I had this problem when migrating log4j1 to log4j2 through the Log4j 1.x bridge (log4j-1.2-api)

My logj1 configuration was correct and the log4j2 and bridge JARs were correctly placed but I forgot this step. So adding this JVM parameter worked:

-Dlog4j1.compatibility=true
golimar
  • 2,419
  • 1
  • 22
  • 33