Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/dlog / app / index.js
Size: Mime:
#!/usr/bin/env node

const readline = require('readline');
const level = require('./level');
const options = require('./options');
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);

    // If include is set, only show messages that match the patterns supplied.
    // For example dlog --include="foo" --include="bar"
    if (options.include) {
      if (!log || !log.message) return;

      const messageContainsKeys = options.include.some(key => log.message.search(key) > -1)
      if (!messageContainsKeys) {
        return;
      }
    }

    log.pod = pod;
    log.container = container;

    if (options.local) {
      log.apptime = new Date(log.apptime).toLocaleString();
    } else if (options.utc) {
      log.apptime = new Date(log.apptime).toUTCString();
    }
  } 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);
});