All files / app pretty-print.js

96.88% Statements 31/32
91.67% Branches 11/12
100% Functions 3/3
96.88% Lines 31/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115          11x 11x   11x   11x     11x                                                                                     11x   46x   46x 46x 46x   46x     46x 46x 46x 46x   46x   46x                   11x 29x 29x 29x     29x 87x   29x     29x         29x 4x       29x 25x     4x   4x    
/* eslint-disable security/detect-object-injection */
/**
 * @fileoverview Handles pretty printing for logality, used in
 *  local development.
 */
const chalk = require('chalk');
const format = require('json-format');
 
const { isObjectEmpty } = require('./utils');
 
const pretty = (module.exports = {});
 
/** @const {Object} LEVELS_CONFIG Levels colors and icons */
const LEVELS_CONFIG = {
  emergency: {
    color: chalk.red.underline,
    icon: '●',
  },
  alert: {
    color: chalk.red.underline,
    icon: '◆',
  },
  critical: {
    color: chalk.red,
    icon: '✖',
  },
  error: {
    color: chalk.red,
    icon: '■',
  },
  warn: {
    color: chalk.yellow,
    icon: '⚠',
  },
  notice: {
    color: chalk.cyan,
    icon: '▶',
  },
  info: {
    color: chalk.blue,
    icon: 'ℹ',
  },
  debug: {
    color: chalk.green,
    icon: '★',
  },
};
 
/**
 * Write prettified log to selected output.
 *
 * @param {Object} logContext The log context to write.
 * @param {boolean|Object} prettyOpts Possible pretty print options.
 * @return {string} Formatted output.
 * @private
 */
pretty.writePretty = function (logContext, prettyOpts) {
  // current level icon and color
  const config = LEVELS_CONFIG[logContext.level];
 
  const noTimestamp = !!prettyOpts?.noTimestamp;
  const noFilename = !!prettyOpts?.noFilename;
  const onlyMessage = !!prettyOpts?.onlyMessage;
 
  const file = noFilename
    ? ''
    : ` ${chalk.underline.green(logContext.context.source.file_name)}`;
  const date = noTimestamp ? '' : chalk.white(`[${logContext.dt}] `);
  const level = config.color(`${config.icon} ${logContext.level}`);
  const message = config.color(logContext.message);
  const logs = onlyMessage ? '' : pretty._getLogs(logContext);
 
  const output = `${date}${level}${file} - ${message}\n${logs}`;
 
  return output;
};
 
/**
 * Returns formatted logs for pretty print.
 *
 * @param {Object} logContext The log context to format.
 * @return {string} Log output.
 * @private
 */
pretty._getLogs = function (logContext) {
  const logs = {};
  const blacklist = ['runtime', 'source', 'system'];
  const { event, context } = logContext;
 
  // remove unnecessary keys
  blacklist.forEach((key) => {
    delete context[key];
  });
  delete event.http_request;
 
  // set event if exists
  Iif (!isObjectEmpty(event)) {
    logs.event = event;
  }
 
  // set context
  if (!isObjectEmpty(context)) {
    logs.context = context;
  }
 
  // empty string if the logs are emtpy
  if (isObjectEmpty(logs)) {
    return '';
  }
 
  const prettyLogs = format(logs, { type: 'space', size: 2 });
 
  return `${prettyLogs}\n`;
};