logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Burton Strauss" <Bur...@SmallNetSolutions.com>
Subject RE: Silly ? - How do I figure out the name of the file the log is being written to?
Date Thu, 01 Dec 2005 21:03:41 GMT
That's easy ... console output is from System.out.println().  How else are
you going to test the logger if you can't trust it... :-)
 
However, I moved the log4j.properties files around and that may have been it
- an old one hiding in the directory tree.
 
So this code gives the expected results:
 
    Appender t = LogManager.getRootLogger().getAppender( "test");
    if(null == t) {
      System.out.println("LogManager.getRootLogger().getAppender(\"test\")
is null");
    } else if(t instanceof FileAppender) {
      FileAppender fileAppender = (FileAppender)t;
      System.out.println("LogManager.getRootLogger().getAppender(\"test\")
is FileAppender " + fileAppender.getFile());
    } else {
      System.out.println("LogManager.getRootLogger().getAppender(\"test\")
is something else");
    }
 
LogManager.getRootLogger().getAppender("test") is FileAppender
somethingelse.log
 
 
But the other path still fails.  I expanded the test program to this:
 
  public static void main(String[] args) {
    Logger logger = null;
    logger = logger.getLogger("test");
    logger.info("Beginning logger test");
    int countOfLoggers = 0;
    Enumeration loggers =
LogManager.getLoggerRepository().getCurrentLoggers();
    if(loggers != null){
      while(loggers.hasMoreElements()){
        Logger l = (Logger)loggers.nextElement();
        System.out.println("Logger #" + (++countOfLoggers) + " is " +
l.getName());
        int countOfAppenders = 0;
        Enumeration allAppenders = l.getAllAppenders();
        if(null == allAppenders) {
          System.out.println("\tgetAllAppenders() returned null");
          continue;
        }
        if(allAppenders instanceof org.apache.log4j.helpers.NullEnumeration)
{
          System.out.println("\tgetAllAppenders() returned
org.apache.log4j.helpers.NullEnumeration");
          continue;
        }
        
        while(allAppenders.hasMoreElements()){
          Appender t = (Appender)allAppenders.nextElement();
          
          if(null == t) {
            System.out.println("\tAppender is null");
          } else {
            countOfAppenders++;
            if(t instanceof FileAppender) {
              FileAppender fileAppender = (FileAppender)t;
              System.out.println("\tAppender " + countOfAppenders + " " +
t.getName() + " is FileAppender " + fileAppender.getFile());
            } else {
              System.out.println("\tAppender " + countOfAppenders + " " +
t.getName() + " is something else");
            }
          }
        }
      }
    }
  }
}

 
And it gives this:
 
Logger #1 is test
        getAllAppenders() returned org.apache.log4j.helpers.NullEnumeration
 
 
-----Burton

  _____  

From: Paul Smith [mailto:psmith@aconex.com] 
Sent: Thursday, December 01, 2005 1:50 PM
To: Log4J Users List
Subject: Re: Silly ? - How do I figure out the name of the file the log is
being written to?


if this is your log4j.properties: 

On 02/12/2005, at 6:40 AM, Burton Strauss wrote:


log4j.rootLogger=debug,test




log4j.appender.test=org.apache.log4j.DailyRollingFileAppender

log4j.appender.test.Append=false

log4j.appender.test.File=testbed.log

log4j.appender.test.layout=org.apache.log4j.SimpleLayout


And you're getting output on the consoled, then I would suggest that your
test is not actually configuring with the above property file, because there
is no ConsoleAppender defined, I'm not sure how you're getting your test
output! :)

Add:
log4j.debug=true

In your property file and make sure you see log4j initialization output on
the console, that will show you it setting up the file appender.

Paul


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message