guigrpa/storyboard

Is it possible to overload console?

TotallyInformation opened this issue · 6 comments

I have a complex application (Node-Red) that logs to stdout using console and I would love to be able to redirect that output to storyboard since it runs on a raspberry pi that is tucked away somewhere remote. It would be much easier to debug things and monitor log output if I could use storyboard to do it. But, I obviously cannot dig into the Node-Red code and change all of the console outputs.

In Linux/Raspbian, you can pipe the output of your program to a simple logger.js script as follows:

$ ./myProgram |& node logger

(using |& to connect both stdout and stderr to the logger). And in logger.js:

var split = require('split');
var mainStory = require('storyboard').mainStory;

process.stdin.pipe(split())
  .on('data', function(line) { mainStory.info(line); })
  .on('end', function() { process.exit(0); });

I'm using here the split package to read stdin line by line and pass it to Storyboard's mainStory for logging. You may want to parse the input line to determine its level (whether it should be written as debug, info, warn, error, etc.) and source, which would be useful for filtering.

Example output:

$ ls |& node logger
2016-04-22T05:43:23.238Z           storyboard INFO  ┌── ROOT STORY: Node.js 5.4.1 on Linux 64-bit [CREATED]
2016-04-22T05:43:23.249Z                 main INFO  logger.js
2016-04-22T05:43:23.250Z                 main INFO  node_modules
2016-04-22T05:43:23.250Z                 main INFO  package.json
2016-04-22T05:43:23.250Z                 main INFO  src
2016-04-22T05:43:23.250Z                 main INFO  test
2016-04-22T05:43:23.251Z                 main INFO
2016-04-22T05:43:23.252Z           storyboard INFO  └── ROOT STORY: Node.js 5.4.1 on Linux 64-bit [CLOSED]
$ missingCommand |& node logger
2016-04-22T05:43:37.035Z           storyboard INFO  ┌── ROOT STORY: Node.js 5.4.1 on Linux 64-bit [CREATED]
2016-04-22T05:43:37.046Z                 main INFO  missingCommand: command not found
2016-04-22T05:43:37.048Z                 main INFO
2016-04-22T05:43:37.048Z           storyboard INFO  └── ROOT STORY: Node.js 5.4.1 on Linux 64-bit [CLOSED]

Please tell me if it works for you!

Closing this now. Please reopen if the solution is not sufficient

Included the logger tool in the library, under lib/stdinLogger.js (src: src/stdinLogger.coffee).

Just FYI, Storyboard version 2.0 includes a revamped CLI tool that handles not only stdout, but also stderr and stdin. In fact, you can even wrap a whole shell like this: sb --server sh.

Thanks Guillermo, I'll check it out.