Create Custom logger Using Log4J:
Sometimes , there are requirements to create separate log files based on some conditions , apart from root logger files . For Example, we want to log each logged in user's activity in its own separate file. So , user1 will have user1.log and each time if the user1 logged in to the system , his activity will be logged in user1.log also these log files will be created in the same folder as the root logger folder. This is a custom logger class , which will have the custom logger for every user id .In this class i have created a map which will have all the instances of custom logger , and based on the input userId , it will give the specific logger .If there is any exception creating logger , it will return root logger.We just have to call logMessage method along with the file name and message .package com.util; import java.io.File; import java.util.Enumeration; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.apache.log4j.Appender; import org.apache.log4j.FileAppender; import org.apache.log4j.Layout; import org.apache.log4j.Level; import org.apache.log4j.Logger; /** * * custom logger class to create separate logs based on user and each auction */ public class CustomLogger { private static MapPost comments to make it more useful !!m_loggers = new ConcurrentHashMap (); private static String dir; // Root log directory private static Layout layout; private static Logger rootLogger = Logger.getRootLogger(); public static synchronized void logMessage(String jobName, String message) { Logger l = getJobLogger(jobName); l.info(message); } /** * this is our custome logger which stores custome logger based on unique * name Like for each user it will have logger defined like * user_userId log files will be created in root logger directory * * @param jobName * @return */ private static Logger getJobLogger(String jobName) { Logger logger = m_loggers.get(jobName); if (dir == null || layout == null) { try { initialize(); } catch (Exception e) { rootLogger.info("error getting file appender for custom logger"); return rootLogger; } } if (logger == null) { logger = Logger.getLogger(jobName); m_loggers.put(jobName, logger); logger.setLevel(Level.INFO); try { File file = new File(dir); file.mkdirs(); file = new File(dir, jobName + ".log"); FileAppender appender = new FileAppender(layout, file.getAbsolutePath(), false); logger.removeAllAppenders(); logger.addAppender(appender); rootLogger.info("file absolute path is " + file.getAbsolutePath()); } catch (Exception e) { rootLogger.info("error getting custom logger , return root logger"); logger = rootLogger; } } return logger; } private static void initialize() throws Exception { Enumeration enumeration = Logger.getRootLogger().getAllAppenders(); while (enumeration.hasMoreElements()) { Appender app = (Appender) enumeration.nextElement(); if (app instanceof FileAppender) { layout = app.getLayout(); File f = new File(((FileAppender) app).getFile()); dir = f.getParent(); } } if (dir == null) { throw new Exception("dir is null "); } } }