@dotcms/analytics
Version:
Official JavaScript library for Content Analytics with DotCMS.
91 lines (90 loc) • 2.2 kB
JavaScript
const o = {
debug: 0,
info: 1,
warn: 2,
error: 3
};
class r {
constructor(e, i, t = "warn") {
this.packageName = e, this.context = i, this.minLevel = t;
}
/**
* Creates the formatted prefix for log messages
* Format: [DotCMS PackageName | Context] [LEVEL]
*/
getPrefix(e) {
return `[DotCMS ${this.packageName} | ${this.context}] [${e.toUpperCase()}]`;
}
/**
* Checks if a log level should be displayed based on the minimum threshold
*/
shouldLog(e) {
return o[e] >= o[this.minLevel];
}
/**
* Log a DEBUG level message
* Used for detailed debugging information
*/
debug(...e) {
this.shouldLog("debug") && console.log(this.getPrefix("debug"), ...e);
}
/**
* Log an INFO level message
* Used for general informational messages
*/
info(...e) {
this.shouldLog("info") && console.info(this.getPrefix("info"), ...e);
}
/**
* Log a WARN level message
* Used for warning messages that don't prevent operation
*/
warn(...e) {
this.shouldLog("warn") && console.warn(this.getPrefix("warn"), ...e);
}
/**
* Log an ERROR level message
* Always logs regardless of threshold for critical errors
*/
error(...e) {
console.error(this.getPrefix("error"), ...e);
}
/**
* Create a console group for organizing related log messages
* Respects the minimum log level threshold
*/
group(e) {
this.shouldLog("debug") && console.group(`${this.getPrefix("debug")} ${e}`);
}
/**
* End the current console group
* Respects the minimum log level threshold
*/
groupEnd() {
this.shouldLog("debug") && console.groupEnd();
}
/**
* Start a timer with the given label
* Useful for performance measurements
*/
time(e) {
this.shouldLog("debug") && console.time(`${this.getPrefix("debug")} ${e}`);
}
/**
* End a timer and log the elapsed time
* Useful for performance measurements
*/
timeEnd(e) {
this.shouldLog("debug") && console.timeEnd(`${this.getPrefix("debug")} ${e}`);
}
/**
* Log a message (alias for info)
* Provided for backward compatibility
*/
log(...e) {
this.info(...e);
}
}
export {
r as DotLogger
};