74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
export enum LOGGER_LEVEL {
|
|
DEBUG = 1,
|
|
INFO,
|
|
WARN,
|
|
ERROR,
|
|
DISABLED,
|
|
};
|
|
|
|
export class Logger {
|
|
private _notificationId = undefined;
|
|
private _log_level = LOGGER_LEVEL.INFO;
|
|
private _notify_level = LOGGER_LEVEL.ERROR;
|
|
constructor(logLevel, notifyLevel) {
|
|
if (logLevel) this._log_level = logLevel;
|
|
if (notifyLevel) this._notify_level = notifyLevel;
|
|
chrome.notifications.onClosed.addListener((id, byUser) => { this._notify_level = undefined });
|
|
}
|
|
get logLevel() {
|
|
return this._log_level;
|
|
}
|
|
set logLevel(val: LOGGER_LEVEL) {
|
|
this._log_level = val;
|
|
}
|
|
get notifyLevel() {
|
|
return this._notify_level;
|
|
}
|
|
set notifyLevel(val: LOGGER_LEVEL) {
|
|
this._notify_level = val;
|
|
}
|
|
log(level: LOGGER_LEVEL, loggerFn: Function, ...msgs) {
|
|
if (level < this._log_level) return;
|
|
let time = new Date().toLocaleString();
|
|
loggerFn(`${time} [${LOGGER_LEVEL[level]}]`, ...msgs);
|
|
if (level < this._notify_level) return;
|
|
this.notify(...msgs);
|
|
}
|
|
debug(...msgs) {
|
|
this.log(LOGGER_LEVEL.DEBUG, console.debug, ...msgs);
|
|
}
|
|
info(...msgs) {
|
|
this.log(LOGGER_LEVEL.INFO, console.info, ...msgs);
|
|
}
|
|
warn(...msgs) {
|
|
this.log(LOGGER_LEVEL.WARN, console.info, ...msgs);
|
|
}
|
|
error(...msgs) {
|
|
this.log(LOGGER_LEVEL.ERROR, console.info, ...msgs);
|
|
}
|
|
notify(...msgs) {
|
|
let msg = msgs.join(' ');
|
|
if (!this._notificationId) {
|
|
chrome.notifications.create(
|
|
null,
|
|
{
|
|
"type": "basic",
|
|
"iconUrl": chrome.extension.getURL('icon.png'),
|
|
"title": "Data Extractor",
|
|
"message": msg,
|
|
"priority": 0,
|
|
"requireInteraction": true,
|
|
},
|
|
notificationId => {
|
|
this._notificationId = notificationId;
|
|
}
|
|
);
|
|
return;
|
|
}
|
|
chrome.notifications.update(
|
|
this._notificationId,
|
|
{ "message": msg }
|
|
);
|
|
}
|
|
}
|