-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
113 lines (100 loc) · 2.79 KB
/
app.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import * as dotenv from "dotenv";
import pm2 from "pm2";
import os from "os";
import winston from "winston";
import stripAnsi from "strip-ansi";
import LogstashTransport from "winston-logstash/lib/winston-logstash-latest.js";
import { LogserverTransport } from "winston-logserver-transport";
dotenv.config();
const {
LOGSTASH_HOST,
LOGSTASH_PORT,
SSL_CERT_PATH,
SSL_KEY_PATH,
USE_LOGSERVER,
LOGSERVER_BASEURL,
LOGSERVER_API_KEY,
} = process.env;
const hostname = os.hostname();
const logger = winston.createLogger();
if (USE_LOGSERVER == "yes")
logger.add(
new LogserverTransport({
apiBaseUrl: LOGSERVER_BASEURL,
apiKey: LOGSERVER_API_KEY,
})
);
else {
logger.add(
new LogstashTransport({
port: LOGSTASH_PORT,
host: LOGSTASH_HOST,
ssl_enable: true,
ssl_key: SSL_KEY_PATH,
ssl_cert: SSL_CERT_PATH,
rejectUnauthorized: false, //Does Not Work Without This Apparently
})
);
}
logger.on("warn", (error) => {
console.log("Winston Failed To Send Logs: " + error);
});
logger.on("error", (error) => {
console.log("Winston Failed To Send Logs: " + error);
});
type logType = {
data: string;
at: number;
process: {
namespace: string;
rev: string;
name: string;
pm_id: number;
};
};
pm2.launchBus(function (err, bus) {
if (err) return console.error(err);
bus.on("log:out", function (log: logType) {
if (!["PM2-LOGSTASH", "LOGSTASH-SERVER"].includes(log.process.name)) {
log.data = stripAnsi(log.data); // Removing Ansi Color Codes
//Some times errors are logged in normal output
let errorFlag = log.data.toUpperCase().indexOf("ERROR") != -1;
if (!(log.data.startsWith("/") || log.data.startsWith("GET"))) {
//Skipping GET Call Logs (Remove If Needed)
var message = {
timestamp: new Date(log.at).toISOString(),
version: "1",
service: "PM2",
application: log.process.name,
environment: errorFlag ? "error" : "output",
logLevel: errorFlag ? "error" : "info",
host: hostname,
message: log.data,
};
logger.info(message);
}
}
});
bus.on("log:err", function (log: logType) {
if (!["PM2-LOGSTASH", "LOGSTASH-SERVER"].includes(log.process.name)) {
log.data = stripAnsi(log.data); // Removing Ansi Color Codes
var message = {
timestamp: new Date(log.at).toISOString(),
version: "1",
service: "PM2",
application: log.process.name,
environment: "error",
logLevel: "error",
host: hostname,
message: log.data,
};
logger.error(message);
}
});
bus.on("reconnect attempt", function () {
console.log("PM2: Bus reconnecting");
});
bus.on("close", function () {
pm2.disconnect();
});
});