This post assumes you have a Red Hat Mobile Platform instance. You can sign up for a community edition instance at using your OpenShift account.

Not so Pretty Logs

Just yesterday I blogged about the fh-bunyan module, and how it makes sensible logging decisions on your behalf when your node.js code is deployed on Red Hat Mobile Application Platform. One of the interesting things about the bunyan module is that it prints logs in JSON format. Logging in this manner is extremely valuable since it means you can stream logs to multiple stores and persist them in a structured manner that can be queried easily. Unfortunately this also makes reading these logs a little tough! Just look at the screenshot and sample below - it’s not what I would consider user friendly.

{"name":"/application.js","hostname":"ps-consult-na-rht-dev","pid":9736,"level":30,"msg":"App started at: Tue May 24 2016 20:22:03 GMT+0000 (UTC) on port: 8184","time":"2016-05-24T20:22:03.763Z","v":0}

fh-bunyan logs

Not so Pretty Logs

To get these logs to print in a friendly format we have numerous options, but I consider using the fhc CLI to be best solution. Using the fhc app logs command allows users to:

  • Delete log files
  • View log files
  • Tail logs in realtime

Tailing logs is what we’re after for now, so try the following:

# You might need sudo to global install - using nvm can get around this
npm i -g fh-fhc@latest-2

# Target your RHMAP instance
fhc target

# Login with username and password - you will be prompted for them
fhc login

# Tail logs from the set $CLOUD_APP_ID
fhc app logs tail --app=$CLOUD_APP_ID --env=$CLOUD_APP_ENV

This will print your logs to the terminal, but they’re still not formatted in a friendly manner since fhc simply streams them “as is”.

fhc fh-bunyan logs

The solution? Use the bunyan CLI to pretty-print them! Run the following command and marvel at the beauty of your logs:

# You might need sudo to global install - using nvm can get around this
npm i -g bunyan
fhc app logs tail --app=$CLOUD_APP_ID --env=$CLOUD_APP_ENV | bunyan

fhc fh-bunyan pretty logs

This solution becomes even more powerful if you need to find specific logs since you can pipe to grep and perform a realtime filter like so.

# View all delete calls
fhc app logs tail --app=$CLOUD_APP_ID --env=$CLOUD_APP_ENV | bunyan | grep -i "delete"

fhc fh-bunyan pretty logs