0

I am using log4j for webapplication. I had tried a java example of log4j that makes different log files according to package. But when I applied it to webApplication the file is generating but it is blank. No output is displaying in file. Below is the code.

log4j.rootLogger=DEBUG, CA

log4j.appender.pkg1Appender=org.apache.log4j.FileAppender
log4j.appender.pkg1Appender.File=E://pkg1.log

log4j.appender.pkg2Appender=org.apache.log4j.FileAppender
log4j.appender.pkg2Appender.File=E://pkg2.log

# ConsoleAppender
log4j.appender.CA=org.apache.log4j.ConsoleAppender

log4j.logger.pkg1=,pkg1Appender
log4j.logger.pkg2=,pkg2Appender
charmi
  • 197
  • 3
  • 17

1 Answers1

0

Try adding the package specific appenders to the root logger line.

log4j.rootLogger=DEBUG, CA, pkg1Appender, pkg2Appender

See the answer to this question here: log4j: package-specific logging

Community
  • 1
  • 1
Kiran K
  • 703
  • 4
  • 11
  • I also tried it. It will print all logs into it not package specific. – charmi Apr 04 '16 at 12:29
  • My project has a central method name printout() that I had used all over my project. Now I want to apply log4j in application. I made a static variable of logger and using it in printout() method. So, I never get from which package logger is printing. I think it is reason that my appender file is blank? Do you have any alternative so that I dnt have to change all source file to implement logger. – charmi Apr 04 '16 at 12:34
  • You can try using aop,if that is feasible for you – S.B Apr 04 '16 at 12:43
  • thanks but i have just went through it once in video tutorial and know that it will be used in spring. Should I use it without spring? and project uses jdk 5. – charmi Apr 04 '16 at 12:55
  • This is a hack - Make your printout method to accept a logger defined in individual classes to print the message instead of using a central logger. – Kiran K Apr 04 '16 at 13:55
  • Kiran there are hundreds of file in existing project I can't change all files for logging. I am thinking if my url change jsp package dynamically according to that should I get logs in different file? using this central logger concept is thi possible? I read in one thread of stackoverflow that once logger is initialized it cannot change file dynamically. thats why I am confused a bit. – charmi Apr 05 '16 at 07:24