logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Steve Ebersole" <steveebers...@austin.rr.com>
Subject Re: How to use categoryFactory element in the properties file?
Date Sun, 13 Jul 2003 13:26:40 GMT
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