1

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

talex
  • 17,973
  • 3
  • 29
  • 66
chetra tep
  • 189
  • 5
  • 20

2 Answers2

2

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.

Community
  • 1
  • 1
Stefan
  • 12,108
  • 5
  • 47
  • 66
  • I follow you but there still not file in folder logs – chetra tep Jun 23 '15 at 08:09
  • The file will be placed in the logs directory, in your project, on the server, where you deployed your app. I.e. if you are using Tomcat, have a look in the webapps directory. If you want the file to appear in your Eclipse project folder, you will have to set an absolute path. – Stefan Jun 23 '15 at 08:21
  • can you tell me how to put absolute path?? – chetra tep Jun 23 '15 at 08:23
  • Sure just adjust this line: log4j.appender.info.File=C:/[PATH_TO_PROJECT_FOLDER]/logs/mylog.log – Stefan Jun 23 '15 at 08:23
  • are there anyway beside put the absolute path? – chetra tep Jun 23 '15 at 09:31
0

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
Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • no you are confusing my question. I want to know what path should i put in properties file in order to store log file that record all log folder logs that I red cirecle in the picture – chetra tep Jun 23 '15 at 07:36
  • Oh i am sorry , do you want to put your log file inside the logs folder , if i understand it correctly ? – Santhosh Jun 23 '15 at 07:37
  • I put this but the log was store in eclipse director not in logs folders in my project – chetra tep Jun 23 '15 at 08:12