• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Logging into multiple files.

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>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>
 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic