Repository URL to install this package:
|
Version:
0.0.1-9ecc40928b9b22 ▾
|
#!/usr/bin/env node
const readline = require('readline');
const level = require('./level');
const options = require('./options');
const { layout } = require('./constants');
const genericFormatter = require('./component/background-text');
const severityComp = require('./component/severity');
const messageComp = require('./component/message');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
const printDefaultLayout = (log, normalizedSeverity) => {
const separator = genericFormatter.format('-', normalizedSeverity);
const severity = severityComp.format(normalizedSeverity);
const instant = genericFormatter.format(log.apptime, normalizedSeverity);
const message = messageComp.format(log.message, normalizedSeverity);
const clazz = genericFormatter.format(log.class, normalizedSeverity);
const thread = genericFormatter.format(log.thread, normalizedSeverity);
const exception = log.ex ? genericFormatter.format(log.ex, normalizedSeverity) : null
const pod = genericFormatter.format(log.pod, normalizedSeverity);
const container = genericFormatter.format(log.container, normalizedSeverity);
let prettyLog = log.pod ? `${pod} ${separator} ${container}\n` : ``;
prettyLog = `${prettyLog}${instant} ${separator} ${clazz} ${separator} ${thread}\n${severity} ${message}`;
prettyLog = exception ? `${prettyLog}\n${exception}\n` : `${prettyLog}\n`;
console.log(prettyLog)
}
rl.on('line', function(line) {
let log;
try {
// stern default pattern: {pod-name} {container-name} {log-message}
const matches = line.match(/^([\w\d-_]+) ([\w\d-_]+) (\{.+\})$/);
const pod = matches ? matches[1] : undefined;
const container = matches ? matches[2] : undefined;
const json = matches ? line.replace(pod, '').replace(container,'').trim() : line;
log = JSON.parse(json);
log.pod = pod;
log.container = container;
} catch (e) {
// the SpringBoot banner is not a valid JSON, but we can ignore it here
// console.log(e);
return;
}
const normalizedSeverity = log.severity.trim().toLowerCase();
if (!level.shouldDisplay(normalizedSeverity, options.minimumSeverityToDisplay)) {
return;
}
printDefaultLayout(log, normalizedSeverity);
});