Donna,

 

I think you have to call activateOptions() method on your Appenders. Also what error message / debug trace are you getting?

Hope this helps.

 

-Kamal.

 


From: Donna Johnson [mailto:[EMAIL PROTECTED]
Sent: Tuesday, September 19, 2006 4:48 PM
To: log4j-user@logging.apache.org
Subject: Add appender programmatically after logger configured initially with PropertyConfigurator

 

I am trying to add a couple of appenders to my logger in the code based on flags in the application's properties file.  I configure the logger with an appender in the logger.properties file initially and I want to add appenders to that programmatically.  I add a console appender as well as a file appender (based on the flags).  Nothing is being logged to either the console or the file, even though the file is being created.  Any help is very much appreciated!

 

Here is the logger.properties file:

 

# Logger for Errors/Debugging (Syslog)
log4j.logger.error=DEBUG, A1
log4j.appender.A1=org.apache.log4j.net.SyslogAppender
log4j.appender.A1.syslogHost=localhost
log4j.appender.A1.facility=LOCAL5
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %m%n

# Logger for stats
log4j.logger.stats=INFO,A2
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.File=./cust/properties/stats
log4j.appender.A2.MaxFileSize=10MB
log4j.appender.A2.MaxBackupIndex=1
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d %m%n

 

Here is the application properties file:

 

# Log to console
ngl.console=true


# Log to local5 (Unix only)
ngl.syslog=localhost:local5


# Log to a disk file
ngl.logfile=./cust/properties/test.txt


# Logging level (can be DEBUG, INFO, WARN, ERROR, or CRITICAL|FATAL)
ngl.level=DEBUG


# Host name that will print out on each log line
ngl.hostname=localhost


# If true, writes abbreviated log header with only level and message
ngl.brief=false

 

ngl.config=./cust/properties/logger_config.properties

 

ngl.logger.name=error

 

 

Here is the actual code to initialize the logger:

 

  try {

   PatternLayout layout;
   String loggerPropFile = properties.getProperty("ngl.config",null);
   String loggerName = properties.getProperty("ngl.logger.name", null);
   if (loggerPropFile != null)
   {
    PropertyConfigurator.configure(loggerPropFile);
   }

   logger = Logger.getLogger(loggerName);
   
   Enumeration en = logger.getAllAppenders();
   int nbrOfAppenders = 0;
   while (en.hasMoreElements()) {
    nbrOfAppenders++;
    Object ap = en.nextElement();
   }
   if (nbrOfAppenders > 1) {
          return;
   }
   try {
    if ("true".equals(properties
      .getProperty("ngl.console", "false"))) {
     layout = new PatternLayout("%d [%t] %-5p %m%n");
     logger.addAppender(new ConsoleAppender(layout));
    }
   } catch (RuntimeException e) {
    handleException(e);
   }

 

   try {
    String logfile = properties.getProperty("ngl.logfile", null);
     if (logfile != null) {
     layout = new PatternLayout("%d [%t] %-5p %m%n");
     logger.addAppender(new FileAppender(layout, logfile));
    }
   } catch (RuntimeException e) {
    handleException(e);
   }
  } catch (Exception ex) {
   handleException(ex);
  }

 

 

Donna Johnson
Sr. Programmer Analyst

FedEx
10 Fedex Pkwy
2nd Floor Horizontal
Collierville, TN 38017

[EMAIL PROTECTED]

tel:

901-263-9914

 

Add me to your address book...

Want a signature like this?

 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to