Reputation: 7395
In my log4j properties file I have the root logger configured with a DailyRollingFileAppender
. The name of it is INFOFILE. This is my properties file.
log4j.rootLogger = debug, INFOFILE
log4j.appender.INFOFILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.INFOFILE.File=D:/INOVA/RequestBroker/logs/mog.log
log4j.appender.INFOFILE.ImmediateFlush=true
log4j.appender.INFOFILE.Threshold=debug
log4j.appender.INFOFILE.Append=true
log4j.appender.INFOFILE.DatePattern='.' yyyy-MM-dd-a
log4j.appender.INFOFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.INFOFILE.layout.conversionPattern=%-5p %m%n
Now I need another appender called ERRORFILE with the same properties as previously configured INFOFILE appender(except File and Threshold properties those I am going to override later).
I know I can rewrite all the above properties for ERRORFILE appender, in property file. Or I can copy all the properties of INFOFILE appender to ERRORFILE appender in my java code. But before doing that I want to know whether there is any way to extend the properties of INFOFILE appender in to ERRORFILE appender.
For an example I want some thing like this in log4j.properties file for ERRORFILE appender.
log4j.appender.ERRORFILE.parent=INFOFILE
log4j.appender.ERRORFILE.File=D:/INOVA/RequestBroker/logs/mog-error.log
log4j.appender.ERRORFILE.Threshold=error
Thank you.
Upvotes: 3
Views: 1393
Reputation: 466
No, it isn't possible to "inherit" properties from another appender. The code used to parse the properties file (PropertyConfigurator class), first lists all the defined appenders and for each of them, it looks for a log4j.appender.<name>
key. The corresponding value is expected to be a class. I didn't find anything in the code that lets you reuse a previously defined appender. It means you have to specify each property of your appender.
You may be able to do it by creating your own appender class with a delegate appender. Then in the properties file, set the delegate property to INFOFILE.
I made a quick test with the following class:
public class MyAppender extends AppenderSkeleton {
private String delegate;
public String getDelegate() {
return delegate;
}
public void setDelegate(final String delegate) {
this.delegate = delegate;
}
@Override
public void close() {
Logger.getRootLogger().getAppender(delegate).close();
}
@Override
public boolean requiresLayout() {
// We won't set the layout in the configuration file
return false;
}
@Override
protected void append(final LoggingEvent event) {
Logger.getRootLogger().getAppender(delegate).doAppend(event);
}
}
And then the following configuration:
log4j.appender.ERRORFILE = org.example.MyAppender
log4j.appender.ERRORFILE.delegate = INFOFILE
It seems to work.
Upvotes: 1
Reputation: 16736
Not really an extension, but you can do this:
log4j.appender.NEWLOGGER=${log4j.appender.INFOLOGGER}
log4j.appender.NEWLOGGER.File=${log4j.appender.INFOLOGGER.File}
...
And so forth. Log4J supports variable substitution. Read here: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html
Upvotes: 2
Reputation: 1445
Here is simple example, you have to change org.myapppackagepattern with your application specific package or any hard coded logger name string
log4j.rootLogger = debug, INFOFILE
log4j.appender.INFOFILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.INFOFILE.File=D:/INOVA/RequestBroker/logs/mog.log
log4j.appender.INFOFILE.ImmediateFlush=true
log4j.appender.INFOFILE.Threshold=debug
log4j.appender.INFOFILE.Append=true
log4j.appender.INFOFILE.DatePattern='.' yyyy-MM-dd-a
log4j.appender.INFOFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.INFOFILE.layout.conversionPattern=%-5p %m%n
log4j.logger.org.myapppackagepattern=DEBUG, NEWLOGGER
log4j.appender.NEWLOGGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.NEWLOGGER.File=D:/INOVA/RequestBroker/logs/newlogger.log
log4j.appender.NEWLOGGER.ImmediateFlush=true
log4j.appender.NEWLOGGER.Threshold=debug
log4j.appender.NEWLOGGER.Append=true
log4j.appender.NEWLOGGER.DatePattern='.' yyyy-MM-dd-a
log4j.appender.NEWLOGGER.layout=org.apache.log4j.PatternLayout
log4j.appender.NEWLOGGER.layout.conversionPattern=%-5p %m%n
Upvotes: 0