11436 SSO

Tutorial: Adding a Logger Plugin to Apigee Edge Microgateway

wwitman
Feb 01, 2016

In this latest tutorial on developing Apigee Edge Microgateway plugins, we'll add a third-party logging module to Edge Microgateway.

Edge Microgateway includes a built-in logging feature; however, if this built-in logger doesn't fit with your specific logging requirements, you can use plugins to configure and use a third-party logger.

This week, we'll complement the built-in Edge Microgateway logging feature with the bunyan JSON logger. Bunyan is a Node.js module available through NPM.

Note: We assume you've read our previous blog on Microgateway plugins, so we’ll skip most of the background details about plugins presented there. We also assume you're familiar with Edge Microgateway, have configured and run an instance of Edge Microgateway, and have built and tried the "hello world" plugin. If not, you can find getting started docs on the Apigee Edge documentation site.

Step 1: Set up a new plugin project

First, we'll create a new plugin project in the <microgateway-root-dir>/plugins folder. These are boilerplate Node.js steps for creating a project:

  • Change directory to <microgateway-root-dir>/plugins.
  • Create a directory for the plugin: mkdir bunyan-logger
  • Create a new index.js file: touch index.js 
  • Install bunyan with NPM: npm install bunyan --save

Now that the plugin project is set up, you're ready to code the plugin.

Step 2: Implement the plugin code

In index.js, require the bunyan NPM module and create a Logger instance in the plugin's init() function. In this example, we configure bunyan streams to send error output to stdout and info output to a log file. Then, we output log information from each of the plugin's event handlers.

  • Open <microgateway-root-dir>/plugins/index.js.
  • Add the following code to the file. The best practice is to place the logger's configuration code in the plugin's init() function.


var bunyan = require('bunyan');
module.exports.init = function() {
 var log = bunyan.createLogger({
   name: "bunyan-logger-plugin",
   streams: [
     {
       level: 'error',
       stream: process.stdout            
     },
     {
       level: 'info',
       path: '/var/tmp/edgemicrogateway-bunyan-info.log'  
     }
   ]
 });
 return {
   onrequest: function(req, res, next) {
     log.info('ONREQUEST');
     next();
   },
   onend_request: function(req, res, data, next) {
     log.info("ONEND_REQUEST");
     next(null, data);
   },
   ondata_response: function(req, res, data, next) {
     log.info("ONDATA_RESPONSE");
     next(null, data);
   },
   onend_response: function(req, res, data, next) {
     log.info("ONEND_RESPONSE");
     next(null, data);
   }
 };
}
  • Save the file.

Step 3: Configure the Edge Microgateway to use the plugin

We now need to add the plugin to the Edge Microgateway agent's config file. We'll also change the log level for the built-in logger to prevent the built-in logger from logging info messages.

  • Change directory to <microgateway-root-dir>/agent/config.
  • Open default.yaml in an editor.
  • Change the log level for the built-in logger to warn. This step simply prevents any info messages from being logged by the default logger. We're going to log info messages with our third-party logger.

logging:
   level: warn
   dir: /var/tmp   
   stats_log_interval: 60
  • Add the bunyan-logger plugin to the plugins sequence:

...
plugins:
   dir: ../plugins
   sequence:
     - oauth
     - bunyan-logger
...
  • Save the config file.

Step 4: Try it

Okay, now the plugin is ready to go.

  • Restart the agent.
  • Restart Edge Microgateway
  • Call an API through Edge Microgateway (like the weather API used in the getting started docs). For example:

curl http://localhost:8000/weather/forecastrss?w=12797282
  • The plugin writes log messages to the file specified in the configuration:

/var/tmp/edgemicrogateway-bunyan-info.log. 

Here's a sample of the log file output. The output fields are described in the bunyan documentation.


[  
  {  
     "name":"bunyan-logger-plugin",
     "hostname":"myhost",
     "pid":57824,
     "level":30,
     "msg":"ONREQUEST",
     "time":"2016-01-12T22:05:28.577Z",
     "v":0
  },
  {  
     "name":"bunyan-logger-plugin",
     "hostname":"myhost",
     "pid":57824,
     "level":30,
     "msg":"ONEND_REQUEST",
     "time":"2016-01-12T22:05:28.600Z",
     "v":0
  },
  {  
     "name":"bunyan-logger-plugin",
     "hostname":"myhost",
     "pid":57824,
     "level":30,
     "msg":"ONDATA_RESPONSE",
     "time":"2016-01-12T22:05:28.814Z",
     "v":0
  },
  {  
     "name":"bunyan-logger-plugin",
     "hostname":"myhost",
     "pid":57824,
     "level":30,
     "msg":"ONEND_RESPONSE",
     "time":"2016-01-12T22:05:28.815Z",
     "v":0
  }
]
 
For more information about Edge Microgateway, go to the Apigee docs site. You can also ask questions on the Apigee Community. For a deep dive into Edge Microgateway, check out this webcast.
 

Image: Lake George Mirror

Microservices Done Right

Next Steps

 
 

Resources Gallery

News