In node.js vm module, i can execute some javascript in another node.js process. What i want to achieve is being able to catch logs performed in the script runned by the vm.

For example i want to catch the "foobar" log:

var vm = require('vm')

vm.runInThisContext('console.log("foobar");', 'myfile.vm');

// how can i get the "foobar" log?

Thanks in advance

EDIT: The answer below works well, although there's a shorter version:

function captureStdout(callback) {
    var output = '', old_write = process.stdout.write

    // start capture
    process.stdout.write = function(str, encoding, fd) {
        output += str
    }

    callback()

    // end capture
    process.stdout.write = old_write

    return output
}
Edit
Report