9951 explained code solutions for 126 technologies


nodejsHow to log to file


const fs = require('fs');

const logger = new console.Console({
  stdout: fs.createWriteStream('/tmp/out.log'),
  stderr: fs.createWriteStream('/tmp/err.log')
});

logger.log('hi, I am message');
logger.error('I AM ERROR HA HA HA');ctrl + c
require('fs')

library to work with file system

new console.Console

create new Console object to log data

fs.createWriteStream

open file and create writing stream from it

stdout

set stream (file) to write standard messages to

stderr

set stream (file) to write errors to

logger.log

log standard message

logger.error

log error