Please follow the below steps to implement this example.
Step 1 : XML file for configuration.
log4j-configuration.xml
<xml version="1.0"
encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration
SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="ROLLING"
class="org.apache.log4j.RollingFileAppender">
<param name="File"
value="sample.log" />
<param name="MaxFileSize"
value="100KB" />
<param name="MaxBackupIndex"
value="2" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %5p [%t] %c (%F:%L) - %m%n" />
</layout>
</appender>
<root>
<appender-ref ref="ROLLING"
/>
</root>
</log4j:configuration>
Step 2 : Class to load logger configuration.
LoggerConfiguration.java:
public final class LoggerConfiguration
{
private
LoggerConfiguration() {
DOMConfigurator.configure("log4j-configuration.xml");
}
private static class
LoggerConfigurationHolder {
private static final LoggerConfiguration INSTANCE = new
LoggerConfiguration();
}
public static LoggerConfiguration
configure() {
return
LoggerConfigurationHolder.INSTANCE;
}
}
Step 3 : Class to log the logging details
MyLogger.java
import
org.apache.log4j.Level;
import
org.apache.log4j.Logger;
public class MyLogger {
private static String FQCN = MyLogger.class.getName();
private Logger logger;
public MyLogger(String name) {
LoggerConfiguration.configure();
this.logger = Logger.getLogger(name);
}
public MyLogger(@SuppressWarnings("rawtypes") Class clazz) {
this(clazz.getName());
}
public void trace(Object message) {
logger.log(FQCN, Level.TRACE, message, null);
}
public void trace(Object message, Throwable t) {
logger.log(FQCN, Level.TRACE, message, t);
}
public boolean isTraceEnabled() {
return logger.isEnabledFor(Level.TRACE);
}
public void debug(Object message) {
logger.log(FQCN, Level.DEBUG, message, null);
}
public void debug(Object message, Throwable throwable) {
logger.log(FQCN, Level.DEBUG, message, throwable);
}
public boolean isDebugEnabled() {
return logger.isDebugEnabled();
}
public void info(Object message) {
logger.log(FQCN, Level.INFO, message, null);
}
public void info(Object message, Throwable t) {
logger.log(FQCN, Level.INFO, message, t);
}
public boolean isInfoEnabled() {
return logger.isInfoEnabled();
}
public void warn(Object message) {
logger.log(FQCN, Level.WARN, message, null);
}
public void warn(Object message, Throwable t) {
logger.log(FQCN, Level.WARN, message, t);
}
public void error(Object message) {
logger.log(FQCN, Level.ERROR, message, null);
}
public void error(Object message, Throwable t) {
logger.log(FQCN, Level.ERROR, message, t);
}
public void fatal(Object message) {
logger.log(FQCN, Level.FATAL, message, null);
}
public void fatal(Object message, Throwable t) {
logger.log(FQCN, Level.FATAL, message, t);
}
}
Step 4 : Class to test the Logger.
LoggerTest.java
public class LoggerTest {
private static MyLogger LOG = new MyLogger(LoggerTest.class);
public static void main(String[] args) {
LOG.debug("Debugging
Message");
LOG.error("Error
Message");
LOG.info("Information
Message");
LOG.warn("Warning
Message");
LOG.trace("Tracing
Message");
}
}
No comments:
Post a Comment