@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
99 lines (96 loc) • 2.84 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 1.0.3
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bitrix24/b24jssdk
* @see https://bitrix24.github.io/b24jssdk/
*/
import { LogLevel } from '../../types/logger.mjs';
import { AbstractHandler } from './abstract-handler.mjs';
import { LineFormatter } from '../formatter/line-formatter.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class ConsoleHandler extends AbstractHandler {
static {
__name(this, "ConsoleHandler");
}
_styles = /* @__PURE__ */ new Map();
_useStyles;
constructor(level = LogLevel.DEBUG, options) {
const opts = {
useStyles: true,
...options
};
super(level, opts.bubble);
this._useStyles = opts.useStyles;
this._initStyles();
this.setFormatter(new LineFormatter());
}
_initStyles() {
const style = "color: _color_; background: _bg_; padding: 2px 6px; border-radius: 3px; font-size: 11px;";
this._styles.set(LogLevel.DEBUG, [
"%cDEBUG",
style.replace("_color_", "#666666").replace("_bg_", "#F0F0F0")
]);
this._styles.set(LogLevel.INFO, [
"%cINFO",
style.replace("_color_", "white").replace("_bg_", "#2196F3")
]);
this._styles.set(LogLevel.NOTICE, [
"%cNOTICE",
style.replace("_color_", "white").replace("_bg_", "#213BF3")
]);
this._styles.set(LogLevel.WARNING, [
"%cWARN",
style.replace("_color_", "white").replace("_bg_", "#FF9800")
]);
this._styles.set(LogLevel.ERROR, [
"%cERROR",
style.replace("_color_", "white").replace("_bg_", "#F44336")
]);
this._styles.set(LogLevel.CRITICAL, [
"%cCRITICAL",
style.replace("_color_", "white").replace("_bg_", "#9C27B0")
]);
}
/**
* @inheritDoc
*/
async handle(record) {
const formatter = this.getFormatter();
const message = formatter.format(record);
let method = this._getConsoleMethod(record.level);
if (record.context["needTrace"] === true) {
method = "trace";
}
const params = [];
if (this._useStyles && this._styles.has(record.level)) {
const style = this._styles.get(record.level);
params.push(style[0], style[1]);
}
params.push(message);
console[method](
...params.filter(Boolean)
);
return true;
}
_getConsoleMethod(level) {
switch (level) {
case LogLevel.INFO:
case LogLevel.NOTICE:
return "info";
case LogLevel.WARNING:
return "warn";
case LogLevel.ERROR:
case LogLevel.CRITICAL:
case LogLevel.ALERT:
case LogLevel.EMERGENCY:
return "error";
default:
return "log";
}
}
}
export { ConsoleHandler };
//# sourceMappingURL=console-handler.mjs.map