Misca
Misca

Reputation: 469

Log4j doesn't write logs to file

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

Upvotes: 7

Views: 22275

Answers (14)

golimar
golimar

Reputation: 2548

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

Upvotes: 0

duvo
duvo

Reputation: 1734

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

Upvotes: 0

Nathan
Nathan

Reputation: 11

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.

Upvotes: 1

biology.info
biology.info

Reputation: 3569

log4j.appender.file.File=${user.home}\\Logs\\mylogfile.log

Upvotes: 0

TheVoicedElk
TheVoicedElk

Reputation: 31

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.

Upvotes: 0

Drona
Drona

Reputation: 7234

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

Upvotes: 0

Tushar
Tushar

Reputation: 440

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.

Upvotes: 0

Arqan
Arqan

Reputation: 386

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.

Upvotes: 3

olambert
olambert

Reputation: 1115

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.

Upvotes: 4

Goyal Vicky
Goyal Vicky

Reputation: 1299

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.

Upvotes: 0

Tupac
Tupac

Reputation: 655

Try with

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

If it not working. Please put your web.xml

Upvotes: 0

karim mohsen
karim mohsen

Reputation: 2254

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

Upvotes: 1

heenenee
heenenee

Reputation: 20125

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.

Upvotes: 0

Daniel Boncioaga
Daniel Boncioaga

Reputation: 348

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

Upvotes: 0

Related Questions