Skip to content

Instantly share code, notes, and snippets.

@anastasiya29
Created August 21, 2020 01:36
Show Gist options
  • Save anastasiya29/81f6f519d1dccd0ca1b64f03dc32a76b to your computer and use it in GitHub Desktop.
Save anastasiya29/81f6f519d1dccd0ca1b64f03dc32a76b to your computer and use it in GitHub Desktop.
Example of logging to a file in Node
import logger from ‘./logger’;
function foo {
try {
// do something
logger.log('Victory in foo!');
} catch (error) {
logger.error(`Foo failed. Error: $(error)`);
}
}
import fs from 'fs';
import { Console } from 'console';
const filename = (new Date).toISOString().split('T')[0];
const infoLog = filename + '-log.log';
const errorLog = filename + '-error.log';
// test if files exist, and create them if they don't
const output = fs.createWriteStream(infoLog);
const errorOutput = fs.createWriteStream(errorLog);
const logger = new Console({ stdout: output, stderr: errorOutput });
export default logger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment