Created
August 21, 2020 01:36
-
-
Save anastasiya29/81f6f519d1dccd0ca1b64f03dc32a76b to your computer and use it in GitHub Desktop.
Example of logging to a file in Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logger from ‘./logger’; | |
function foo { | |
try { | |
// do something | |
logger.log('Victory in foo!'); | |
} catch (error) { | |
logger.error(`Foo failed. Error: $(error)`); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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