Output of Java code is not getting in WSO2 logs

不羁的心 提交于 2019-12-25 09:10:29

问题


I am unable to see the output of the below java code in WSO2 logs.

    package com.esb.integration.mediators;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;

import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class DbLookup extends AbstractMediator {

    private String url;
    private String userName;
    private String password;
    private String database;
    private String status;
    private String action;
    private String DSP;
    private String USER;
    private String PID;
    private String PMJ;
    private String PMT;
    private String NZPO;
    private String PD;
    private String SD;
    public  boolean mediate(MessageContext context) {

        String url = (String) context.getProperty("url");
        String userName = (String) context.getProperty("userName");
        String password = (String) context.getProperty("password");
        String database = (String) context.getProperty("database");
        String status = (String) context.getProperty("status");
        String action = (String) context.getProperty("action");

        try {
            System.out.println("Inside DB Extractor");
            System.out.println("Action: "+action);
            Class.forName("com.access.JDBCDriver");

            Connection conn = DriverManager.getConnection(url,userName,password);
            Statement stmt = conn.createStatement();
            System.out.println("After getting connection");

             if (action.equals("updateData")){
                System.out.println("Inside  if: "+action);
                int result =0;
                DSP = (String) context.getProperty("DSP");
                USER = (String) context.getProperty("USER");
                PID = (String) context.getProperty("PID");
                PMJ = (String) context.getProperty("PMJ");
                PMT = (String) context.getProperty("PMT");
                NZPO = (String) context.getProperty("NZPO");
                PD = (String) context.getProperty("PD");
                SD = (String) context.getProperty("SD");

                String updateQuery = "Update Table Set DSP = '"+DSP+"',USER = '"+USER+"',PID = '"+PID+"',PMJ="+PMJ+",PMT="+PMT+" Where DSP<>'Y' AND NZPO='"+NZPO+"' AND PD='"+PD+"' AND SD="+SD;
                System.out.println("Query String: "+updateQuery);
                result = stmt.executeUpdate(updateQuery);
                if(result>0){
                    String response = "successfully updated "+result+" rows";
                    System.out.println("successfully added "+result);
                    context.setProperty("status",response);
                    return true;
                }

                else{
                    System.out.println("failed");
                    context.setProperty("status","0 rows were updated");
                    return true;
                }

            }
            else {
                context.setProperty("status","Failed");
                return false;
            }
        }catch (Exception e) {
            System.out.println("Got an exception! ");
            System.out.println(e.getMessage());
            context.setProperty("status","Failed");
            return false;
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }
    public String getstatus() {
        return status;
    }

    public void setstatus(String status) {
        this.status = status;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }


    public String getDSP() {
        return DSP;
    }
    public void setDSP(String DSP) {
        DSP = DSP;
    }
    public String getUSER() {
        return USER;
    }
    public void setUSER(String USER) {
        USER = USER;
    }
    public String getPID() {
        return PID;
    }
    public void setPID(String PID) {
        PID = PID;
    }
    public String getPMJ() {
        return PMJ;
    }
    public void setPMJ(String PMJ) {
        PMJ = PMJ;
    }
    public String getPMT() {
        return PMT;
    }
    public void setPMT(String PMT) {
        PMT = PMT;
    }
    public String getNZPO() {
        return NZPO;
    }
    public void setNZPO(String NZPO) {
        NZPO = NZPO;
    }
    public String getPD() {
        return PD;
    }
    public void setPD(String PD) {
        PD = PD;
    }
    public String getSD() {
        return SD;
    }
    public void setSD(String SD) {
        SD = SD;
    }
}

Here is the proxy service:-

<proxy xmlns="http://ws.apache.org/ns/synapse" name="TestProxy" transports="jms" statistics="disable" trace="disable" startOnLoad="true">
       <inSequence>
       <property name="userName" value="****"></property>
         <property name="password" value="****"></property>
         <property name="url" value="********"></property>         
         <property name="action" value="updateData"></property>
         <iterate id="Item" expression="//itemList/item" sequential="true">
            <target>
                 <sequence>
                                 <property name="DSP" expression="//DSP/text()"></property>
                                 <property name="USER" expression="//USER/text()"></property>
                                 <property name="PID" expression="//PID/text()"></property>
                                 <property name="PMJ" expression="//PMJ/text()"></property>
                                 <property name="PMT" expression="//PMT/text()"></property>
                                 <property name="NZPO" expression="//NZPO/text()"></property>
                                 <property name="PD" expression="//PD/text()"></property>
                                 <property name="SD" expression="//SD/text()"></property>
                                 <class name="com.esb.integration.mediators.DbLookup"></class>
                                 <log separator=",**after updatedb call**" description=""></log>

                 </sequence>
                 </target>
                 </iterator>         
         </inSequence>
         <loopback/>
          <outSequence/>
        <parameter name="transport.jms.ContentType">application/json</parameter>
    <parameter name="transport.jms.Destination">TestProxy.Q</parameter>
</proxy>

But,I can only see the "after updatedb call" message in logs when the query is executing successfully.

What should I add/modify above to get the message(which are written as System.out.println(" ")) in the WSO2 logs?


回答1:


In [ESB_HOME]\repository\conf\log4j.properties put this:

log4j.logger.package=INFO, CARBON_LOGFILE, CARBON_MEMORY

Where package = the package of your class, in my case:

log4j.logger.org.softdevelop.test=INFO, CARBON_LOGFILE, CARBON_MEMORY

Restart the server and try with:

if (log.isInfoEnabled()) {
            log.info("First Element log : " + firstElement.getLocalName());
        }       

System.out.println("First Element Text syso : " + firstElement.getText());

In my case the log print:

[2017-03-13 12:16:30,010]  INFO - testIterateProperty First Element log : jsonObject

First Element Text syso :



回答2:


log.info("whatever you want");


来源:https://stackoverflow.com/questions/42745107/output-of-java-code-is-not-getting-in-wso2-logs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!