-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlogging-to-file.js
47 lines (28 loc) · 1.37 KB
/
logging-to-file.js
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
42
43
44
45
46
47
/* ------------------------------------------------------------------------ */
const fs = require ('fs')
const ansi = require ('ansicolor') // that comes with ololog
/* ------------------------------------------------------------------------ */
const log = require ('ololog').configure ({
/* Injects a function after the "render" step */
'render+' (text, { consoleMethod = '' }) {
if (text) {
const strippedText = ansi.strip (text).trim () + '\n' // remove ANSI codes
/* Writes to the file only if .info or .error or .warn has been specified. */
if (consoleMethod) {
fs.appendFileSync ('info.log', strippedText)
/* Writes .error and .warn calls to a separate file */
if ((consoleMethod === 'error') || (consoleMethod === 'warn')) {
fs.appendFileSync ('error.log', strippedText)
}
}
}
return text
}
})
/* ------------------------------------------------------------------------ */
log ("this isn't going to a file!")
log.info ("goes to info.log")
log.warn ("goes to info.log and error.log (a warning)")
log.error ("goes to info.log and error.log (an error)")
log.red.info ("ANSI codes are stripped when writing to a file")
/* ------------------------------------------------------------------------ */