aspose file tools*
The moose likes Other Open Source Projects and the fly likes log4j -> fileAppender.getFile() to read unix log dir location Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "log4j -> fileAppender.getFile() to read unix log dir location" Watch "log4j -> fileAppender.getFile() to read unix log dir location" New topic
Author

log4j -> fileAppender.getFile() to read unix log dir location

sudheshna Iyer
Ranch Hand

Joined: Aug 20, 2004
Posts: 71
I have the following "log4j.xml" in the UNIX class path. When I read the fileAppender.getFile from my java program, I am receing Null value.
I want to use "File" param for directory path and contruct file name to create a file in the directory mentioned by param "File".


I tried replacing File param with the below values and all returned Null.

<param name="File" value="\/home\/aaa"/> ==>fileAppender.getFile(): null>
<param name="File" value="/home/aaa"/> ==>fileAppender.getFile(): null>
<param name="File" value="\\/home\\/aaa"/>==>fileAppender.getFile(): null>
<param name="File" value="\/home\/aaa"/> ==> <fileAppender.getFile(): null>
<param name="File" value="'/home/aaa'"/> ===> <fileAppender.getFile(): null>
<param name="File" value="${HOME}"/> ==> <fileAppender.getFile(): null>
<param name="File" value="'file:///home/aaa'"/> ==> <fileAppender.getFile(): null>



<?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3 <!--THIS FILES CONFIGURES LOG4J TO LOG TO CONSOLE -->
4 <log4j:configuration>
5 <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
6 <layout class="org.apache.log4j.PatternLayout">
7 <param name="ConversionPattern"
8 value="%d %-5p [%t] %C %c - %m%n"/>
9 </layout>
10 </appender>
11 <appender name="FileAppender" class="org.apache.log4j.RollingFileAppender">
12 <param name="MaxBackupIndex" value="20"/>
13 <param name="File" value="'file:///home/aaa'"/>
14 <layout class="org.apache.log4j.PatternLayout">
15 <param name="ConversionPattern" value="%d %-5p [%t] %C - %m%n"/>
16 </layout>
17 </appender>
18 <root>
19 <priority value="debug" />
20 <appender-ref ref="ConsoleAppender"/>
21 <appender-ref ref="FileAppender"/>
22 </root>
23 </log4j:configuration>

My sample java program:

private String log(String fileName, Level logLevel,String message) {
Enumeration appenders = logger.getAllAppenders();
RollingFileAppender fileAppender = null;
String strMsg = "";
String logDest = "";

while(appenders.hasMoreElements())
{
Appender currAppender = (Appender) appenders.nextElement();
if(currAppender instanceof FileAppender)
{
fileAppender = (RollingFileAppender) currAppender;
}
}
if(fileAppender != null)
{
try {
logDest = fileAppender.getFile();

System.out.println("logDest : " + logDest); ===> Receiving NULL here
File logFile = new File(fileAppender.getFile() + System.getProperty("file.separator") +
fileName + ".log");
if (!logFile.exists()) {
logFile.createNewFile();
}
fileAppender.setFile(logFile.getPath());

fileAppender.activateOptions();
} catch (IOException ioe) {
logger.error("Log file could not be created. Reason "+ioe.getMessage());
ioe.printStackTrace();
strMsg = "Error";
return strMsg;
}
}
else
{
logger.error("No File Appender found");
strMsg = "Failed";
return strMsg;
}
strMsg = "Success";
logger.log(logLevel, message);
return strMsg;
}


I appreciate your help in this regard.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi there, can you please edit your post with UseCodeTags :-)


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
sudheshna Iyer
Ranch Hand

Joined: Aug 20, 2004
Posts: 71
I have the following "log4j.xml" in the UNIX class path. When I read the fileAppender.getFile from my java program, I am receing Null value.
I want to use "File" param for directory path and contruct file name to create a file in the directory mentioned by param "File".


I tried replacing File param with the below values and all returned Null.

My sample java program:

I appreciate your help in this regard.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: log4j -> fileAppender.getFile() to read unix log dir location
 
Similar Threads
wierd logging issue with my app
Log4j : specific class to log on FileAppender other classes log on Console Appender.
RollingFileAppender not happening
struts 2 and log4j
How to get the timestamp in GMT format in Log4j.xml?