Log Annotate

Introduction

Consider this problem.

You have a channel that has two translator instances in it – let’s say a From HTTP translator instance and a To Translator instance. When logging occurs in either translator component how do you tell which component was logging?

This module can solve this problem. It modifies the built in logging commands in the translator like:

  • print
  • iguana.logInfo
  • iguana.logWarning
  • etc.

And puts in new versions which put a prefix of the translator type followed by “: ” in front of each log statement. So imagine you had a line that logged “Sent message” within the From HTTP component of a channel then the log would show “HTTP: Sent message” instead of just “Sent message”. If the same log statement happened within a filter translator instance one would see “Filter: Sent message”.

This channel has the code to do this and shows how to use it. You can use the same module in your own channels to solve this problem.

Using the code [top]

  • Import the Log Annotate channel from the Builtin: Tools repository
  • Experiment with the code to find out how it works
  • Then add the module(s) to your Translator project
  • Copy the require ‘log.annotate’ statement from the channel and add it at the top of your script
    Note: This module modifies the global namespace

How it works [top]

At startup the module inspects the configuration for the channel and figures out the specific translator instance type by comparing its’ translator ID against the ID’s present in the configuration for the channel.

Then each of the builtin logging functions (like print(), iguana.loginfo(), iguanaLogError() etc.) is then replaced by a new version of each function which gives a prefix indicating the type of translator that is logging.

To see the code in action run the channel and click on the included link to bring up the logs for the channel. Take note of the prefixes on the logs which highlighted with red rectangles:

screen-shot-2016-09-16-at-2-26-09-pm
This is the github code for the From component main module:

More information [top]

  • Source code for the From component main module on github
    Note: You can also look at the (trivial) code for the main module of the Filter and To components
  • Source code for the log/annotate.lua module on github
  • API documentation for iguana.logInfo and print statements.

Leave A Comment?