Cannot find log file in my project logs folder in spring mvc project

5.2k views Asked by At

I use log4j in my spring mvc project and I create log file uing log4j.properties in the following.

# Root logger option
log4j.rootLogger= INFO, info, file, stdout, error

# Direct log messages to a log file [INFO]
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.File=.logs\\mylog.log
log4j.appender.info.Threshold=INFO
log4j.appender.info.MaxFileSize=1Kb
log4j.appender.info.MaxBackupIndex=10
log4j.appender.info.DatePattern='.'yyyy-MM-dd
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

I want it to be stored in myapp/logs folder in my web application . what part i should for the file.

here is the directory enter image description here

2

There are 2 answers

5
Stefan On BEST ANSWER

Right now it will be saved in the current working directory of your IDE (Eclipse or STS, I guess). Its not a good idea to put the file in the project structure, because that structure wont exist, once you deployed the app anywhere.

In Spring you can define a property in youe web.xml, to make the location variable:

<context-param>
  <param-name>webAppRootKey</param-name>
  <param-value>mywebapp.root</param-value>
</context-param>

Then add a variable to your config:

log4j.appender.info.File=${mywebapp.root}/logs/mylog.log

If you have to store it in the application, you can also define that programmatically, by makeing use of "ServletContext.getRealPath(".") to find out where your app is deployed, and then set the path to the file.

3
Santhosh On

In the normal scenario web-app checks for your log file at the root of your class path. To load a file from custom location , have a look at below posts

Change location of log4j.properties

How to use custom file instead of log4j.properties

EDIT :

From op's comment to generate your log file in the projects path , where you have placed your logs directory . you can simply specify it as ,

log4j.appender.file.File= logs/mylog.log