-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (74 loc) · 2.18 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
79
80
81
82
83
84
85
86
import readline from "readline";
import TailFile from "@logdna/tail-file";
import colorizer from "json-colorizer";
const QUIET_STRING = ["functions:", " hosting:", "storage:", "pubsub:"];
const quiet = process.argv.indexOf("--quiet");
const prettyOff = process.argv.indexOf("--pretty-off");
const fileIndex = process.argv.indexOf("--file");
if (fileIndex <= -1 || !process.argv[fileIndex + 1]) {
console.error(
"You seem to be missing the --file argument. Please provide a file to tail."
);
process.exit(1);
}
const options = {
pretty: prettyOff <= -1 ? true : false,
colors: { STRING_LITERAL: "white" },
};
async function startTail() {
const tail = new TailFile(process.argv[fileIndex + 1]).on(
"tail_error",
(err) => {
console.error("TailFile had an error!", err);
}
);
try {
await tail.start();
const linesplitter = readline.createInterface({
input: tail,
});
linesplitter.on("line", (line) => {
if (
quiet >= 0 &&
QUIET_STRING.some((str) =>
new RegExp(`(?<=^...)(.*)${str}`, "gm").test(line)
)
)
return;
let newLine = line;
if (newLine.startsWith(">") && newLine.endsWith("}")) {
const overrideOptions = { ...options };
try {
const json = JSON.parse(newLine.slice(3));
switch (json?.severity) {
case "INFO":
overrideOptions.colors.STRING_KEY = "blue";
overrideOptions.colors.BRACE = "blue";
break;
case "WARNING":
overrideOptions.colors.STRING_KEY = "yellow";
overrideOptions.colors.BRACE = "yellow";
break;
case "ERROR":
overrideOptions.colors.STRING_KEY = "red";
overrideOptions.colors.BRACE = "red";
break;
default:
break;
}
newLine = colorizer(newLine.slice(3), overrideOptions);
} catch (err) {
// ignore
}
}
console.log(newLine);
});
} catch (err) {
console.error("Cannot start. Does the file exist?", err);
}
}
startTail().catch((err) => {
process.nextTick(() => {
throw err;
});
});