The last few days I’ve been exploring the CLI and doing a bit of NodeJS code at the same time. Today, I want to continue on that theme and fulfill a real scenario that my friend and colleague Mike Taulty asked about a few months ago. Mike asked (paraphrased): “It would be cool if there was some tool that would receive or poll for logs so, during development, I can just refer to my log watcher window to see that latest logs. Is there a way I can do that?”. Today, we’re going to use the new CLI and node to build this.
First, you’ll need node installed and ready to go. You’ll need this in the coming days of this series so go get it now. I have detailed instructions in this post: Getting started with the CLI and backing up your scripts (Day 4)
Once again, we’re going to use NodeJS to write a small console program that will invoke the CLI to poll for logs for a given service, and only show new logs since the last poll. One of the great things about using NodeJS for this is that it will work on both Windows and Mac.
This is the node script that I saved into a log.js file:
The script is pretty simple if you’re familiar with the script that we created in yesterdays post: More CLI – changing your Mobile Services workflow (Day 5)
The main function is the executeJsonCmd which invokes an Azure Mobile CLI command and adds the --json switch to make the CLI return JSON. We then parse that into JSON that we can process inside Node. The rest of the code is mostly formatting and a little ‘polling….’ animation so you can see when we’re invoking the CLI. The command we use to get the logs for your service is
azure mobile log <servicename>
And, as you might expect, to start our little node app running you just need to copy this in a js file (called log.js here) and invoke
node log.js <servicename>
From your node.js command prompt (or Terminal in Mac). The LogWatcher will then poll for logs every 10 seconds, but you can tune this to your pleasure by adjusting the pollingInterval variable at the top of the script (heeding the warning in the comments). Here’s some example output:
This day 6 of the twelve days of ZUMO. Tune back tomorrow for more!
(and Happy Boxing Day to Mike and my UK readers!)