|
@@ -1,6 +1,6 @@
|
|
|
-const chalk = require('chalk')
|
|
|
const escapeStringRegexp = require('escape-string-regexp')
|
|
|
const util = require('node:util')
|
|
|
+const supportsColors = require('supports-color')
|
|
|
|
|
|
const valuesToMask = []
|
|
|
/**
|
|
@@ -38,6 +38,12 @@ exports.setProcessName = (newProcessName) => {
|
|
|
processName = newProcessName
|
|
|
}
|
|
|
|
|
|
+const styleText =
|
|
|
+ typeof util.styleText === "function" && supportsColors.stderr ?
|
|
|
+ util.styleText
|
|
|
+ : (style, text) => text;
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* message log
|
|
|
*
|
|
@@ -46,9 +52,9 @@ exports.setProcessName = (newProcessName) => {
|
|
|
* @param {string} params.tag a unique tag to easily search for this message
|
|
|
* @param {string} params.level error | info | debug
|
|
|
* @param {string} [params.traceId] a unique id to easily trace logs tied to a request
|
|
|
- * @param {Function} [params.color] function to display the log in appropriate color
|
|
|
+ * @param {string[]} [params.color] Format(s) that can be passed to `util.styleText`.
|
|
|
*/
|
|
|
-const log = ({ arg, tag = '', level, traceId = '', color = (message) => message }) => {
|
|
|
+const log = ({ arg, tag = '', level, traceId = '', color = [] }) => {
|
|
|
const time = new Date().toISOString()
|
|
|
const whitespace = tag && traceId ? ' ' : ''
|
|
|
|
|
@@ -66,7 +72,7 @@ const log = ({ arg, tag = '', level, traceId = '', color = (message) => message
|
|
|
const msgString = msgToString()
|
|
|
const masked = maskMessage(msgString)
|
|
|
// eslint-disable-next-line no-console
|
|
|
- console.log(color(`${processName}: ${time} [${level}] ${traceId}${whitespace}${tag}`), color(masked))
|
|
|
+ console.log(styleText(color, `${processName}: ${time} [${level}] ${traceId}${whitespace}${tag}`), styleText(color, masked))
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -88,7 +94,7 @@ exports.info = (msg, tag, traceId) => {
|
|
|
* @param {string} [traceId] a unique id to easily trace logs tied to a request
|
|
|
*/
|
|
|
exports.warn = (msg, tag, traceId) => {
|
|
|
- log({ arg: msg, tag, level: 'warn', traceId, color: chalk.bold.yellow })
|
|
|
+ log({ arg: msg, tag, level: 'warn', traceId, color: ['bold', 'yellow'] })
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -99,7 +105,7 @@ exports.warn = (msg, tag, traceId) => {
|
|
|
* @param {string} [traceId] a unique id to easily trace logs tied to a request
|
|
|
*/
|
|
|
exports.error = (msg, tag, traceId) => {
|
|
|
- log({ arg: msg, tag, level: 'error', traceId, color: chalk.bold.red })
|
|
|
+ log({ arg: msg, tag, level: 'error', traceId, color: ['bold', 'red'] })
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -111,6 +117,6 @@ exports.error = (msg, tag, traceId) => {
|
|
|
*/
|
|
|
exports.debug = (msg, tag, traceId) => {
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
- log({ arg: msg, tag, level: 'debug', traceId, color: chalk.bold.blue })
|
|
|
+ log({ arg: msg, tag, level: 'debug', traceId, color: ['bold', 'blue'] })
|
|
|
}
|
|
|
}
|