@milandadhaniya/tiny-logger-js
Version:
A tiny logger for both Node.js and browser environments, with configurable log levels and console output methods.
64 lines (63 loc) • 1.63 kB
JavaScript
// src/index.ts
var TinyLogger = class {
constructor(config) {
this.allowed = config?.allowed ?? /* @__PURE__ */ new Set([
"log",
"info",
"warn",
"error",
"debug",
"table",
"trace",
"group",
"groupEnd",
"time",
"timeEnd"
]);
}
shouldLog(type, force = false) {
return force || this.allowed.has(type);
}
format(title, msg) {
return title ? [title, msg] : [msg];
}
log({ title, msg, force }) {
if (this.shouldLog("log", force)) console.log(...this.format(title, msg));
}
info({ title, msg, force }) {
if (this.shouldLog("info", force)) console.info(...this.format(title, msg));
}
warn({ title, msg, force }) {
if (this.shouldLog("warn", force)) console.warn(...this.format(title, msg));
}
error({ title, msg, force }) {
if (this.shouldLog("error", force)) console.error(...this.format(title, msg));
}
debug({ title, msg, force }) {
if (this.shouldLog("debug", force)) console.debug(...this.format(title, msg));
}
trace(msg) {
if (this.shouldLog("trace")) console.trace(msg);
}
table({ msg, force }) {
if (this.shouldLog("table", force)) console.table(msg);
}
group(label) {
if (this.shouldLog("group")) console.group(label);
}
groupEnd() {
if (this.shouldLog("groupEnd")) console.groupEnd();
}
time(label) {
if (this.shouldLog("time")) console.time(label);
}
timeEnd(label) {
if (this.shouldLog("timeEnd")) console.timeEnd(label);
}
};
var logger = new TinyLogger();
var index_default = logger;
export {
TinyLogger,
index_default as default
};