Log Output Adapter Sample

Introduction

This sample demonstrates the TIBCO StreamBase® Log File Output Adapter, an output adapter that passes its input tuple unchanged to the currently configured StreamBase logging system and then to it its output port. The Log adapter participates automatically in the logging system currently in use by its containing StreamBase Server. This means that by default, the Log adapter uses:

  • Logback logging when run in Studio or when run from the command line with sbd and no -b switch.

  • Log4j logging when run as a Windows service or when run from the command line with sbd -b.

The sample's input stream has a schema of {symbol: string, price: double, numshares: double}. The input stream is followed by a Map operator whose sole purpose is to provide default values for the input stream; this allows you to click the Send Data button in the Manual Input view in StreamBase Studio without entering any values. The incoming tuple is then split into three instances of the Log adapter, each of which illustrates a different formatting option of the same log message written to the Console view.

Logger1 shows the output of a default-configured Log adapter, where only the Logger Name property is specified. With the default input tuple, this adapter emits a log message like the following. (The following examples are shown on two lines for publication clarity, but are emitted as one long line each.)

2011-08-23 20:19:56.798-0400 [ThreadPool-9] WARN  
   Logger1 - IBM,164.35,1000.0

Logger2 illustrates two message formatting options, the inclusion of input tuple field names, and a message prefix.

2011-08-23 20:19:56.799-0400 [ThreadPool-9] WARN  
   Logger2 - Incoming tuple: (tupleid=0,symbol="IBM",price=164.35,numshares=1000.0)

Logger3 illustrates the Custom MessageFormat control, which provides a format string in the format defined by java.text.MessageFormat, with zero-based positional parameters filled in with field values. The provided format string is:

{2, number, integer} shares of {0} traded at {1, number, currency}.

which results in a log message like the following:

2011-08-23 20:19:56.800-0400 [ThreadPool-9] WARN  
   Logger3 - 1,000 shares of IBM traded at $164.35.

Notice that the overall format of these log messages is the default Logback message format as described in the Logback Default Configuration section of Using StreamBase Logging in the Administrator's Guide. The message formatting options described above only apply to the message text portion (%m) of the log message — the portion shown on the second line of each example above.

To change the format of the overall log message, you must configure a PatternLayout format in the Logback or Log4j configuration file, as described on the Using StreamBase Logging page. To see this in action, go through the next section's extra steps when running the sample in Studio.

Running this Sample in StreamBase Studio

  1. In the Package Explorer view, double-click to open the log.sbapp application. Make sure the application is the currently active tab in the EventFlow Editor.

  2. Click the Run button. This opens the SB Test/Debug perspective and starts the application.

  3. In the Manual Input view, click Send Data without entering any field values. This sends the default input tuple defined in the DefaultValues operator.

  4. As it always does, the Application Output view shows the tuples emitted on the output port. But pay attention to the Console View, which also shows one log message for each tuple emitted.

  5. Now, enter values in the Manual Input view and click Send Data. The Application Output view shows three emitted tuples, and the Console view shows three log messages.

  6. When done, press F9 or click the Stop Running Application button.

Extra Steps.To see the effect of changing the overall log message format, run these additional steps:

  1. In Studio, use FileLoad StreamBase Sample to load the logging-logback sample.

  2. In the Package Explorer view, copy two files, logback.xml and sbd.sbconf, from the sample_logging-logback folder to this sample_adapter_embedded_log sample's folder.

  3. Click the Run button again to restart this sample.

  4. In the Manual Input view, once again click Send Data without entering any field values.

  5. Notice that the three log messages emitted on the console now have a different format, but the message text portion of each message is the same as before:

    [LogBack WARN] Logger1 - IBM,164.35,1000.0
    [LogBack WARN] Logger2 - Incoming tuple: (tupleid=0,symbol="IBM",price=164.35,numshares=1000.0)
    [LogBack WARN] Logger3 - 1,000 shares of IBM traded at $164.35.
  6. When done, press F9 or click the Stop Running Application button.

  7. Press F5 to refresh the Package Explorer view. Notice that the new configuration wrote log files as well as emitting messages on the console.

See the logging-logback sample's README file for further details.

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, as described above.

  2. In window 1, type:

    sbd log.sbapp

  3. In window 2: type:

    sbc enq LoggerIn

  4. In window 2, type a comma-separated set of values for symbol, price, and number of shares. For example:

    IBM,164.35,1000
    DELL,14.68,500

    Press Return after each set. Notice that there are three log messages written in window 1 for each input tuple you send.

  5. In window 2, type: Ctrl+C to exit the sbc session.

  6. In window 2, type the following command to terminate the server:

    sbadmin shutdown

Importing This Sample into StreamBase Studio

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

  • From the top menu, click FileLoad StreamBase Sample.

  • Select this sample from the Embedded Output Adapters list.

  • Click OK.

StreamBase Studio creates a single project containing the sample files.

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_adapter_embedded_log

See Default Installation Directories for the default 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/adapter/embedded/log

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