UNPKG

pino-pretty

Version:
67 lines (59 loc) 1.88 kB
'use strict' const { LEVELS, LEVEL_NAMES } = require('./constants') const nocolor = input => input const plain = { default: nocolor, 60: nocolor, 50: nocolor, 40: nocolor, 30: nocolor, 20: nocolor, 10: nocolor, message: nocolor } const chalk = require('chalk') const ctx = new chalk.Instance({ level: 3 }) const colored = { default: ctx.white, 60: ctx.bgRed, 50: ctx.red, 40: ctx.yellow, 30: ctx.green, 20: ctx.blue, 10: ctx.grey, message: ctx.cyan } function colorizeLevel (level, colorizer) { if (Number.isInteger(+level)) { return Object.prototype.hasOwnProperty.call(LEVELS, level) ? colorizer[level](LEVELS[level]) : colorizer.default(LEVELS.default) } const levelNum = LEVEL_NAMES[level.toLowerCase()] || 'default' return colorizer[levelNum](LEVELS[levelNum]) } function plainColorizer (level) { return colorizeLevel(level, plain) } plainColorizer.message = plain.message function coloredColorizer (level) { return colorizeLevel(level, colored) } coloredColorizer.message = colored.message /** * Factory function get a function to colorized levels. The returned function * also includes a `.message(str)` method to colorize strings. * * @param {bool} [useColors=false] When `true` a function that applies standard * terminal colors is returned. * * @returns {function} `function (level) {}` has a `.message(str)` method to * apply colorization to a string. The core function accepts either an integer * `level` or a `string` level. The integer level will map to a known level * string or to `USERLVL` if not known. The string `level` will map to the same * colors as the integer `level` and will also default to `USERLVL` if the given * string is not a recognized level name. */ module.exports = function getColorizer (useColors = false) { return useColors ? coloredColorizer : plainColorizer }