This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes How to separate Console and / or File appenders in Log4J? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "How to separate Console and / or File appenders in Log4J?" Watch "How to separate Console and / or File appenders in Log4J?" New topic
Author

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

Unnsse Khan
Ranch Hand

Joined: Nov 12, 2001
Posts: 511
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

Joined: Jul 20, 2005
Posts: 10141
    
165

(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


[My Blog] [JavaRanch Journal]
Unnsse Khan
Ranch Hand

Joined: Nov 12, 2001
Posts: 511
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

Joined: Jul 20, 2005
Posts: 10141
    
165


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:


 
GeeCON Prague 2014
 
subject: How to separate Console and / or File appenders in Log4J?