If you are using ANT for daily EOD build, then you may wish to get a pager or e-mail notification when an error occurs during the build process. Integrating log4j with ANT is fairly straight-forward, as the previous part has explored. But there might be scenarios where using the default log4j configuration may not be desirable.
1. If you have more than one build routines running under the same project, you may not wish to maintain multiple log4j.properties files.
2. If you are deploying an enterprise application, and your EOD build script requires the loading of databases, you may have additional interested parties such as DBAs and network administrators who will monitor your process and potentially make changes. Maintaining a separate properties file for logging may not be acceptable to them. In that case, you may find yourself having to consolidate the log4j.properties file into a properties file configuring the major aspects of your ANT program.
The following code demonstrates how to write a custom ANT task to hook a particular ANT script to log4j, and to read the main ANT properties file for that program for the log4j settings.
Properties props = new Properties();
try {
//props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(
// "ant_config.properties"));
props.load(new FileInputStream(configFile));
Enumeration enum =
props.propertyNames();
while(enum.hasMoreElements()){
String name =
(String) enum.nextElement();
if(!name.startsWith("log4j.")){
props.remove(name);
}
else{
log(name + "=" + props.getProperty(name));
}
}
PropertyConfigurator.configure(props);
} catch (IOException e) {
e.printStackTrace();
}
project.addBuildListener(new Log4jListener());
After this is done, then there is no mystery in getting log4j to send out e-mail or pager notifications when something goes wrong with your build process. Below is an example log4j configuration that enables the SMTPAppender for e-mails.
# log4j configuration
log4j.rootLogger=INFO, R, mail
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=build.log
log4j.appender.R.MaxFileSize=10000KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.threshold=ERROR
log4j.appender.mail.SMTPHost=youmailhost.com
log4j.appender.mail.BufferSize=1
log4j.appender.mail.subject=Build failure
log4j.appender.mail.from=yourbuild@build.com
log4j.appender.mail.to=xyz@zyx.com,zzz@att.mobile.com
log4j.appender.mail.layout=org.apache.log4j.HTMLLayout
#log4j.appender.mail.layout.ConversionPattern=%d{ISO8601} %5p [%t] (%F:%L) - %m%n
1. If you have more than one build routines running under the same project, you may not wish to maintain multiple log4j.properties files.
2. If you are deploying an enterprise application, and your EOD build script requires the loading of databases, you may have additional interested parties such as DBAs and network administrators who will monitor your process and potentially make changes. Maintaining a separate properties file for logging may not be acceptable to them. In that case, you may find yourself having to consolidate the log4j.properties file into a properties file configuring the major aspects of your ANT program.
The following code demonstrates how to write a custom ANT task to hook a particular ANT script to log4j, and to read the main ANT properties file for that program for the log4j settings.
Properties props = new Properties();
try {
//props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(
// "ant_config.properties"));
props.load(new FileInputStream(configFile));
Enumeration enum =
props.propertyNames();
while(enum.hasMoreElements()){
String name =
(String) enum.nextElement();
if(!name.startsWith("log4j.")){
props.remove(name);
}
else{
log(name + "=" + props.getProperty(name));
}
}
PropertyConfigurator.configure(props);
} catch (IOException e) {
e.printStackTrace();
}
project.addBuildListener(new Log4jListener());
After this is done, then there is no mystery in getting log4j to send out e-mail or pager notifications when something goes wrong with your build process. Below is an example log4j configuration that enables the SMTPAppender for e-mails.
# log4j configuration
log4j.rootLogger=INFO, R, mail
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=build.log
log4j.appender.R.MaxFileSize=10000KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.threshold=ERROR
log4j.appender.mail.SMTPHost=youmailhost.com
log4j.appender.mail.BufferSize=1
log4j.appender.mail.subject=Build failure
log4j.appender.mail.from=yourbuild@build.com
log4j.appender.mail.to=xyz@zyx.com,zzz@att.mobile.com
log4j.appender.mail.layout=org.apache.log4j.HTMLLayout
#log4j.appender.mail.layout.ConversionPattern=%d{ISO8601} %5p [%t] (%F:%L) - %m%n