• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can we pass a parameter to log4j..?  RSS feed

 
SirishKumar Nukkala
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai guys..

Comming to the point.I've got a major problem in log4j i.e to send a parameter to log4j so that it could generate random files( Using file appender ).

My problem is : -

I've got a user of id 47.He sends a file "Blah.xls".The file tracking Id for that ("Blah.xls") file is "EFG123".So, inorder to track the file status i need to log a log file using log4j ("I'm thinking like to create a new log file using file appender") like user-id_file-Tracking-ID.log.

Similarly for another user of id 48 I need to create a different log with his user-id and file-Tracking-ID("randomly generated - don't bother").

So,can i pass any parameter to log4j.xml and create logs dynamically instead of using java code.

I'm pasting the java as well as log4j.xml.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy.MM.dd HH:mm:ss.SSS} %m%n "/>
</layout>
</appender>

<appender name="fileAppender1" class="org.apache.log4j.FileAppender">
<param name="File" value="${i}\info.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%r %m%n "/>
</layout>
</appender>

<root>
<priority value ="debug" />
<appender-ref ref="ConsoleAppender"/>
</root>

</log4j:configuration>

Can i pass parameter in ${i} through java.

Can any one please find the solution for my problem.

Your helpness will be appreciated
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question will probably be sent to the log4j forum

Who about programmatically making a new Appender and assigning it to your Logger (addAppender) ? You can use FileAppender(Layout layout, String filename) and pass the filename that you want.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!