This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to separate Console and / or File appenders in Log4J?

 
Unnsse Khan
Ranch Hand
Posts: 511
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

I am using an init servlet to load my log4j properties file during app start up...



The log4j.properties file:



Question(s):

(1) Let's say I want to use a file appender for logging different parts of the app's behavior, do I need to create a different properties file?

(2) If not, how would I integrate that with the properties file above?

(3) Is there a way I set the DEBUG warning to just log to the Console and the LOG warning to just log to specific files (which I specify)?

(4) What if I wanted two separate log files generated for the file appender?

(e.g. one being for cars.log and another one being planes.log)

(5) Do I have to create a different init servlet and different properties file?

Happy programming,
[ June 01, 2007: Message edited by: Unnsse Khan ]
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(1) Let's say I want to use a file appender for logging different parts of the app's behavior, do I need to create a different properties file?


No. You can add the configurations to your existing log4j.properties file.

(2) If not, how would I integrate that with the properties file above?


Have a look at the javadoc of doConfigure method of PropertyConfigurator at doConfigure javadoc which has some examples.

(4) What if I wanted two separate log files generated for the file appender?


A file appender corresponds to one file. So if you want more different log files, you will have to create that many file appenders

(5) Do I have to create a different init servlet and different properties file?


No. A single init servlet and properties file is enough.

A good place to get more details about log4j configurations is the Log4j Manual
 
Unnsse Khan
Ranch Hand
Posts: 511
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jaikiran,

Thank you so much for the response!

This is what I did (I didn't change the Log4JInitServlet):



Question(s):

(1) Where do the files plane.log and car.log become created?

(2) How do I differentiate the log output for each file?

(e.g. How do I tell the Logger to send "this" message to car.log and "that" message to plane.log)?

(3) I tried doing this:



But Eclipse displayed this error:

The method log(String, Priority, Object, Throwable) in the type Category is not applicable for the arguments (String)

Many, many thanks!
[ June 01, 2007: Message edited by: Unnsse Khan ]
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

log4j.appender.add.File=car.log
log4j.appender.edit.File=plane.log
Where do the files plane.log and car.log become created?


The file path that you given in your log4j.properties are relative paths. I prefer giving absolute path like:



Also,

log4j.logger.com.myapp=LOG, car
log4j.logger.com.myapp=LOG, plane


This should be


You will be mentioning the appender names which correspond to the log files.

How do I differentiate the log output for each file?



Assuming you want the logs from the classes under com.myapp.core to go into car.log then you will have to change your log4j.properties file to include:



and in your code, use:


And then if you want the classes under com.myapp.ui to log into the plane.log then you will have to add this to your log4j.properties:



and in your code:


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic