Wednesday 12 November 2014

How to use log4j.framework in java Application(project)


package com.logfor.common.notification;
public class Notification{

   public static final Logger logger = Logger.getLogger(Notification.class);
    public void notify(VO VO) throws ApplicationException {
     
        if(ConfigFile.getConfigProperty(Constants.LOG_SEVERITYS).contains(VO.getLogLevel())) {
           logMessage(VO);
        }
        if(ConfigFile.getConfigProperty(Constants.EMAIL_SEVERITYS).contains(VO.getLogLevel())) {
           //Call Email Notification
           if(Constants.DB.equals(VO.getGroupName())) {
               VO.setToList(ConfigFile.getConfigProperty(Constants.DB_GROUP));
               VO.setMailSubject(ConfigFile.getConfigProperty(Constants.MAIL_SUBJECT));
           }
           Email.send(VO);//for sending the message
           logger.info("Email Notification");
       }
       if(ConfigFile.getConfigProperty(Constants.EXCEPTION_SEVERITYS).contains(VO.getLogLevel())) {
           //Call Email Notification
          // logMessage(VO);
          logger.info("Exception Notification");
           if(StringUtils.isBlank(VO.getDisplayMessage())){
               throw new ApplicationException(ConfigFile.getConfigProperty(Constants.DISPLAY_MESSAGE));
           }else{
                throw new ApplicationException(VO.getDisplayMessage());
            }//else
         
       }
 
   }
    public  void logMessage(VO VO) {
        String logMessage="";
        StringBuilder sb=new StringBuilder();
        sb.append(VO.getLogLevel()+"~");
        sb.append(VO.getClassName().getClass().getName());  
        logMessage=sb.toString();
        if(Constants.INFO.equals(VO.getLogLevel()))
          logger.info(logMessage);
        else if(Constants.WARN.equals(VO.getLogLevel()))
          logger.warn(logMessage);
        else if(Constants.ERROR.equals(VO.getLogLevel()))
          logger.error(logMessage);
        else if(Constants.FATAL.equals(VO.getLogLevel()))
          logger.fatal(logMessage);
        else if(Constants.DEBUG.equals(VO.getLogLevel()))
         logger.debug(logMessage);
    }
 
}
-----------------------------------------------------
now we need a VO which has all value need to show in logging for .log

package com.ashish.eapply.vo;
public class VO extends BaseVTO{
    
    private String logLevel;
    private Object className;
    private String methodName;   
    /**
     * @param logLevel
     * @param message
     * @param className
     * @param methodName
     */
    public VO(String logLevel, String message, Object className, String methodName)
    {
        super();
        this.logLevel = logLevel;
        this.message = message;
        this.className = className;
        this.methodName = methodName;
    }
    public VO(String logLevel, String logMessage, String displayMessage, Object className, String methodName)
    {
        super();
        this.logLevel = logLevel;
        this.message = logMessage;
        this.displayMessage = displayMessage;
        this.className = className;
        this.methodName = methodName;
    } 
    public VO(String logLevel, String message, Object className, String methodName,String groupName)
    {
        super();
        this.logLevel = logLevel;
        this.message = message;
        this.className = className;
        this.methodName = methodName;
        this.groupName = groupName;
    }

    public VO() {
        super();
    }
create getter settere..
---------------------------------------------------------------------------
 now keep this kind of code in your application .anywhere ...all the best

       try {
                     notification.notify(new NotifyVO(Constants.INFO,
                                                  "in valide  Number :"+no,
                                                  this,Thread.currentThread().getStackTrace()[1].getMethodName() +
                                                  NotificationConstants.LINE +Thread.currentThread().getStackTrace()[1].getLineNumber()));
                 } catch (ApplicationException e) {
                 }
 
        try {
                // logic based on logic
            } catch (Exception e) {
                notification.notify(new VO(Constants.INFO," Exception ", this,Thread.currentThread().getStackTrace()[1].getMethodName() +
                                                  NotificationConstants.LINE +Thread.currentThread().getStackTrace()[1].getLineNumber()));   
            }


No comments:

Post a Comment