-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbasic-console-logging.js
More file actions
41 lines (32 loc) · 887 Bytes
/
Copy pathbasic-console-logging.js
File metadata and controls
41 lines (32 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Node.JS Console Module
*
* Save logs to output.log file, but send error logs to console.
*/
const { Console } = require('console');
const { createWriteStream } = require('fs');
const output = createWriteStream('./output.log');
const logger = new Console({
stdout: output,
stderr: process.stdout
})
let count = 0;
const startLogging = new Promise((resolve, reject) => {
// log a message for every half second
const log = setInterval(() => {
logger.log('Count at %s is %d', new Date(), ++count)
}, 500);
// log an error for every 2 seconds
const err = setInterval(() => {
logger.error('Logged error at %s', new Date())
}, 2000);
// stop logging after 10 seconds
const timeout = setTimeout(() => {
clearInterval(log);
clearInterval(err);
resolve(timeout);
}, 10000)
});
startLogging.then(timeout => {
clearTimeout(timeout)
})