Ok, so I have made some small self progress here and I think I know what the
underlining issues is.

When I debug the Log4J code, when I first initialise, like so, 

Logger.getLogger(MyClass.class);

I can see that Log4J iterates over the properties contained in my properties
file, then goes over each appender one by one and sets the properties of
that appender.

My issue comes from the fact that when the nta (NTEventLogAppender) appender
is getting its properties set, my custom "instanceId" field is being set on
the Layout (which is my CUSTPatternLayout class) BEFORE the
ConversionPattern property is being set.

This raises the issue, because in my overridden method, createPatternParser,
(which gets called from the setConversionPattern in the PatternLayout
class), I create a local instance of a CustomPatternParser:

@Override
protected PatternParser createPatternParser(String pattern) {
    this.customParser = new CustomPatternParser(pattern);
    return gbstParser;
}

and in my custom property "instanceId" setter (setInstanceId), I do this:

public void setInstanceId(String instanceId) {
    customParser.setInstanceId(instanceId);
}

now, the big million dollar question is, why ONLY in the NTEventLogAppender,
is the "instanceId" property try to be set BEFORE the ConversionPattern,
when in both the other appenders, (ConsoleAppender and RollingFileAppender),
their ConversionPattern propert gets called first...

Does this come down to how the Properties are loaded and parsed in the
internals of Log4J?

Thanks
Mark


Kram wrote:
> 
> Hello all,
> 
> I am using Log4J 1.2.15, and I have the need to output a special, custom,
> pattern to my appenders. I have looked up at the samples and googled
> around and I sort of have it working, however, when I try to add my custom
> pattern property "instanceId" to the NT Logger, I get a NPE.
> 
> My properties file is like this:
> 
> log4j.rootLogger=WARN, stdout, rfa, nta
> 
> log4j.appender.stdout=org.apache.log4j.ConsoleAppender
> log4j.appender.rfa=org.apache.log4j.RollingFileAppender
> log4j.appender.nta=org.apache.log4j.nt.NTEventLogAppender
> 
> log4j.appender.stdout.layout=com.mypackage.CUSTPatternLayout
> log4j.appender.stdout.layout.instanceId=PC1
> log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %G [%t] %-5p %c
> - %m%n
> 
> log4j.appender.rfa.layout=com.mypackage.CUSTPatternLayout
> log4j.appender.rfa.layout.instanceId=PC1
> log4j.appender.rfa.layout.ConversionPattern=%d{ISO8601} %G [%t] %-5p %c -
> %m%n
> 
> log4j.appender.nta.layout=com.mypackage.CUSTPatternLayout
> log4j.appender.nta.layout.instanceId=PC1
> log4j.appender.nta.layout.ConversionPattern=%d{ISO8601} %G [%t] %-5p %c -
> %m%n
> 
> The actual NPE comes from the nta.layout.instanceId, when I remove it, it
> all works fine. I have debugged the code for the CUSTPatternLayout class,
> and I got a bit confused as to how it all works. And that is where you
> guys come in!
> 
> I am confused as to how to the instances work, when I do something like
> this:
> 
> private final Logger logger = Logger.getLogger(MyClass.class);
> 
> Is there an instance created of CUSTPatternLayout for the layout property
> of each appender? If so, can I get each appender to share the same
> instance of CUSTPatternLayout?
> 
> like:
> log4j.appender.layout=com.mypackage.CUSTPatternLayout ???
> 
> I am a bit confused about it all at the moment, but Im getting there...
> 
> Thanks for anyone who can help out here, it is greatly appreciated!
> Mark
> 

-- 
View this message in context: 
http://www.nabble.com/Custom-Parser-to-handle-special-properties-tp18882222p18882808.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


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

Reply via email to