Custom Logger:
package myapp.misc;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
public class CustomLogger {
private static String BASE_PATH = "/var/log/custom/";
private static String CLASS_NAME = "myapp.misc.CustomLogger";
private static Logger customLogger1;
static {
init();
}
public static void logDebug(String msg) {
customLogger1.log(CLASS_NAME, Level.DEBUG, msg, null);
}
public static void logInfo(String msg) {
customLogger1.log(CLASS_NAME, Level.INFO, msg, null);
}
public static void logWarn(String msg) {
customLogger1.log(CLASS_NAME, Level.WARN, msg, null);
}
public static void logError(String msg) {
customLogger1.log(CLASS_NAME, Level.ERROR, msg, null);
}
public static void logError(String msg, Throwable throwable) {
customLogger1.log(CLASS_NAME, Level.ERROR, msg, throwable);
}
private static void init() {
customLogger1 = Logger.getLogger(CustomLogger.class);
if (customLogger1.getAppender("RollingFileAppender_custom1") == null) {
RollingFileAppender rollingFileAppender = new RollingFileAppender();
rollingFileAppender.setName("RollingFileAppender_custom1");
rollingFileAppender.setFile(BASE_PATH + "catalog.log");
rollingFileAppender.setLayout(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss,SSS/zzz} [%t] %5p [%C %M() : line %L] :: %m%n"));
rollingFileAppender.setThreshold(Level.INFO);
rollingFileAppender.setAppend(true);
rollingFileAppender.setMaxFileSize("50MB");
rollingFileAppender.setMaxBackupIndex(100);
rollingFileAppender.activateOptions();
customLogger1.addAppender(rollingFileAppender);
}
if (customLogger1.getAppender("RollingFileAppender_custom1_error") == null) {
RollingFileAppender rollingFileAppenderError = new RollingFileAppender();
rollingFileAppenderError.setName("RollingFileAppender_custom1_error");
rollingFileAppenderError.setFile(BASE_PATH + "catalog_error.log");
rollingFileAppenderError.setLayout(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss,SSS/zzz} [%t] %5p [%C %M() : line %L] :: %m%n"));
rollingFileAppenderError.setThreshold(Level.WARN);
rollingFileAppenderError.setAppend(true);
rollingFileAppenderError.setMaxFileSize("50MB");
rollingFileAppenderError.setMaxBackupIndex(100);
rollingFileAppenderError.activateOptions();
customLogger1.addAppender(rollingFileAppenderError);
}
if (customLogger1.getAppender("RollingFileAppender_custom1_console") == null) {
ConsoleAppender consoleAppender = new ConsoleAppender();
consoleAppender.setName("RollingFileAppender_custom1_console");
consoleAppender.setLayout(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss,SSS/zzz} [%t] %5p [%C %M() : line %L] :: %m%n"));
consoleAppender.setThreshold(Level.INFO);
consoleAppender.activateOptions();
// customLogger1.addAppender(consoleAppender);
}
}
}
package myapp.misc;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
public class CustomLogger {
private static String BASE_PATH = "/var/log/custom/";
private static String CLASS_NAME = "myapp.misc.CustomLogger";
private static Logger customLogger1;
static {
init();
}
public static void logDebug(String msg) {
customLogger1.log(CLASS_NAME, Level.DEBUG, msg, null);
}
public static void logInfo(String msg) {
customLogger1.log(CLASS_NAME, Level.INFO, msg, null);
}
public static void logWarn(String msg) {
customLogger1.log(CLASS_NAME, Level.WARN, msg, null);
}
public static void logError(String msg) {
customLogger1.log(CLASS_NAME, Level.ERROR, msg, null);
}
public static void logError(String msg, Throwable throwable) {
customLogger1.log(CLASS_NAME, Level.ERROR, msg, throwable);
}
private static void init() {
customLogger1 = Logger.getLogger(CustomLogger.class);
if (customLogger1.getAppender("RollingFileAppender_custom1") == null) {
RollingFileAppender rollingFileAppender = new RollingFileAppender();
rollingFileAppender.setName("RollingFileAppender_custom1");
rollingFileAppender.setFile(BASE_PATH + "catalog.log");
rollingFileAppender.setLayout(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss,SSS/zzz} [%t] %5p [%C %M() : line %L] :: %m%n"));
rollingFileAppender.setThreshold(Level.INFO);
rollingFileAppender.setAppend(true);
rollingFileAppender.setMaxFileSize("50MB");
rollingFileAppender.setMaxBackupIndex(100);
rollingFileAppender.activateOptions();
customLogger1.addAppender(rollingFileAppender);
}
if (customLogger1.getAppender("RollingFileAppender_custom1_error") == null) {
RollingFileAppender rollingFileAppenderError = new RollingFileAppender();
rollingFileAppenderError.setName("RollingFileAppender_custom1_error");
rollingFileAppenderError.setFile(BASE_PATH + "catalog_error.log");
rollingFileAppenderError.setLayout(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss,SSS/zzz} [%t] %5p [%C %M() : line %L] :: %m%n"));
rollingFileAppenderError.setThreshold(Level.WARN);
rollingFileAppenderError.setAppend(true);
rollingFileAppenderError.setMaxFileSize("50MB");
rollingFileAppenderError.setMaxBackupIndex(100);
rollingFileAppenderError.activateOptions();
customLogger1.addAppender(rollingFileAppenderError);
}
if (customLogger1.getAppender("RollingFileAppender_custom1_console") == null) {
ConsoleAppender consoleAppender = new ConsoleAppender();
consoleAppender.setName("RollingFileAppender_custom1_console");
consoleAppender.setLayout(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss,SSS/zzz} [%t] %5p [%C %M() : line %L] :: %m%n"));
consoleAppender.setThreshold(Level.INFO);
consoleAppender.activateOptions();
// customLogger1.addAppender(consoleAppender);
}
}
}