Join us on May 14-15, 2019 at Forrester Digital Transformation & Innovation 2019 in Chicago, USA.

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

While using BeanShell, if we need to log the messages (info, error, warning) properly in joget.log file, we can use the joget LogUtil api. It is a very handy feature.

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

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

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

Sample Code

Code Block
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 also refer here.