Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I get the log4j logfile-name in an application?

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
Hi all,

I am using log4j with a DailyRollingFileAppender. Everything works fine so far but there is one problem left....

I want to be able to let my application know how its logfile is named an where it can be found.

Therefor I would have to browse through the appenders and if I find an appender of class org.apache.log4j.DailyRollingFileAppender I want to read the parameter "file"... but I have no clue how to achieve this.

All I have is a static instance of Logger. But if I try to access the appenders with logger.getAllApenders the enumeration that is returned seems to be empty... needless to say that I don't know what to do with the enumeration even if it was not empty.


Any ideas about this?

Cheers
frag

real_firestorm@gmx.de
 
Well, the Logger class subclasses Category which carries a getAppender and a getAllAppenders method. Using one of these, you should be able to get hold of the Appender for any particular logging context.

Once you've got the Appender, you would check it to see if it is a FileAppender (your DailyRollingFileAppender is a subclass so qualifies) using instanceof. If so, cast the Appender to a FileAppender and use the getFile method to get the file you're appending to.

I've not tried this, I just trawled through the log4j API for you. I hope it works [smile].

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
... and having just re-read the lower part of you're post I've realised you were part of the way there [bigsmile].

Now, if you've called the getAppenders() method, and the enumerator is empty, then I would assume that your DailyRollingFileAppender, as it was set up in your log4j.properties (assuming you're doing configuration like that), isn't set to apply to the class you're enumerating them for. Check your setup!

Once you have sorted this problem out, you'd do something like this to get all files which would be appended-to by the Logger in the current logging context:-
Code:
[COLOR=green]//... at the top of your class producing logging output[/color]
private static final Logger LOGGER = Logger.getLogger([i]someclass.class[/i]);

[COLOR=green]//... somewhere in that class where you want to know the files appended to...[/color]
Enumeration enum = LOGGER.getAllAppenders();
while ( enum.hasMoreElements() ){
  Appender app = (Appender)enum.nextElement();
  if ( app instanceof FileAppender ){
    [COLOR=green]//I'll dump the file names to the console for now
    //... you do what you like with them [/color]
    System.out.println("Appended File=" + ((FileAppender)app).getFile();
  }
}

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Hey this works! Thank you very much... my mistake was that I used:

Code:
satic Logger logger = Logger.getLogger(myClass.class);

But apparently I have to use:

Code:
satic Logger logger = Logger.getRootLogger();

In my log4j.xml I have:

Code:
<log4j:configuration xmlns:log4j="[URL unfurl="true"]http://jakarta.apache.org/log4j/"[/URL] threshold="debug">

  <appender name="myConsoleAppender" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.SimpleLayout"/>
  </appender>

  <appender name="myFileAppender" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <param name="file" value="C:\\ecbloader.log" />
    <param name="append" value="false" />
    <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="\%d [%t] %-5p %c - %m%n" />
    </layout>
  </appender>
 
 
  <root>
    <priority value ="info" />
    <appender-ref ref="tisConsoleAppender"/>
    <appender-ref ref="tisFileAppender"/>
  </root>

</log4j:configuration>

What would I have to change in order to be able to use "getLogger(myClass.class)" instead of the root logger?


Thanks
frag

real_firestorm@gmx.de
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top