External Logging

You can persist your  log messages by creating a logger for externalizing your API logs.  uses the java.util.logging framework for API-level logging.
lac32
You can persist your 
CA Live API Creator
 log messages by creating a logger for externalizing your API logs. 
CA Live API Creator
 uses the 
java.util.logging
framework for API-level logging.
Having your API logs in a central repository can help you:
  • Identify API errors.
  • Identify error frequency.
  • Narrow down the rules or SQL execution that are causing the API errors. 
For more information about the 
java.util.logging
 framework, see the Oracle documentation.
In this article:
3
Configure External Logging
Use the following process to configure external logging:
Create the Logging Property File
Create a logging property file that includes a logger entry for
CA Live API Creator
 logging. You can specify the logger to use the built-in
java.util.logging.FileHandler
 handler or point it to a custom handler, such as a database handler. Prefix the logger entry in the logging property file with the
lacapplogging.
 keyword, including the period (.).
The following code snippet shows an example logger entry:
lacapplogging.<YOUR LOGGER NAME>.level = FINEST
lacapplogging.<YOUR LOGGER NAME>.handlers = java.util.logging.FileHandler
The logging property file is created and is available to API Server. 
CA Live API Creator
 reads the logger that is prefixed with the 
lacapplogging.
keyword and dynamically links the logger to 
CA Live API Creator
.
Example:
The following sample logging property file uses a file handler to push API log entries to a file in the server:
# root handlers
handlers = java.util.logging.ConsoleHandler
# Set the default logging level for the root logger
.level = FINEST
# -- Logging to File -- #
lacapplogging.file.logger.level = FINEST
lacapplogging.file.logger.handlers = java.util.logging.FileHandler
java.util.logging.FileHandler.level     = FINEST
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.limit     = 0
java.util.logging.FileHandler.append    = true
java.util.logging.FileHandler.pattern   = /yourpath/applog.%u.%g.txt
java.util.logging.SimpleFormatter.format="%1$tc %2$s%n%4$s: %5$s%6$s%n"
Sample Database Handler
You can create a custom handler that logs to a database instead of to a file or a socket. The following code sample is an example of a database handler. You can use the following code sample as an example to create your own handler:
Code sample
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
/**
* This is a sample Database handler for Java.util.logging. --CUSTOMIZE and TEST BEFORE USE--
*
* Schema for the log table.
*
* CREATE TABLE lacapplog (
* ident BIGINT AUTO_INCREMENT PRIMARY KEY,
level integer NOT NULL,
logger varchar(64) NOT NULL,
message varchar(255) NOT NULL,
sequence integer NOT NULL,
sourceClass varchar(64) NOT NULL,
sourceMethod varchar(32) NOT NULL,
threadID integer NOT NULL,
timeEntered datetime NOT NULL)
*
*/
public class LACDBHandler extends Handler{
  private String driverClassName;
  private String connectionURL;
  private Connection con;
  private String username;
  private String password;
  private PreparedStatement pStmt;
  
  /**
  * Parameters to be passed to the constructor
  * @param driverClassName - Class of the driver.
  * @param connectionUrl - Full JDBC connectionURL
  * @param username - DB access Username
  * @param password - DB access password
  */
  public LACDBHandler(String driverClassName, String connectionUrl,String username, String password){
    this.driverClassName = driverClassName;
    this.connectionURL = connectionUrl;
    this.username = username;
    this.password = password;
    System.out.println("Driver :"+driverClassName);
    System.out.println("Connection URL :"+connectionURL);
  
    connect();
  }
  
  public LACDBHandler(){
  }
  private void connect() {
    try {
      Class.forName(driverClassName);
      con = DriverManager.getConnection(connectionURL, username, password);
      con.setAutoCommit(true);
      String insertQuery = "INSERT INTO `laclogging`.`lacapplog`" + "(`level`," + "`logger`,"
        + "`message`," + "`sequence`," + "`sourceClass`," + "`sourceMethod`," + "`threadId`," + "`timeEntered`)"
        + "VALUES" + "(?,?,?,?,?,?,?,?);";
      pStmt = con.prepareStatement(insertQuery);
    } catch (ClassNotFoundException e) {
        System.out.println("Driver class not found for Database Log Handler.");
        e.printStackTrace();
    } catch (SQLException e) {
        System.out.println("SQL Exception while connecting to the database for Logging.");
        e.printStackTrace();
    }
  }
/**
* Publish log record.
* @param record
*/
  @Override
  public void publish(LogRecord record) {
  
    try {
      Timestamp ts = new Timestamp(record.getMillis());
      pStmt.setInt(1, record.getLevel().intValue());
      pStmt.setString(2, record.getLoggerName());
      pStmt.setString(3, record.getMessage());
      pStmt.setLong(4, record.getSequenceNumber());
      pStmt.setString(5, record.getSourceClassName());
      pStmt.setString(6, record.getSourceMethodName());
      pStmt.setString(7,record.getThreadID()+"");
      pStmt.setTimestamp(8, ts);
         
      pStmt.executeUpdate();
  
    } catch (SQLException e) {
        System.out.println("Exception inserting :"+record.toString());
        e.printStackTrace();
    }
  }
  @Override
  public void flush() {
  // Nothing here.
  
  }
  @Override
  public void close() throws SecurityException {
    try {
      con.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
}
Create the Logger
Specify the location of the logging property file as the value for the 
java.util.logging.config.file
 Java system property.
Create the logger based on the application servers you have installed
CA Live API Creator
:
Create a Logger for a Jetty application server
Add the 
java.util.logging.config.file
 Java system property to the API Server startup:
-Djava.util.logging.config.file=/<path_to_your_property_file>/<property_file_name>
A logger for Jetty is created.
Create a Logger for an Apache Tomcat application server
  1.  Add the 
    java.util.logging.config.file
     Java system property to the API Server startup:
    -Djava.util.logging.config.file=/<path_to_your_property_file>/<property_file_name>
  2. Complete one of the following:
    • Add the logger entry for 
      CA Live API Creator
       logging to Tomcat’s JULI framework to the 
      {CATALINA_HOME}/conf/logging.properties
       file.
      Tomcat uses JULI for logging. For more information about logging in Tomcat, including JULI, see Apache Tomcat documentation.
    • Add the logging property file to the 
      {CATALINA_HOME}/webapps/<CA_Live_API_Creator_folder>/
      WEB-INF/classes
      directory.
A logger for Tomcat is created.
Create a Logger for an Oracle WebLogic application server
Add the 
java.util.logging.config.file
 Java system property to the API Server startup:
-Djava.util.logging.config.file=/<path_to_your_property_file>/<property_file_name>
A logger for WebLogic is created.
Create a Logger for a JBoss/WildFly application server
For more information about how to configure logging in JBoss/WildFly, see the JBoss documentation.
  1. From the JBoss/WildFly admin console, complete the following:
    1. Create the logging property file, 
      java.util.logging.config.file
       Java system property.
      For more information about how to create a Java system property in JBoss/WildFly, see the JBoss documentation.
    2. Add the 
      java.util.logging.config.file
       Java system property as the
      Key
      and add the absolute path to the property file as the
      Value
      .
  2. Open the <
    WILDFLY_HOME>/standalone/configuration/standalone.xml
     file and complete the following:
    1. Add the handler configuration for
      CA Live API Creator
       logging that matches the logger name in the logging properties file:
      <periodic-rotating-file-handler name="<YOUR HANDLER NAME>" autoflush="true"> <formatter> <named-formatter name="PATTERN"/> </formatter> <file path="/pathtothelogfile/server.log"/> <suffix value=".yyyy-MM-dd"/> <append value="true"/> </periodic-rotating-file-handler>
    2. Add a logger entry for 
      CA Live API Creator
       logging that matches the logger name in the logging properties file:
      <logger category="<YOUR LOGGER NAME>" use-parent-handlers="false">
      <level name="FINEST"/>
      <handlers>
      <handler name=" CALAC_FILE_HANDLER "/>
      </handlers>
      </logger>
    3. Add a formatter for the log entries:
      <formatter name="PATTERN">
      <pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%e%n"/>
      </formatter>
    The logging property file is added to JBoss/WildFly’s logging subsystem.
  3. Reload the server.
A logger for JBoss is created.
Create a Logger for an IBM WebSphere application server
Add your own application logger for 
CA Live API Creator
 deployed on WebSphere application server.
Based on your WebSphere application server setup, there might be extra configuration that is required to create a logger for WebSphere. We recommend that you contact your WebSphere administrator before creating a logger for WebSphere.
In the WebSphere administrative console, navigate to the Java virtual machine (JVM) Custom Properties page and ensure that WebSphere initializes 
java.util.logging
 by enabling the following properties:
-Djava.util.logging.config.file=/opt/laclog/laclogging.properties -Djava.util.logging.configureByLoggingPropertiesFile=true
For more information about how to add system properties to the JVM, see the IBM documentation.
A logger for WebSphere is created.
View your Logs in a Central Repository
The external logs are formatted based on the handler to which your logger is pointing.
The following properties are available in your log message:
Property
Description
date
The timestamp for the log message.
millis
The event time in milliseconds (since 1970) for the log message.
sequence
The unique sequence number for the log message.
logger
The type of logger in
CA Live API Creator
. For example,
a
dmini
and
resrcs
.
level
The log level for the log message.
Values:
(From highest to lowest) error, warning, info, debug, finer, and finest
You can control the level of logging for the auth tokens you have created within your API.
For more information about how to control the logging level, see The apikeys Endpoint.
For more information about logging levels in
CA Live API Creator
, see View Logging Information.
class
The class for the log message, using the following syntax:
host:<host name> - keyname:<key name> - <full URL>
method
The HTTP method (GET, PUT, POST, DELETE) for the log message.
thread
The thread identifier within the Java VM.
message
The log message. For example:
Resource Read demo:employee SQL: `dblocal_demo`.`employee` select 0 `EL__BATCHNUMBER`      ,`employee_id`      ,`login`      ,`name`  from `dblocal_demo`.`employee` `el__this` order by `login` limit 21