You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

While using beanshell, if we need to log the messages (info, error, warn) properly in joget.log file, we can use the joget LogUtil api. Its a very handy feature.

// logging message - info
LogUtil.info("ClassName","Message");

// logging message - warn
LogUtil.warn("ClassName","Message");

// logging message - error
LogUtil.error("ClassName",Exception,"Message");

Sample Code

import org.joget.apps.form.model.*;
import org.joget.apps.form.service.*;
import java.sql.*;
import org.apache.commons.collections.SequencedHashMap;
import java.util.*;
import org.joget.commons.util.LogUtil;
import org.joget.apps.app.service.AppUtil;

Class.forName("com.mysql.jdbc.Driver").newInstance();
try {
    con = DriverManager.getConnection("jdbc:mysql://localhost:3307/jwdb?characterEncoding=UTF-8", "root", "");
    if(!con.isClosed()){
        String sql = "INSERT INTO table(col1,col2) VALUES(?,?)";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, "coffee");
        ps.setString(2, "coffee");
        ps.executeUpdate();
        // Loggin the info message that record has been inserted
        LogUtil.info("","("+AppUtil.getCurrentAppDefinition().getAppId()+") - Record Insert Successfully.");
    } else {
       // Loggin the warning message the cant connect to database
        LogUtil.warn("","("+AppUtil.getCurrentAppDefinition().getAppId()+") - Datebase connection is not open");
    }
} catch (InstantiationException ex) {
    LogUtil.error("",ex,"("+AppUtil.getCurrentAppDefinition().getAppId()+") - can not create the instance of database driver.");
} catch (IllegalAccessException ex) {
    LogUtil.error("",ex,"("+AppUtil.getCurrentAppDefinition().getAppId()+") - IllegalAccessException OR Custom Exception message");
} catch (ClassNotFoundException ex) {
    LogUtil.error("",ex,"("+AppUtil.getCurrentAppDefinition().getAppId()+") - ClassNotFoundException - Database Driver not found");
} catch (SQLException ex) {
    LogUtil.error("",ex,"("+AppUtil.getCurrentAppDefinition().getAppId()+") - SQL Exception :- "+ex.toString());
}

You can refer [here|https://github.com/jogetworkflow/jogetworkflow/blob/master/wflow-commons/src/main/java/org/joget/commons/util/LogUtil.java] also

  • No labels