-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
29 lines (22 loc) · 714 Bytes
/
logger.js
File metadata and controls
29 lines (22 loc) · 714 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
const fs = require('fs')
const moment = require('moment')
const directory = fs.promises.mkdir('/var/log/lennybot', { recursive: true })
/** @type {import('fs').WriteStream} */
let file = null;
/**
*
* @param {'log' | 'error'} type
* @param {string} text
*/
async function append_log(type, text) {
if (file === null) {
await directory
file = fs.createWriteStream(`/var/log/lennybot/${moment().format('YYYY-MM-DD')}.log`, { flags: 'a+' })
}
const line = `[${type === 'log' ? 'info' : type}][${moment().utc().format()}] - ${text}`
console[type](line)
file.write(line)
file.write('\n')
}
exports.log = text => append_log('log', text)
exports.error = text => append_log('error', text)