logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sanjay Gupta \(sanjaygu\)" <sanja...@cisco.com>
Subject A possible BUG? Logger vs category tag in xml (DOMConfigurator)
Date Mon, 14 Jul 2003 19:21:49 GMT

Hi,

I am trying to subclass Logger(Category) & LoggerFactory and use
DOMConfigurator(log4j.xml). I ran into interesting problem. My stuff
works fine when I configure the logger from program i.e. not using a xml
or properties file.
But when I use xml file, it turns out that <logger> tag does not have a
way of specifying class so My subclassed Logger never gets instanciated.
I digged deeper and figured out that <category> tag has "class"
attribute.
So, now I am confused. Should I subclass Logger or Category. If I should
subclass Logger then how can I make sure that my subclassed Logger gets
instanciated from the log4j.xml and not org.apache.log4j.Logger?

I think that it might be a bug?

Regards,
-Sanjay

-----Original Message-----
From: Sanjay Gupta (sanjaygu) [mailto:sanjaygu@cisco.com] 
Sent: Monday, July 14, 2003 11:27 AM
To: 'Steve Ebersole'; 'Log4J Users List'
Subject: RE: How to use categoryFactory element in the properties file?


I tried ur suggestion. That eliminated "no such properties error.." But
It is still not working. NtLoggerFactory's makeNewLoggerInstance method
is not getting called hence I am getting Class cast Exception. 
Thanks, 
-sanjay

Here is the Log4jTest.java from which I am using the logger ---
---------------------Log4jTest.java-------------------
	   public static void main(String[] args)
	   throws Exception
	   {
		NtLogger c=null;
						  
		if(args.length == 0) {
		  // Note that the appender is added to root but that
the log
		  // request is made to an instance of MyLogger. The
output still
		  // goes to System.out.
		  System.out.println("using default conf");
		  Logger root = NtLogger.getRootLogger();
		  Layout layout = new NtPatternLayout("%p <%a> =>%h=<
[%t] %c (%F:%L:%M) - %m%n");
		  root.addAppender(new ConsoleAppender(layout,
ConsoleAppender.SYSTEM_OUT));
		  NtLoggerFactory factory = new NtLoggerFactory();
          factory.setAppId("LogWriter");
          factory.setHostName("LocalMachine");
		  c = (NtLogger) NtLogger.getLogger("CodeLogWriter",
factory); 
		} else if(args.length == 1) {
		  System.out.println("using " + args[0]);
		  Logger root = NtLogger.getRootLogger();
		  DOMConfigurator.configure(args[0]);
		  c = (NtLogger)NtLogger.getLogger("LogWriter"); 
		} else {
		  System.out.println("Wrong Number of Arguments");
		  System.exit(1);
		}

		try {
//			Logger c = (Logger)
Logger.getLogger("LogWriter");
		  c.info("Hello info ");
		  c.debug("Hello debug");
		} catch(ClassCastException e) {
		  LogLog.error("Did you forget to set the factory in the
config file?", e);
		}
	System.out.println("Done...");
	   }
--------------------------------------

-------------log4j.xml------------------
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
        <appender name="RBERR"
class="org.apache.log4j.RollingFileAppender">
           <param name="File" value="D:\\Logwriter\\test.log" />
           <param name="Threshold" value="DEBUG" />
           <param name="MaxFileSize" value="1024KB" />
           <param name="MaxBackupIndex" value="10" />
           <layout class="com.cisco.nettools.log4j.NtPatternLayout">
              <param name="ConversionPattern" value="%p [%a] =)%h=( [%t]
%c (%F:%L:%M) - %m%n" />
           </layout>
        </appender>
        <logger name="LogWriter">
           <level value="debug"/>

           <appender-ref ref="RBERR" />

        </logger> 
        <categoryFactory
class="com.cisco.nettools.log4j.NtLoggerFactory">
          <param name="appId" value="MYAPPID" />
          <param name="hostName" value="LOCAL_MACHINE" />
        </categoryFactory>
</log4j:configuration>  
-----------------------------------------------
--------------------------NtLogger.java--------------------
public class NtLogger extends Logger {

	protected String appId;
	protected String hostName;

	   private static LoggerFactory factory = new NtLoggerFactory();
	/**
	 * @param name
	 */
	public NtLogger(String loggerName) {
		super(loggerName);
	}
	
	protected  NtLogger(String loggerName, String appId, String
hostName)
	{
	  super(loggerName );

//		  instanceFQN =
"org.apache.log4j.examples.appserver.AppServerCategory";
  
		  this.appId  = appId;
		  this.hostName = hostName;

	   }

	public String getAppId()
	{  
	   return ( this.appId == null ) ? "" : this.appId; 
	}

	public String getHostName()
	{  
	   return ( this.hostName == null ) ? "" : this.hostName; 
	}

	/**
	   This method overrides {@link Logger#getInstance} by supplying
	   its own factory type as a parameter.
	*/
	public static Category getInstance(String name) 
	{
	  return Logger.getLogger(name, factory); 
	}
  
	/**
	   This method overrides {@link Logger#getLogger} by supplying
	   its own factory type as a parameter.
	*/
	public static Logger getLogger(String name) 
	{
	  return Logger.getLogger(name, factory); 
	}
  
	protected void forcedLog(String    fqn, 
							 Priority
priority, 
							 Object    msg, 
							 Throwable t) 

	 {

		LoggingEvent event = new NtLoggingEvent(fqn, this,
priority, msg, t);
		callAppenders( event );

	 }

	public static void main(String[] args) {
	}
}
--------------------------------------------------

-----Original Message-----
From: Steve Ebersole [mailto:steveebersole@austin.rr.com] 
Sent: Sunday, July 13, 2003 6:27 AM
To: Log4J Users List; sanjaygu@cisco.com
Subject: Re: How to use categoryFactory element in the properties file?


appId and hostName need to be properties on your class, not arguments to
its constructor.

public class NtLoggerFactory
{
    protected String appId;
    protected String hostName;

    public NtLoggerFactory() {}

    public String getAppId() { return appId; }
    public void setAppId( String appId ) { this.appId = appId; }

    public String getHostName() { return hostName; }
    public void setHostName( String hostName ) { this.hostName =
hostName; }

    public Logger makeNewLoggerInstance( String loggerName )
    {
        if (appId == null || hostName == null)
            throw new IllegalStateException( "Cannot access factory
prior to setting appId/hostName" );
        return new NtLogger( loggerName, appId, hostName );
    }
}


----- Original Message -----
From: "Sanjay Gupta (sanjaygu)" <sanjaygu@cisco.com>
To: "'Log4J Users List'" <log4j-user@jakarta.apache.org>
Sent: Friday, July 11, 2003 7:21 PM
Subject: How to use categoryFactory element in the properties file?


> I have extended Logger and LoggerFactory so that I can pass few params
> such as hostname.
>
> ---------Extended LoggerFactory-------------
> public NtLoggerFactory(String appId, String hostName) {
>   this.appId = appId;
>   this.hostName = hostName;
> }
>
> public Logger makeNewLoggerInstance(String name) {
>    return new NtLogger(name, this.appId, this.hostName);
> }
> -----------------------------------------------
>
> My program works fine when I initialize and configure the logger from
> the program but when I try to use DOMConfigurator using log4j.xml I am

> lost. How so I specify LoggerFactory/CategoryFactory and the params in

> the log4j.xml file?
>
> Here is the program that works without a Configurator/xml file -
> -----------------
> Logger root = Logger.getRootLogger();
> NtLoggerFactory factory = new
> NtLoggerFactory("LogWriterApp", "LocalMachine");
> Layout layout = new NtPatternLayout("%p <%a> =>%h=< [%t]
> %c (%F:%L:%M) - %m%n");
> root.addAppender(new ConsoleAppender(layout,
> ConsoleAppender.SYSTEM_OUT));
>         NtLogger logger = (NtLogger) NtLogger.getLogger("LogWriter", 
> factory);
>            logger.info("Hello info ");
>
> ---------------
>
> This is what I came up with log4j.xml and code that doesn't work------
>
> -----------log4j.xml----
> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
>         <appender name="RBERR"
> class="org.apache.log4j.RollingFileAppender">
>            <param name="File" value="D:\\Logwriter\\test.log" />
>            <param name="Threshold" value="DEBUG" />
>            <param name="MaxFileSize" value="1024KB" />
>            <param name="MaxBackupIndex" value="10" />
>            <layout class="com.sanjay.log4j.NtPatternLayout">
>               <param name="ConversionPattern" value="%p [%a] =)%h=( 
> [%t] %c (%F:%L:%M) - %m%n" />
>            </layout>
>         </appender>
>         <logger name="LogWriter">
>            <level value="debug"/>
>
>            <appender-ref ref="RBERR" />
>
>         </logger>
>         <categoryFactory
> class="com.cisco.nettools.log4j.NtLoggerFactory">
>           <param name="appId" value="MYAPPID" />
>           <param name="hostName" value="LOCAL_MACHINE" />
>         </categoryFactory>
> </log4j:configuration>
> ---------------------
>
> And java code is ---
>   DOMConfigurator.configure(args[0]);
>   NtLogger logger = (NtLogger) NtLogger.getLogger("LogWriter");
>   logger.info("Hello info ");
>
> I get the error -
> log4j:WARN No such property [appId] in
> com.cisco.nettools.log4j.NtLoggerFactory.
> log4j:WARN No such property [hostName] in 
> com.cisco.nettools.log4j.NtLoggerFactory.
> log4j:WARN No such property [appId] in 
> com.cisco.nettools.log4j.NtLoggerFactory.
> log4j:WARN No such property [hostName] in 
> com.cisco.nettools.log4j.NtLoggerFactory.
> java.lang.ClassCastException
> at com.sanjay.log4j.Log4jTest.main(Log4jTest.java:53)
> Exception in thread "main"
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: log4j-user-help@jakarta.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org


Mime
View raw message