Logging with Log4j

Introduction

This sample demonstrates how to replace the default Logback logging back end with the Log4j back end. There are four criteria that must be met to successfully replace the Logback back end with the Log4J back end. The criteria can each be implemented in different ways to impact the different ways to run StreamBase applications. For more details on these criteria, refer to the Help for Advanced: Replacing Logback with Log4J.

This sample project has been pre-configured to meet these criteria in the following ways:

  1. The STREAMBASE_USE_CUSTOM_LOGGING environment variable is set to 'true' in the StreamBase Studio launch configuration. In addition, the sbclient-no-logging-backend.jar has been added to the Project's Referenced Libraries

  2. For Windows environments, the sbclient.jar is removed from the CLASSPATH when running the sample applications from a StreamBase Command Prompt.

  3. slf4j-log4j12-x.y.z.jar and log4j-x.y.z.jar have been added to the StreamBase Server Configuration File (sbd.sbconf) under the <java-vm> section. These files are also included in the Project's Referenced Libraries.

  4. The location of the log4j configuration file (log4j.xml) has been added to sbd.sbconf under the <java-vm> section. StreamBase Server reads its Log4J configuration instructions from this file.

This Sample's Files

The following files are included in the root of the sample's project:

  • log4j.xml — The log4j configuration file from which StreamBase Server reads its Log4j configuration instructions.

  • AppThatLogs2-LogAdapter.launch.sbapp — A sample Event Flow application that uses the Log Output Adapter to define a custom message format for log entries.

  • AppThatLogs2-LogAdapter.launch — A launch configuration for AppThatLogs2-LogAdapter.sbapp that sets the STREAMBASE_USE_CUSTOM_LOGGING environment variable.

  • logging-sample.sbfs — A feed simulation that feeds tuples into the AppThatLogs2-LogAdapter.sbapp application.

  • AppThatLogs2.sbapp — A sample Event Flow application that uses a Custom Java Operator class (com.streambase.sample.LoggingOperator) to add a log entry for each tuple processed. Each tuple processed is identified by an integer field x on the Input Stream.

  • AppThatLogs2.launch — A launch configuration for LogOperator.sbapp that sets the STREAMBASE_USE_CUSTOM_LOGGING environment variable.

  • LoggingOperator.java — Javalogs source for the LoggingOperator class used in the LogOperator.sbapp sample application.

  • LoggingOperatorBeanInfo.java — The BeanInfo class to control which properties in the custom logging operator are exposed to the user.

  • run-sbd-with-env.cmd — A batch script that sets the CLASSPATH and STREAMBASE_USE_CUSTOM_LOGGING environment variables. Use when running the sample applications from a StreamBase Command Prompt on Windows systems.

  • run-sbd-with-env.sh — A shell script that sets the STREAMBASE_USE_CUSTOM_LOGGING environment variable. Use when running the application from a UNIX terminal window.

  • sbd.sbconf — A StreamBase Server configuration file containing references to the Log4j configuration file and required .jar files.

Running This Sample in StreamBase Studio

To run either sample in StreamBase Studio, in the Project Explorer, open the sample_logging-log4j project folder.

AppThatLogs2-LogAdapter.sbapp

  1. In the Project Explorer view, right-click on AppThatLogs2-LogAdapter.launch and select Run AsAppThatLogs2-LogAdapter. This opens the SB Test/Debug perspective and starts the application.

  2. In the Feed Simulations tab, select the logging-sample.sbfs feed simulation and click the Run button.

  3. In the Console view, note the custom log entries generated from the custom message format in the Log Output Adapter:

    [Log4j INFO] Log4jSample - 500 shares of SymA traded at $170.00.
    [Log4j INFO] Log4jSample - 400 shares of SymA traded at $169.00.
    ...
    
  4. When done, press F9 or click the Stop Running Application button.

  5. In the Project Explorer view, select the sample_logging-log4j root directory and press F5 to refresh its contents. The resulting console output is logged to sample-log4j.log.

  6. Alternatively, you may run the application with DEBUG level logging by modifying the root <priority> element in log4j.xml:

    ...
    <root>
         <priority value="DEBUG"/>
         ...
    

AppThatLogs2.sbapp:

  1. In the Project Explorer view, right-click on AppThatLogs2.launch and select Run AsAppThatLogs2. This opens the SB Test/Debug perspective and starts the application.

  2. In the Feed Simulations tab, select the logging-sample.sbfs feed simulation and click the Run button.

  3. In the Console view, note the custom log entries generated from the LoggingOperator Custom Java Operator:

    [Log4j INFO] LoggingOperator - Processing tuple: 7309
    [Log4j INFO] LoggingOperator - Processing tuple: 2405
    [Log4j INFO] LoggingOperator - Processing tuple: 6374
     ...
    
  4. When done, press F9 or click the Stop Running Application button.

  5. In the Project Explorer view, select the sample_logging-log4j root directory and press F5 to refresh its contents. The console output you observed has been written to this file.

  6. Alternatively, you may run the application with DEBUG level logging by modifying the root <priority> element in log4j.xml:

    ...
    <root>
         <priority value="DEBUG"/>
         ...
    

Running This Sample in Terminal Windows

This section describes how to run the sample in UNIX terminal windows or Windows command prompt windows. On Windows, be sure to use the StreamBase Command Prompt from the Start menu as described in the Test/Debug Guide, not the default command prompt.

  1. Open two terminal windows on UNIX, or two StreamBase Command Prompts on Windows. In each window, navigate to your workspace copy of the sample.

    sbd -f sbd.sbconf AppThatLogs2-LogAdapter.sbapp
  2. In Window 2, run the following command to enqueue tuples to the application's Input Stream:

    sbc enqueue InputStream

    then type a comma-separated set of values for symbol, price, and numshares. For example:

    IBM, 135.77, 200
    IBM, 135.58, 2000
  3. Confirm that these entries have been logged in the resulting sample-log4j.log file, which exists in the logs sub-directory in your workspace project folder. The custom message format set by the Log Output Adapter should produce the entries:

    [Log4j INFO] LoggingSample - 200 shares of IBM traded at $135.77.
    [Log4j INFO] LoggingSample - 2,000 shares of IBM traded at $135.58.
  4. In window 2, type: Ctrl+C to exit the sbc enqueue session.

  5. In window 2, type the following command to terminate the sbd server instance: sbadmin shutdown

  6. Alternatively, you may run the application with DEBUG level logging by modifying the root <priority> element in log4j.xml:

    ...
    <root>
         <priority value="DEBUG"/>
         ...
    

AppThatLogs2.sbapp:

  1. Open two terminal windows on UNIX, or two StreamBase Command Prompts on Windows. In each window, navigate to your workspace copy of the sample.

  2. In Window 1, run either run-sbd-with-env.cmd (Windows) or run-sbd-with-env.sh (UNIX) to configure the environment to use custom logging. Then start the application using the command:

    sbd -f sbd.sbconf AppThatLogs2.sbapp
  3. In Window 2, run the following command to enqueue tuples to the application's Input Stream:

    sbc enqueue InputStream

    Then enter the following values for the tuple ID field 'x':

    1
    2
    3
  4. Confirm that these entries have been logged in the resulting sample-log4j.log file, which exists in the logs sub-directory in your workspace project folder. The custom message format set by the Log Output Adapter should produce the entries:

    [Log4j INFO] LoggingOperator - Processing tuple: 1
    [Log4j INFO] LoggingOperator - Processing tuple: 2
    [Log4j INFO] LoggingOperator - Processing tuple: 3
  5. In Window 2, type: Ctrl+C to exit the sbc enqueue session.

  6. Again in Window 2, type the following command to terminate the sbd server instance:

    sbadmin shutdown
  7. Alternatively, you may run the application with DEBUG level logging by modifying the root <priority> element in log4j.xml:

    ...
    <root>
         <priority value="DEBUG"/>
         ...
    

Importing This Sample into StreamBase Studio

In StreamBase Studio, import this sample with the following steps:

  • From the top menu, select FileLoad StreamBase Sample.

  • Select StreamBase Deployment Files from the Application category.

  • Click OK.

StreamBase Studio creates a project for the sample.

Sample Location

When you load the sample into StreamBase Studio, Studio copies the sample project's files to your Studio workspace, which is normally part of your home directory, with full access rights.

Important

Load this sample in StreamBase Studio, and thereafter use the Studio workspace copy of the sample to run and test it, even when running from the command prompt.

Using the workspace copy of the sample avoids permission problems. The default workspace location for this sample is:

studio-workspace/sample_sbdeploy

See Default Installation Directories for the location of studio-workspace on your system.

In the default TIBCO StreamBase installation, this sample's files are initially installed in:

streambase-install-dir/sample/sbdeploy

See Default Installation Directories for the default location of studio-workspace on your system.