This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Other Open Source Projects and the fly likes Logging into multiple files. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Logging into multiple files." Watch "Logging into multiple files." New topic
Author

Logging into multiple files.

Saurabh Agrawal
Ranch Hand

Joined: Oct 07, 2003
Posts: 244
Hello everyone,
I am using commons logging for my application that uses log4j as such.

My requirement:

I want to have seperate log files for different application usage.For Example, all SQL logs should be logged into a file called SQL_LOG.txt and similar is the case with all logs for exceptions should be logged into seperate file.

This requirement needs 5 seperate log files to be created.So can you suggest me what changes need to be done as far as configuration in LOG4J is concerned.

A sample log4j.properties file would be appreciated where anyone can highlight the change to be done.

Thanks in advance,
Saurabh


Success is not doing extraordinary things but doing ordinary things extraordinarily well.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
There's no problem in declaring multiple loggers in one file. You can start out by duplicating your single logger 5 times, giving them different names and pointing them at different files.


Ping & DNS - my free Android networking tools app
Saurabh Agrawal
Ranch Hand

Joined: Oct 07, 2003
Posts: 244
Originally posted by Ulf Dittmer:
There's no problem in declaring multiple loggers in one file. You can start out by duplicating your single logger 5 times, giving them different names and pointing them at different files.


I agree we can duplicate the logger multiple times and specify different file names but the question is in my JAVA code when i declare a logger like:
private static final Logger logger = Logger.getLogger(Test.class);

So if i use the instance of the logger to log , then it would dump to which log file, how would it know which log file to dump.

So i am still there itself.Please think it from development perspective in JAVA.

Using logger.debug("::: Test Debug :::"); would dump into which log file as i have define 5 loggers in my log4j.xml file
Mahesh Trikannad
Ranch Hand

Joined: Dec 14, 2004
Posts: 68
>private static final Logger logger = Logger.getLogger(Test.class);

>Using logger.debug("::: Test Debug :::");

If u create a category of your class name ( full class with package com......Test ) and reference a appender from there, then it will go to that appender.

By default it will go to the root category.

here is how it would look in xml config

<appender name="FILE1" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="./myfile1.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p %c{2} - %m%n"/>
</layout>
</appender>

<category name="com......Test">
<priority value="info"/>
<appender-ref ref="FILE1"/>
</category>


Mahesh Trikannad
Saurabh Agrawal
Ranch Hand

Joined: Oct 07, 2003
Posts: 244
Originally posted by Mahesh Trikannad:
>private static final Logger logger = Logger.getLogger(Test.class);

>Using logger.debug("::: Test Debug :::");

If u create a category of your class name ( full class with package com......Test ) and reference a appender from there, then it will go to that appender.

By default it will go to the root category.

here is how it would look in xml config

<appender name="FILE1" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="./myfile1.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p %c{2} - %m%n"/>
</layout>
</appender>

<category name="com......Test">
<priority value="info"/>
<appender-ref ref="FILE1"/>
</category>

Thanks Mahesh, but you dint get me .. i said i want to log to differnt files .. and i have hell lot of classes in my code, so i cant explicitly point to any class as same loging strategy would be used in multiple classes.

So in that case , how can i log to multiple files based on my exceptions.

For eg, SQL exceptions in sql.log, database exceptions in Db.log

Can you help me witha sample snippet specifying different file names

Saurabh
Mahesh Trikannad
Ranch Hand

Joined: Dec 14, 2004
Posts: 68
There are 2 ways u can do it.

I have successfully implemented the first method.
The second one Iam unsure about, since it did not work for me.

(1) Define your own categories

<category name="SQLLOG">
<priority value="info"/>
<appender-ref ref="SQLAPPENDER"/>

</category>

<category name="SOMELOG">
<priority value="info"/>
<appender-ref ref="OTHERAPPENDER"/>

</category>

and in your code pass the category name. So u would pass "SQLAPPENDER" to getLogger, in places u want to log out there

(2)

I believe u can provide a package name for category, and in the code pass the whole classname to getLogger.

so u have

<category name="com.mysql.*">
<priority value="info"/>
<appender-ref ref="SQLAPPENDER"/>

</category>

For some reason it did not work for me

Regards

Mahesh
Siyaram Singh
Greenhorn

Joined: Jan 21, 2004
Posts: 28
Hi Shaurav,
Have you got the solution ?
If you have please share with us. I am also looking same sort things to generate the diffrent log files for diffrent applications, using same code base.

IS IT POSSIBLE TO DO IT AT CONFIG FILE LEVEL ONLY, INSTEAD OF DOING (CHANGING) ANYTHING IN CODE ???


Regards,
SS
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Logging into multiple files.