@sonar/scan
Version:
SonarQube/SonarCloud Scanner for the JavaScript world
78 lines (77 loc) • 2.47 kB
JavaScript
;
/*
* sonar-scanner-npm
* Copyright (C) 2022-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogLevel = void 0;
exports.log = log;
exports.logWithPrefix = logWithPrefix;
exports.getLogLevel = getLogLevel;
exports.setLogLevel = setLogLevel;
var LogLevel;
(function (LogLevel) {
LogLevel["TRACE"] = "TRACE";
LogLevel["DEBUG"] = "DEBUG";
LogLevel["INFO"] = "INFO";
LogLevel["WARN"] = "WARN";
LogLevel["ERROR"] = "ERROR";
})(LogLevel || (exports.LogLevel = LogLevel = {}));
const logLevelValues = {
ERROR: 0,
WARN: 1,
INFO: 2,
DEBUG: 3,
TRACE: 4,
};
const DEFAULT_LOG_LEVEL = LogLevel.INFO;
const LOG_MESSAGE_PADDING = 7;
let logLevel = DEFAULT_LOG_LEVEL;
function log(level, ...message) {
logWithPrefix(level, 'Bootstrapper', ...message);
}
function logWithPrefix(level, prefix, ...message) {
if (logLevelValues[level] > logLevelValues[logLevel]) {
return;
}
const levelStr = `[${level}]`.padEnd(LOG_MESSAGE_PADDING);
console.log(levelStr, `${prefix}:`, ...message);
}
function getLogLevel() {
return logLevel;
}
function stringToLogLevel(level) {
switch (level.toUpperCase()) {
case 'ERROR':
return LogLevel.ERROR;
case 'WARN':
return LogLevel.WARN;
case 'INFO':
return LogLevel.INFO;
case 'DEBUG':
return LogLevel.DEBUG;
case 'TRACE':
return LogLevel.TRACE;
default:
log(LogLevel.WARN, `Invalid log level: ${level}`);
return DEFAULT_LOG_LEVEL;
}
}
function setLogLevel(level) {
logLevel = stringToLogLevel(level);
}