Ed --
Here's my recommendation:
1) Create a class that extends java.io.PrintStream.
2) In this class, override the public void println(String) method. In
the overridden method, evaluate the string that's being written. If the
string that's being written begins with "log4j:ERROR ", then record the
fact that log4j has encountered an error.
3) Before log4j loads it's configuration, replace System.err with your
extension of java.io.PrintStream.
4) Let log4j configure itself.
5) After the configuration is complete, restore System.err to the
original PrintStream and check your extension of java.io.PrintStream to
see if log4j wrote any error messages to System.err.
Here's some code:
// This is my extension of java.io.PrintStream
public class MyPrintStream extends PrintStream {
private List log4jErrors = new ArrayList();
public MyPrintStream(OutputStream out) throws FileNotFoundException
{
super(out);
}
public void println(String x) {
// Record any errors that originate from log4j.
if ((x != null) && (x.startsWith("log4j:ERROR "))) {
log4jErrors.add(x);
}
super.println(x);
}
public String[] getLog4jErrors() {
String[] result = new String[log4jErrors.size()];
result = (String[])log4jErrors.toArray(result);
return result;
}
}
// This class demonstrates how this would work.
private void runTest() {
try {
// Setup a new error stream.
MyPrintStream myPs = new MyPrintStream(System.err);
PrintStream oldErr = System.err;
System.setErr(myPs);
// Configure log4j.
DOMConfigurator.configureAndWatch("log4j.xml");
// Since my configuration has no errors in it, log4j didn't
write
// anything out to it's 'raw' logger. So, just for testing
purposes,
// I am going to write some messages out to log4j's 'raw' logger
in
// order to illustrate how you can respond to any errors that
occur
// during configuration. In a 'normal' situation, you would not
do
// this.
LogLog.error("Something bad has happened!!");
LogLog.error("This is an exception", new Exception(
"This is the exception"));
// Restore the old error stream.
System.setErr(oldErr);
// Check to see if log4j wrote anything out to System.err
String[] errors = myPs.getLog4jErrors();
if ((errors != null) && (errors.length != 0)) {
System.out.println("log4j recorded " + errors.length
+ (errors.length == 1 ? " error." : " errors."));
for (int i = 0; i < errors.length; i++) {
System.out.println("log4jErrors[" + i + "]="
+ errors[i]);
}
System.exit(1);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Ron Gallagher, AT&T Mobility
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Thursday, June 28, 2007 5:38 AM
To: Log4J Users List
Subject: Re: Log4j Configuration
Hi
Anybody got any ideas on this one?
Am i in correct mail list?
Rgds
Ed Howard
GSK House D3-133 ext 4996
external 00 44 020 8047 4996
[EMAIL PROTECTED] wrote on 25/06/2007 16:26:32:
> Hi
> Hopefully someone can help me.
> I want to configure log4j from a properties file.
> I am writing a little java application. I have several log4j
> appenders and everything works well. However I want the option to
> exit application if ANY of log4j configuration fails.
> By default I am seeing a warning message from Log4j but application
> continues to run...
> Do you know of a property to set for log4j that application exits if
> log4j properties are not correct?
> OR should I attempt this programmicatally.
>
> Rgds
>
> Ed Howard
>
> -----------------------------------------------------------
> This e-mail was sent by GlaxoSmithKline Services Unlimited
> (registered in England and Wales No. 1047315), which is a
> member of the GlaxoSmithKline group of companies. The
> registered address of GlaxoSmithKline Services Unlimited
> is 980 Great West Road, Brentford, Middlesex TW8 9GS.
> -----------------------------------------------------------
-----------------------------------------------------------
This e-mail was sent by GlaxoSmithKline Services Unlimited
(registered in England and Wales No. 1047315), which is a
member of the GlaxoSmithKline group of companies. The
registered address of GlaxoSmithKline Services Unlimited
is 980 Great West Road, Brentford, Middlesex TW8 9GS.
-----------------------------------------------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]