-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateLog.js
29 lines (26 loc) · 911 Bytes
/
createLog.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
import fs from 'fs/promises';
export default async function createLog(directory, content, type = 'text') {
try {
if (type.toLowerCase() === 'json') {
content = JSON.stringify(content, null, ' ');
}
await fs.writeFile(directory + '/' + getFormatedDate(), content);
} catch (error) {
console.error(error);
}
}
function getFormatedDate() {
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth();
if (month.toString().length < 2) month = '0' + month;
let day = date.getDate();
if (day.toString().length < 2) day = '0' + day;
let hours = date.getHours();
if (hours.toString().length < 2) hours = '0' + hours;
let mins = date.getMinutes();
if (mins.toString().length < 2) mins = '0' + mins;
let secs = date.getSeconds();
if (secs.toString().length < 2) secs = '0' + secs;
return `${year}-${month}-${day}-${hours}-${mins}-${secs}`;
}