Meaningless Drivel is fun!*
The moose likes Other Open Source Projects and the fly likes log to differnt files 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 "log to differnt files" Watch "log to differnt files" New topic
Author

log to differnt files

Mary Cole
Ranch Hand

Joined: Dec 02, 2000
Posts: 362
Hi,

I have to log the logs from 2 differnt packages to differnt log files. How do I set the config in log4j.properties.

package com.foo.X has to go to log1.log and package com.foo except com.foo.X , all other package like com.foo.K com.foo.T has to go to log2.log

Any ideas please?
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161
I really don't know how to do that with the log4j.properties.. Now, log4j does support using an log4j.xml file to set it up. usually its the same kind of convenience, of just dropping the log4j.xml into the top of the classpath instead of (remove and replace) the log4j.properties. Or if you do any other kind of elaborate set up logging by system property to log4j config file path.

The log4j.xml is more flexible, and powerful for setting up more elaborate kinds of things like different log files by package as you are looking to do.

Have a look at http://wiki.apache.org/logging-log4j/Log4jXmlFormat

they have an example towards the end where using the log4j.xml to do package specific logging.

Error: Keyboard not attached. Press F1 to continue.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Where are you stuck? Are you using the additivity flag to keep com.foo.X logging out of log2.log?

After you define logger/appender for com.foo.X set additivity false on com.foo.X :

log4j.additivity.com.foo.X = false

You can search on additivity in this forum for examples.
Mary Cole
Ranch Hand

Joined: Dec 02, 2000
Posts: 362
Here is my log4j file




As you can see form the file, I want com.foo.X to go to log2.log and all the remaining package logs to go to log1.log.

Thanks in advance

Tanzy Akhtar
Ranch Hand

Joined: Jul 19, 2009
Posts: 110
Hi Mary,

One idea i have and applied in my project.

Do as follows--

1. Create a class with logger and create methods which call nothing but DEBUG()/INFO()/ ... etc.
2. Configure the above created class in log4j.propertis/log4j.xml.
3. Now call the above created methods to log messages other than package com.foo.X.

For full details you may follow this.
Don't get confused by the subject line used in the given url, what i described above, is available there.

Thanks,
Tanzy.


Roll with punchers, there is always tomorrow.
Techie Blog -- http://jtanzy.blogspot.com/
Mary Cole
Ranch Hand

Joined: Dec 02, 2000
Posts: 362
Thanks for your reply tanzy akhtar...I am not allowed to write any java class for this.. Below config worked fine for me

Tanzy Akhtar
Ranch Hand

Joined: Jul 19, 2009
Posts: 110
That's really great that the problem has been resolved. Congrats.
And thanks for sharing the solution.
 
GeeCON Prague 2014
 
subject: log to differnt files