add option for custom papertrail hostname (#4912)

This commit is contained in:
Patrick Quist
2023-03-30 23:44:19 +02:00
committed by GitHub
parent 72ff64767a
commit a08f6957f4
2 changed files with 23 additions and 6 deletions

3
app.ts
View File

@@ -94,6 +94,7 @@ const opts = nopt({
ensureNoIdClash: [Boolean],
logHost: [String],
logPort: [Number],
hostnameForLogging: [String],
suppressConsoleLog: [Boolean],
metricsPort: [Number],
loki: [String],
@@ -172,7 +173,7 @@ const defArgs = {
};
if (opts.logHost && opts.logPort) {
logToPapertrail(opts.logHost, opts.logPort, defArgs.env.join('.'));
logToPapertrail(opts.logHost, opts.logPort, defArgs.env.join('.'), opts.hostnameForLogging);
}
if (opts.loki) {

View File

@@ -55,22 +55,35 @@ export function makeLogStream(level: string, logger_: winston.Logger = logger):
});
}
type MyPapertrailTransportOptions = TransportStreamOptions & {
host: string;
port: number;
identifier: string;
hostnameForLogging?: string;
};
// Our own transport which uses Papertrail under the hood but better adapts it to work in winston 3.0
class MyPapertrailTransport extends TransportStream {
private readonly hostname: string;
private readonly program: string;
public readonly transport: Papertrail;
constructor(opts: TransportStreamOptions & {host: string; port: number; identifier: string}) {
constructor(opts: MyPapertrailTransportOptions) {
super(opts);
this.hostname = os.hostname();
this.program = opts.identifier;
if (opts.hostnameForLogging) {
this.hostname = opts.hostnameForLogging;
} else {
this.hostname = os.hostname();
}
this.transport = new Papertrail({
host: opts.host,
port: opts.port,
logFormat: (level, message) => message,
hostname: this.hostname,
});
}
@@ -102,12 +115,15 @@ export function logToLoki(url) {
logger.info('Configured loki');
}
export function logToPapertrail(host: string, port: number, identifier: string) {
const transport = new MyPapertrailTransport({
export function logToPapertrail(host: string, port: number, identifier: string, hostnameForLogging?: string) {
const settings: MyPapertrailTransportOptions = {
host: host,
port: port,
identifier: identifier,
});
hostnameForLogging,
};
const transport = new MyPapertrailTransport(settings);
transport.transport.on('error', err => {
logger.error(err);
});