darrin
darrin

Reputation: 839

Using node.js Bunyan Logging with Intellij

I'm using bunyan logging for a node.js subsystem and typically use intellij IDEA. I would like to use bunyan's pretty printer so that I can get the benefits of buynan/json logs within intellij.

I cannot find any way to do this but feel it must be possible. Has anyone figured out how to do this?

UPDATE:

@cyue's answer below works like a champ. When I didn't find something right away I ended up creating something like this in a logging class that I use to wrap this functionality and use it for test servers:

var bunyan = require('bunyan');
var bunyanFormat = require('bunyan-format');
var standardOut = bunyanFormat({outputMode: 'long', levelInString: true});
var errorOut = bunyanFormat({outputMode: 'bunyan', levelInString: true});
var bunyanLogger = bunyan.createLogger({
    name: 'tot',
    streams: [
        {
            stream: process.stdout ,
            level: 'trace'
        },
        {
            stream: process.stderr ,
            level: 'warn'
        }
    ],
    serializers: {
        req: bunyan.stdSerializers.req,
        res: bunyan.stdSerializers.res,
        err: bunyan.stdSerializers.err,
        error: bunyan.stdSerializers.err
    }
});
eventLogger.on('log', function (log) {
    if (resolveLevel(log.level) >= minimumLogLevel) {
        bunyanLogger[log.level].apply(bunyanLogger, log.arguments);
    }
});

The above shouldn't be run in production code but thought it might be useful to some...

Upvotes: 4

Views: 1509

Answers (2)

Compl Yue
Compl Yue

Reputation: 174

I managed for it in WebStorm, write a shell script (mine named node2bunyan) with this:

#!/usr/bin/env bash

node $@ 2>&1 | bunyan --color
exit ${PIPESTATUS[0]}

And add it as a Node interpreter for the Run Configuration: enter image description here

Then you can get: enter image description here

PS. I'm stuck to get the same for PyCharm and reached your question when searching SO, my question for PyCharm

Upvotes: 7

JBCP
JBCP

Reputation: 13485

You can use this library in your JS project:

https://github.com/mrrama/node-bunyan-prettystream

You configure it like this:

var bunyan = require('bunyan');
var PrettyStream = require('bunyan-prettystream');
var prettyStream = new PrettyStream();

prettyStream.pipe(process.stdout);

// Note this is an object with key 'stream' inside array.
// The object is the stream definition as far as Bunyan is concerned.
var streams = [{
   stream: prettyStream
}];

bunyan.createLogger({
   streams: streams,
   serializers: bunyan.stdSerializers
});

Upvotes: 2

Related Questions