UNPKG

log-to-file

Version:

Log text to file (with timestamp)

37 lines (32 loc) 1.31 kB
// Import. const log = require('./app'); const fs = require('fs'); // Remove log files. fs.unlink('default.log', () => { }); fs.unlink('my-log.log', () => { }); // Log data. (async () => { log('Some data to default log file "default.log".'); await new Promise(resolve => setTimeout(resolve, 100)); log('Another data to custom log file "my-log.log".', 'my-log.log'); await new Promise(resolve => setTimeout(resolve, 100)); log('Another data to custom log file "my-log.log" with delimiter "\\r\\n".', 'my-log.log', '\r\n'); await new Promise(resolve => setTimeout(resolve, 100)); log('Another data to custom log file "my-log.log" with delimiter "\\n-----\\n".', 'my-log.log', '\n-----\n'); await new Promise(resolve => setTimeout(resolve, 100)); log('Another data to custom log file "my-log.log".', 'my-log.log'); })(); // Wait 0,5 second and check default log file. setTimeout(function () { fs.readFile('default.log', function (error, data) { if (error) console.log('Error:\n' + error); console.log('Default log file:\n' + data); }); }, 500); // Wait 1 second and check custom log file. setTimeout(function () { fs.readFile('my-log.log', function (error, data) { if (error) console.log('Error:\n' + error); console.log('Custom log file:\n' + data); }); }, 1000);