@splunk/rum-cli
Version:
Tools for handling symbol and mapping files for symbolication
56 lines (55 loc) • 2.67 kB
JavaScript
;
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLogger = createLogger;
const chalk_1 = __importDefault(require("chalk"));
function createLogger(logLevel, spinner) {
// Send info to stdout, and all other logs to stderr
const basicLogger = {
error: (msg, ...params) => 4 /* LogLevel.ERROR */ >= logLevel && prefixedConsoleError(chalk_1.default.stderr.red('ERROR '), msg, ...params),
warn: (msg, ...params) => 3 /* LogLevel.WARN */ >= logLevel && prefixedConsoleError(chalk_1.default.stderr.yellow('WARN '), msg, ...params),
info: (msg, ...params) => 2 /* LogLevel.INFO */ >= logLevel && console.log(msg, ...params),
debug: (msg, ...params) => 1 /* LogLevel.DEBUG */ >= logLevel && prefixedConsoleError(chalk_1.default.stderr.gray('DEBUG '), msg, ...params),
};
if (spinner) {
// wrap logging functions with spinner.interrupt() to avoid jumbled logs when the spinner is active
const spinnerAwareLogger = {
error: (...args) => { spinner.interrupt(() => basicLogger.error(...args)); },
warn: (...args) => { spinner.interrupt(() => basicLogger.warn(...args)); },
info: (...args) => { spinner.interrupt(() => basicLogger.info(...args)); },
debug: (...args) => { spinner.interrupt(() => basicLogger.debug(...args)); },
};
return spinnerAwareLogger;
}
else {
return basicLogger;
}
}
/** Carefully wrap console.error so the logger can properly support format strings */
const prefixedConsoleError = (prefix, msg, ...params) => {
if (typeof msg === 'string') {
// String concatenation is needed for format strings,
// otherwise console.error('Hello ', '%s!', ' World') would print 'Hello %s! World', not 'Hello World!'
console.error(`${prefix}${msg}`, ...params);
}
else {
console.error(prefix, msg, ...params);
}
};