Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

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

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, can you please edit your post with UseCodeTags :-)
 
sudheshna Iyer
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!