-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
PapertrailLogger.js
64 lines (57 loc) · 2.06 KB
/
PapertrailLogger.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
const os = require("os");
const winston = require("winston");
require("winston-syslog");
// Create a new syslog transport with the configured Papertrial log destination.
const PAPERTRAIL_SYSTEM_TRANSPORT = new winston.transports.Syslog({
host: process.env.host,
port: process.env.port,
localhost: process.env.hostname || os.hostname(),
app_name: process.env.module_name || "pm2-papertrail-logger",
protocol: "tls4",
eol: "\n"
});
// The winson logger to use for forwarding logs.
const PAPERTRAIL_LOGGER = winston.createLogger({
format: winston.format.printf(({message}) => { return message }),
levels: winston.config.syslog.levels, // Use all syslog procotol levels.
transports: [ PAPERTRAIL_SYSTEM_TRANSPORT ],
});
const PROCESS_AS_SYSTEM = (process.env?.["process-as-systems"]?.toLowerCase() === "true");
class PapertrailLogger
{
/**
* PapertrailLogger(systemName, programName)
* Creates a new Papertrail logger instance for a process.
*
* @param {String} systemName The name of the system to log as.
* @param {String} programName The program name to log as.
*/
constructor(systemName, programName)
{
this.name = systemName;
this.appName = programName;
this.process = (PROCESS_AS_SYSTEM) ? systemName : programName; // The name of the process for internal logging.
}
/**
* log(level, message)
* Dispatches a log via transport pipeline to Papertrail.
*
* @param {String} level The log level to use.
* @param {String} message The message to log.
*/
log(level, message)
{
try {
if (!PAPERTRAIL_SYSTEM_TRANSPORT || !PAPERTRAIL_LOGGER)
return console.error(`Discarding log from '${this.process}' as Papertrail transport is not ready.`);
// Update the transport to use details of this process.
PAPERTRAIL_SYSTEM_TRANSPORT.localhost = this.name;
PAPERTRAIL_SYSTEM_TRANSPORT.appName = this.appName;
PAPERTRAIL_LOGGER[level](message);
} catch (error) {
console.error(`[ERROR] Failed to log message to Papertrail for process '${this.process}' with level ${level}:`);
console.error(error);
}
}
}
module.exports = PapertrailLogger;