0

I need to keep separate log4j log files in a separate folder.Those log files should be separated for each URL.My project is Spring MVC project.

For example,

www.xyz.com/test/url1
www.xyz.com/test/url2
www.xyz.com/test/url3

How can I configure my log4j? Is there a way to keep separate log4j files for method level?

sampathlk
  • 338
  • 2
  • 17
  • Possible duplicate: http://stackoverflow.com/questions/9652032/how-can-i-create-2-separate-log-files-with-one-log4j-config-file – sura2k Apr 29 '16 at 10:55

2 Answers2

0

Its possible. Yo need to create different logger instances in a class say logger, logger1, logger2 and these should point to different files. Although they will use the same base package to cover but you can move logs in different files for different methods

Vineet Kasat
  • 994
  • 7
  • 14
  • Thats great it worked for you. Please accept the answer. – Vineet Kasat Apr 30 '16 at 06:30
  • well, I don't think it is normally how it should be done. If both URLs called a shared library, and in the library logs are written, you cannot separate such logs by the way you suggested. (it depends on what OP really wants to do though) – Adrian Shum May 03 '16 at 08:25
0

Here I am posting sample code for configuring multiple loggers. I have configured my log4j file in this way.

log4j.rootLogger = INFO , stdout

#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n

log4j.appender.pageOneLog=org.apache.log4j.FileAppender
log4j.appender.pageOneLog.File=C:/work/logs/pageOneLog.log
log4j.appender.pageOneLog.layout=org.apache.log4j.PatternLayout
log4j.appender.pageOneLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n

log4j.appender.pageTwoLog=org.apache.log4j.FileAppender
log4j.appender.pageTwoLog.File=C:/work/logs/pageTwoLog.log
log4j.appender.pageTwoLog.layout=org.apache.log4j.PatternLayout
log4j.appender.pageTwoLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n

log4j.category.pageOneLogger=INFO, pageOneLog
log4j.additivity.pageOneLogger=false

log4j.category.pageTwoLogger=INFO, pageTwoLog
log4j.additivity.pageTwoLogger=false

Then use this loggers in the Java code following manner.

Logger log1 = Logger.getLogger("pageOneLogger");
Logger log2 = Logger.getLogger("pageTwoLogger");
sampathlk
  • 338
  • 2
  • 17
  • (same comment as in another answer) well, I don't think it is normally how it should be done. If both URLs called a shared library, and in the library logs are written, you cannot separate such logs by the way you suggested (it depends on what OP really wants to do though) – Adrian Shum May 03 '16 at 08:26