Wednesday, 30 April 2014

Implementing Log4J on PCF related Java class

Step1:
Create a java project in Eclipse with name  GETCLUSQMGR.

Step2:
By default  we have a "src" folder  under  this folder  create a package by name com.logger.test and place the java classes with PCF,Logger  business logic.

Step3:
Create a source folder by name "resource" and add log4j2.xml file under it.

Step4:
Now the folder structure of the java project  will look like below


Step5:
Add the below code to ClusQmgr.java

package com.logger.test;
import java.io.IOException;
import org.apache.logging.log4j.Logger;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.CMQCFC;
import com.ibm.mq.constants.MQConstants;
import com.ibm.mq.headers.pcf.PCFException;
import com.ibm.mq.headers.pcf.PCFMessage;
import com.ibm.mq.headers.pcf.PCFMessageAgent;
import com.ibm.mq.headers.MQDataException;


public class ClusQmgr
{     
static final Logger    logger      = CodeLogger.getLogger(ClusQmgr.class.getName());

public static void main (String [] args) throws MQDataException,PCFException, MQDataException,IOException
    {
              logger.debug("PCFMessageAgentCreation Start");
               PCFMessageAgent agent = new PCFMessageAgent("EB310000");

               logger.debug("Inquiring Cluster Queue Managers");
                PCFMessage pcfCmd = new PCFMessage(CMQCFC.MQCMD_INQUIRE_CLUSTER_Q_MGR);

                  pcfCmd.addParameter(CMQC.MQCA_CLUSTER_Q_MGR_NAME,"*");
      
                  logger.debug("Cluster Queue Managers Details");
                PCFMessage[] pcfResponse =agent.send(pcfCmd);

        System.out.println("**QueueManager**Host**Port**");
         String ClustQmgr=" ";
         String[] ConDetails;
         String Hostname=" ";
         String Port=" ";

 for (PCFMessage pcfMessage : pcfResponse)
{
     ClustQmgr=pcfMessage.getParameterValue(CMQC.MQCA_CLUSTER_Q_MGR_NAME).toString().trim();
      ConDetails=(pcfMessage.getParameterValue(MQConstants.MQCACH_CONNECTION_NAME).toString().trim()).split("\\(",2);
     Hostname=ConDetails[0];
     Port=ConDetails[1].substring(0,ConDetails[1].length()-1 );
      System.out.println("*****"+ClustQmgr+"*******"+Hostname+"*********"+Port+"**********");
      logger.debug("Values"+"**"+ClustQmgr+"**"+Hostname+"**"+Port+"**");
                    
       }
    logger.debug("End of PCF Details");

}
}

Step6:
Add the below code to CodeLogger.java

package com.logger.test;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.ConfigurationFactory.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;

public final class CodeLogger {
     
private static final String   LOG4J_CONFIG_FILE = "log4j2.xml";
     
static
{
ConfigurationSource source = new ConfigurationSource();
source.setInputStream(CodeLogger.class.getClassLoader().getResourceAsStream(LOG4J_CONFIG_FILE));
Configurator.initialize(CodeLogger.class.getClassLoader(), source);
}
     
public static Logger getLogger(String name)
{
return LogManager.getLogger(name);
}
}

Step7:
Add the below properties  to log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<properties>
<property name="logFileDir">C:\\LOG4J\\</property>
<property name="logFileName">logger</property>
</properties>

<appenders>
             
<RollingFile name="RollingFile" fileName="${logFileDir}${logFileName}.log"
                     filePattern="${logFileDir}$${date:yyyy-MM}/${logFileName}-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="[%d{ISO8601}] [%t] %-5p %c{6} - %msg%n" />

<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 MB" />
</Policies>

</RollingFile>

</appenders>
<!-- Logging done on particular package bases "com.logger.test"-->
<loggers>
<logger name="com.logger.test" additivity="false" level="DEBUG">
<AppenderRef ref="RollingFile" />
</logger>
</loggers>

</configuration>

Step8:
Add the below jars to the Java Build Path of the project
log4j-api-2.0-rc1.jar, log4j-core-2.0-rc1.jar

Step9:
OutPut of the logger file will look like below

[2014-04-30 19:58:18,493] [main] DEBUG com.logger.test.ClusQmgr - PCFMessageAgentCreation Start
[2014-04-30 19:58:18,758] [main] DEBUG com.logger.test.ClusQmgr - Inquiring Cluster Queue Managers
[2014-04-30 19:58:18,758] [main] DEBUG com.logger.test.ClusQmgr - Cluster Queue Managers Details
[2014-04-30 19:58:18,774] [main] DEBUG com.logger.test.ClusQmgr - Values**EB310000**miracle-PC**1000**
[2014-04-30 19:58:18,774] [main] DEBUG com.logger.test.ClusQmgr - Values**EB310001**miracle-PC**1003**
[2014-04-30 19:58:18,774] [main] DEBUG com.logger.test.ClusQmgr - End of PCF Details








No comments:

Post a Comment