@microsoft/teams.common
Version:
<p> <a href="https://www.npmjs.com/package/@microsoft/teams.common" target="_blank"> <img src="https://img.shields.io/npm/v/@microsoft/teams.common" /> </a> <a href="https://www.npmjs.com/package/@microsoft/teams.common?activeTab=code"
89 lines (87 loc) • 2.07 kB
JavaScript
import { ANSI } from './ansi';
class ConsoleLogger {
name;
level;
_enabled;
_levels = {
error: 100,
warn: 200,
info: 300,
debug: 400
};
_colors = {
error: ANSI.ForegroundRed,
warn: ANSI.ForegroundYellow,
info: ANSI.ForegroundCyan,
debug: ANSI.ForegroundMagenta
};
constructor(name, options) {
this.name = name;
const env = typeof process === "undefined" ? void 0 : process.env;
const logNamePattern = env?.LOG || options?.pattern || "*";
this._enabled = parseMagicExpr(logNamePattern).test(name);
this.level = parseLogLevel(env?.LOG_LEVEL) || options?.level || "info";
}
error(...msg) {
this.log("error", ...msg);
}
warn(...msg) {
this.log("warn", ...msg);
}
info(...msg) {
this.log("info", ...msg);
}
debug(...msg) {
this.log("debug", ...msg);
}
log(level, ...msg) {
if (!this._enabled) {
return;
}
if (this._levels[level] > this._levels[this.level]) {
return;
}
const prefix = [this._colors[level], ANSI.Bold, `[${level.toUpperCase()}]`];
const name = [this.name, ANSI.ForegroundReset, ANSI.BoldReset];
for (const m of msg) {
let text = new String(m);
if (typeof m === "object") {
text = JSON.stringify(m, null, 2);
}
for (const line of text.split("\n")) {
console[level](prefix.join(""), name.join(""), line);
}
}
}
child(name) {
return new ConsoleLogger(`${this.name}/${name}`, {
level: this.level
});
}
}
function parseMagicExpr(pattern) {
let res = "";
const parts = pattern.split("*");
for (let i = 0; i < parts.length; i++) {
if (i > 0) {
res += ".*";
}
res += parts[i];
}
return new RegExp(res);
}
function parseLogLevel(level) {
const value = level?.toLowerCase();
switch (value) {
case "error":
case "warn":
case "info":
case "debug":
return value;
default:
return void 0;
}
}
export { ConsoleLogger };
//# sourceMappingURL=console.mjs.map
//# sourceMappingURL=console.mjs.map