2018-08-28 10:40:50 +00:00
|
|
|
// Code originally derived from https://github.com/hadfieldn/node-bunyan-prettystream but since heavily edited
|
2017-06-22 07:03:36 +00:00
|
|
|
// Neither fork nor original repo appear to be maintained
|
|
|
|
|
2019-07-15 09:04:05 +00:00
|
|
|
import { Stream } from 'stream';
|
2020-05-01 16:03:48 +00:00
|
|
|
import * as util from 'util';
|
2019-07-15 09:04:05 +00:00
|
|
|
import chalk from 'chalk';
|
|
|
|
import stringify from 'json-stringify-pretty-compact';
|
2021-03-02 20:44:55 +00:00
|
|
|
import type { BunyanRecord } from './utils';
|
2017-06-22 07:03:36 +00:00
|
|
|
|
2017-07-19 06:41:09 +00:00
|
|
|
const bunyanFields = [
|
|
|
|
'name',
|
|
|
|
'hostname',
|
|
|
|
'pid',
|
|
|
|
'level',
|
|
|
|
'v',
|
|
|
|
'time',
|
|
|
|
'msg',
|
|
|
|
'start_time',
|
|
|
|
];
|
2017-07-19 06:05:26 +00:00
|
|
|
const metaFields = [
|
|
|
|
'repository',
|
|
|
|
'packageFile',
|
|
|
|
'depType',
|
|
|
|
'dependency',
|
2017-08-26 14:10:18 +00:00
|
|
|
'dependencies',
|
2017-07-19 06:05:26 +00:00
|
|
|
'branch',
|
|
|
|
];
|
|
|
|
|
2019-07-15 09:04:05 +00:00
|
|
|
const levels: Record<number, string> = {
|
2017-06-22 07:03:36 +00:00
|
|
|
10: chalk.gray('TRACE'),
|
|
|
|
20: chalk.blue('DEBUG'),
|
|
|
|
30: chalk.green(' INFO'),
|
|
|
|
40: chalk.magenta(' WARN'),
|
|
|
|
50: chalk.red('ERROR'),
|
|
|
|
60: chalk.bgRed('FATAL'),
|
|
|
|
};
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
export function indent(str: string, leading = false): string {
|
2017-06-22 07:03:36 +00:00
|
|
|
const prefix = leading ? ' ' : '';
|
|
|
|
return prefix + str.split(/\r?\n/).join('\n ');
|
|
|
|
}
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
export function getMeta(rec: BunyanRecord): string {
|
2017-06-22 07:03:36 +00:00
|
|
|
if (!rec) {
|
|
|
|
return '';
|
|
|
|
}
|
2018-08-28 10:40:50 +00:00
|
|
|
let res = rec.module ? ` [${rec.module}]` : ``;
|
2020-04-12 16:09:36 +00:00
|
|
|
const filteredMeta = metaFields.filter((elem) => rec[elem]);
|
2017-07-19 06:05:26 +00:00
|
|
|
if (!filteredMeta.length) {
|
2018-08-28 10:40:50 +00:00
|
|
|
return res;
|
2017-06-22 07:03:36 +00:00
|
|
|
}
|
2017-07-19 06:05:26 +00:00
|
|
|
const metaStr = filteredMeta
|
2020-08-27 07:12:37 +00:00
|
|
|
.map((field) => `${field}=${String(rec[field])}`)
|
2017-07-19 06:05:26 +00:00
|
|
|
.join(', ');
|
2018-08-28 10:40:50 +00:00
|
|
|
res = ` (${metaStr})${res}`;
|
|
|
|
return chalk.gray(res);
|
2017-06-22 07:03:36 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
export function getDetails(rec: BunyanRecord): string {
|
2017-07-19 06:05:26 +00:00
|
|
|
if (!rec) {
|
|
|
|
return '';
|
|
|
|
}
|
2017-08-03 06:01:20 +00:00
|
|
|
const recFiltered = { ...rec };
|
2018-08-28 10:40:50 +00:00
|
|
|
delete recFiltered.module;
|
2020-04-12 16:09:36 +00:00
|
|
|
Object.keys(recFiltered).forEach((key) => {
|
2020-02-24 07:43:01 +00:00
|
|
|
if (
|
|
|
|
key === 'logContext' ||
|
|
|
|
bunyanFields.includes(key) ||
|
|
|
|
metaFields.includes(key)
|
|
|
|
) {
|
2017-07-19 06:05:26 +00:00
|
|
|
delete recFiltered[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const remainingKeys = Object.keys(recFiltered);
|
|
|
|
if (remainingKeys.length === 0) {
|
2017-06-22 07:03:36 +00:00
|
|
|
return '';
|
|
|
|
}
|
2017-07-19 06:05:26 +00:00
|
|
|
return `${remainingKeys
|
2020-04-12 16:09:36 +00:00
|
|
|
.map((key) => `${indent(`"${key}": ${stringify(recFiltered[key])}`, true)}`)
|
2017-07-19 06:05:26 +00:00
|
|
|
.join(',\n')}\n`;
|
2017-06-22 07:03:36 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 15:13:07 +00:00
|
|
|
export function formatRecord(rec: BunyanRecord): string {
|
2017-06-22 07:03:36 +00:00
|
|
|
const level = levels[rec.level];
|
|
|
|
const msg = `${indent(rec.msg)}`;
|
|
|
|
const meta = getMeta(rec);
|
|
|
|
const details = getDetails(rec);
|
|
|
|
return util.format('%s: %s%s\n%s', level, msg, meta, details);
|
|
|
|
}
|
|
|
|
|
2019-07-15 09:04:05 +00:00
|
|
|
export class RenovateStream extends Stream {
|
|
|
|
readable: boolean;
|
2017-06-22 07:03:36 +00:00
|
|
|
|
2019-07-15 09:04:05 +00:00
|
|
|
writable: boolean;
|
2017-06-22 07:03:36 +00:00
|
|
|
|
2019-07-15 09:04:05 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.readable = true;
|
|
|
|
this.writable = true;
|
|
|
|
}
|
2017-06-22 07:03:36 +00:00
|
|
|
|
2019-07-15 09:04:05 +00:00
|
|
|
// istanbul ignore next
|
2019-11-26 15:13:07 +00:00
|
|
|
write(data: BunyanRecord): boolean {
|
2019-07-15 09:04:05 +00:00
|
|
|
this.emit('data', formatRecord(data));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|