How do I use console.table but to stderr? - node.js

I find that console.table() is good, in a few cases, for error messages. However, it doesn't print to stderr. How do I print console.table to stderr in Node.js?

In Node.js, one can instantiate a custom Console instance with a different writable stream than stdout using the console module, e.g.:
const { Console } = require("console");
const console = new Console(process.stderr);
This would have any method that typically uses stdout, including table(), use stderr instead.

Related

What does process stdout connect do?

What does
process.stdout.connect({})
do and how can I use it?
Tryied googleing it but found nothing.
Tryied options but get error:
TypeError: self._handle.connect is not a function
As the documentation says, process.stdout returns a stream connected to stdout and that stream is of type Socket. Now, if you check the documentation for Socket and look for the connect method you'll find that it initiates a connection on a given socket.
In order to use process.stdout you don't actually need to call connect, you can just write arbitrary strings to the stream which will get echoed in your stdout, e.g.
process.stdout.write("test"); // will print test to the console
But if logging to the console is your intent, it's probably easier to just use the provided console.log() which adds a lot of formatting and other stuff (see this for further info).

How can I log my errors into a txt file AND the console in nodejs?

How can I log my errors into a txt file AND the console in nodejs?
I want to be able to keep all my errors in a text file.
It depends on what framework you want to use.
You can use Winston, which has the support for Transports - enabling you to create different outputs (file, tcp, stdout etc.) for your logs (based on level and other rules)
Use morgan
const fs = require('fs');
const morgan = require('morgan');
const logs = fs.createWriteStream(
path.join(__dirname, 'logs.log'),
{ flags: 'a' }
);
app.use(morgan('combined', { stream: logs }));
In terms of pure Node.js functionality that works irrespective of frameworks or libraries, console.log() writes data to process.stdout, which is a stream that you can listen to. You can attach a listener function for the data event, and write console.log both to "the console" as well as to file that way.
Alternatively, you can pipe process.stdout into a file, but that's far more tricky to get right.
Also note that if you also yo log error output as well, you'll need to listen for data on process.stderr in addition to process.stdout.

Output being written to process.stdout twice

I want to pass a message from a nodejs script to chrome extension using nativeMessaging and thus am using the chrome-native-messaging node module in the following way to pass a message to stdout.
var rs = new stream.Transform({objectMode: true});
var msg = {"message":"pingfromelectron"};
rs.push(msg);
rs.pipe(output).pipe(process.stdout);
However, this is writing the output twice to stdout as you can see in the screenshot below:
I am not quite sure why this is happening. Any pointers would be helpful.

How to redirect stdout in real time from child process

I have a long running child process that has
{stdio: 'inherit'}
So, I can see whatever it's doing in the console.
How can I redirect this information so it'll show up on my node-webkit app?
Simply handle data events from the STDOUT stream in some way.
childProc.stdout.on('data', function (chunk) {
// Write chunk to the page using whatever normal method you like
});

How do I get a basic setup of piping stdout to a file in Nodejs?

This seems basic enough, but I can't seem to get it to work.
var access = require( 'fs' ).createWriteStream( 'logs/test.access.log', { flags : 'a' } );
process.stdout.pipe( access );
I'm assuming that when I use console.log() after doing this, wouldn't that message be written to test.access.log? I don't get any errors, but I simply don't get anything in the file as well, so I'm wondering if someone can help me understand streams and writing from stdout to a log file like I have above.
Thanks!
Most likely, your Node.js app is closing before the stream's buffer is flushed to disk. This is probably because you're piping the process's own stdout into a file, and stdout normally doesn't close and flush until after Node.js starts to clean up the process.
Try sticking access.destroySoon(); into the end of your code and see if that causes it to flush the data to disk before ending the Javascript event loop.

Resources