|
@@ -1,6 +1,7 @@
|
|
|
const chalk = require('chalk')
|
|
|
const escapeStringRegexp = require('escape-string-regexp')
|
|
|
const util = require('util')
|
|
|
+const { ProviderApiError, ProviderAuthError } = require('./provider/error')
|
|
|
|
|
|
const valuesToMask = []
|
|
|
/**
|
|
@@ -35,37 +36,30 @@ function maskMessage (msg) {
|
|
|
/**
|
|
|
* message log
|
|
|
*
|
|
|
- * @param {string | Error} msg the message to log
|
|
|
+ * @param {string | Error} arg the message or error to log
|
|
|
* @param {string} tag a unique tag to easily search for this message
|
|
|
* @param {string} level error | info | debug
|
|
|
* @param {string=} id a unique id to easily trace logs tied to a request
|
|
|
* @param {Function=} color function to display the log in appropriate color
|
|
|
- * @param {boolean=} shouldLogStackTrace when set to true, errors will be logged with their stack trace
|
|
|
*/
|
|
|
-const log = (msg, tag = '', level, id = '', color = (message) => message, shouldLogStackTrace) => {
|
|
|
+const log = (arg, tag = '', level, id = '', color = (message) => message) => {
|
|
|
const time = new Date().toISOString()
|
|
|
const whitespace = tag && id ? ' ' : ''
|
|
|
|
|
|
- function logMsg (msg2) {
|
|
|
- let msgString = typeof msg2 === 'string' ? msg2 : util.inspect(msg2)
|
|
|
- msgString = maskMessage(msgString)
|
|
|
- // eslint-disable-next-line no-console
|
|
|
- console.log(color(`companion: ${time} [${level}] ${id}${whitespace}${tag}`), color(msgString))
|
|
|
- }
|
|
|
-
|
|
|
- if (msg instanceof Error) {
|
|
|
- // Not sure why it only logs the stack without the message, but this is how the code was originally
|
|
|
- if (shouldLogStackTrace && typeof msg.stack === 'string') {
|
|
|
- logMsg(msg.stack)
|
|
|
- return
|
|
|
+ function msgToString () {
|
|
|
+ // We don't need to log stack trace on special errors that we ourselves have produced
|
|
|
+ // (to reduce log noise)
|
|
|
+ if ((arg instanceof ProviderApiError || arg instanceof ProviderAuthError) && typeof arg.message === 'string') {
|
|
|
+ return arg.message
|
|
|
}
|
|
|
-
|
|
|
- // We don't want to log stack trace (this is how the code was originally)
|
|
|
- logMsg(String(msg))
|
|
|
- return
|
|
|
+ if (typeof arg === 'string') return arg
|
|
|
+ return util.inspect(arg)
|
|
|
}
|
|
|
|
|
|
- logMsg(msg)
|
|
|
+ const msgString = msgToString()
|
|
|
+ const masked = maskMessage(msgString)
|
|
|
+ // eslint-disable-next-line no-console
|
|
|
+ console.log(color(`companion: ${time} [${level}] ${id}${whitespace}${tag}`), color(masked))
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -97,11 +91,10 @@ exports.warn = (msg, tag, traceId) => {
|
|
|
* @param {string | Error} msg the message to log
|
|
|
* @param {string=} tag a unique tag to easily search for this message
|
|
|
* @param {string=} traceId a unique id to easily trace logs tied to a request
|
|
|
- * @param {boolean=} shouldLogStackTrace when set to true, errors will be logged with their stack trace
|
|
|
*/
|
|
|
-exports.error = (msg, tag, traceId, shouldLogStackTrace) => {
|
|
|
+exports.error = (msg, tag, traceId) => {
|
|
|
// @ts-ignore
|
|
|
- log(msg, tag, 'error', traceId, chalk.bold.red, shouldLogStackTrace)
|
|
|
+ log(msg, tag, 'error', traceId, chalk.bold.red)
|
|
|
}
|
|
|
|
|
|
/**
|