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 || log.loggerName, normalizedSeverity);
  const thread = genericFormatter.format(log.thread || log.threadName, normalizedSeverity);
  const exception = log.ex ? genericFormatter.format(log.ex, normalizedSeverity) : null
  const pod = log.pod ? genericFormatter.format(log.pod, normalizedSeverity) : '';
  const container = log.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)
}

/**
 * Get the log level from a log object
 * @param {Object} logObj
 * @returns {String|Null}
 */
const getLogLevel = (logObj) => {
  const { severity, level } = logObj;

  if (level) {
    return level.trim().toLowerCase();
  }

  if (severity) {
    return severity.trim().toLowerCase();
  }

  return null;
}

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.hostName;
    log.container = container;
    log.apptime = log.apptime || log['@timestamp'];

    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 = getLogLevel(log);

  if (!level.shouldDisplay(normalizedSeverity, options.minimumSeverityToDisplay)) {
    return;
  }

  printDefaultLayout(log, normalizedSeverity);
});