-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (62 loc) · 1.75 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const colors = require("colors");
const fs = require("fs");
const Tail = require("tail").Tail;
const logsPath =
"C:/Projects/DanskeSpil.Website/develop/Website/App_data/logs/";
let logFile;
let previousLogFile;
function findLatestLogFile() {
fs.readdir(logsPath, function (err, files) {
//handling error
if (err) {
return console.log("Unable to scan directory: " + err);
}
files = files.filter((file) => file.startsWith("log."));
files.sort((fileA, fileB) => {
const statsA = fs.statSync(logsPath + fileA);
const statsB = fs.statSync(logsPath + fileB);
return statsA.mtimeMs - statsB.mtimeMs;
});
logFile = files.pop();
if (logFile !== previousLogFile) {
previousLogFile = logFile;
tailIt(logFile);
}
});
}
const tailOptions = {
separator: /[\r]{0,1}\n/,
fromBeginning: false,
fsWatchOptions: {},
follow: true,
logger: console,
};
function tailIt(file) {
console.log(`\n\n\n LOGGING FROM: ${file} \n\n`.bgBlue.white);
tail = new Tail(`${logsPath}${file}`, tailOptions);
tail.on("line", function (data) {
const now = new Date().toString().slice(16, 24).gray;
if (data.indexOf("INFO") > -1) {
// console.log(`${now} ${data.gray}`)
return;
}
if (data.indexOf("DEBUG") > -1) {
// console.log(`${now} ${data.gray}`)
return;
}
if (data.indexOf("ERROR") > -1) {
console.log(`${now} ${data.bold.bgRed.white}`);
return;
}
if (data.indexOf("WARN") > -1) {
console.log(`${now} ${data.black.bgYellow}`);
return;
}
console.log(`${now} ${data.white}`);
});
tail.on("error", function (error) {
console.log("ERROR: ", error);
});
}
findLatestLogFile();
setInterval(findLatestLogFile, 1000);