Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
Step 1:
You need to create a Log4J property files , take a look at a sample below.
log = C://logs//log
log4j.rootLogger = INFO, FILE
# Define the file appender
log4j.appender.FILE=util.ownHTMLFileAppender
log4j.appender.FILE.File=${log}/ServiceResult.html
# Define the layout for
file appender
log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout
log4j.appender.FILE.layout.Title=HTML Layout Example
log4j.appender.FILE.layout.LocationInfo=true
Step 2:
Create your own "ownHTMLFileAppender"
package util;
import
java.io.File;
import java.io.IOException;
import org.apache.log4j.FileAppender;
import
org.apache.log4j.Layout;
import
org.apache.log4j.spi.ErrorCode;
/**
*
This is a customized log4j appender, which will create a new file
for every
* run of the application.
*
*
*/
public class OwnHTMLFileAppender extends
FileAppender {
public OwnHTMLFileAppender() {
}
public OwnHTMLFileAppender(Layout layout,
String filename,
boolean append,
boolean bufferedIO, int bufferSize)
throws IOException {
super(layout, filename, append, bufferedIO,
bufferSize);
}
public
OwnHTMLFileAppender(Layout layout, String filename,
boolean append) throws IOException {
super(layout, filename, append);
}
public OwnHTMLFileAppender(Layout layout, String
filename)
throws IOException {
super(layout, filename);
}
public void activateOptions() {
if (fileName != null)
{
try {
fileName
= getNewLogFileName();
setFile(fileName, fileAppend, bufferedIO,
bufferSize);
} catch (Exception e) {
errorHandler.error("Error while
activating log options", e,
ErrorCode.FILE_OPEN_FAILURE);
}
}
}
private
String getNewLogFileName() {
if (fileName != null)
{
final String DOT =
".";
final String HIPHEN =
"-";
final File logFile =
new File(fileName);
final String fileName =
logFile.getName();
String newFileName =
"";
final int dotIndex
= fileName.indexOf(DOT);
if (dotIndex != -1)
{
// the file name has an extension.
so, insert the time stamp
// between
the file name and the extension
newFileName = fileName.substring(0, dotIndex)
+ HIPHEN
+
+System.currentTimeMillis() + DOT
+
fileName.substring(dotIndex + 1);
} else
{
// the file name has no extension.
So, just append the timestamp
// at
the end.
newFileName = fileName +
HIPHEN + System.currentTimeMillis();
}
return logFile.getParent() + File.separator +
newFileName;
}
return null;
}
}
Step 3:
Read the Properties File
static {
PropertyConfigurator.configure("C://ServiceTestNew//conf//log4j.properties");
}
Step4:
private final static Logger log = Logger.getLogger(ValidateLogger.class);
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.