81 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			81 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| const LOGGER_LEVEL = {
 | |
|     DEBUG: 1,
 | |
|     INFO: 2,
 | |
|     WARNING: 3,
 | |
|     ERROR: 4,
 | |
|     DISABLED: 100,
 | |
|     properties: {
 | |
|         1: { name: "debug", value: 1, prefix: "DEBUG" },
 | |
|         2: { name: "info", value: 2, prefix: "INFO" },
 | |
|         3: { name: "warning", value: 3, prefix: "WARN" },
 | |
|         4: { name: "error", value: 3, prefix: "ERROR" }
 | |
|     }
 | |
| };
 | |
| 
 | |
| class Logger {
 | |
|     _notificationId = undefined;
 | |
|     _log_level = LOGGER_LEVEL.INFO;
 | |
|     _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) {
 | |
|         this._log_level = val;
 | |
|     }
 | |
|     get notifyLevel() {
 | |
|         return this._notify_level;
 | |
|     }
 | |
|     set notifyLevel(val) {
 | |
|         this._notify_level = val;
 | |
|     }
 | |
|     log(level, loggerFn, ...msgs) {
 | |
|         if (level < this._log_level) return;
 | |
|         let time = new Date().toLocaleString();
 | |
|         loggerFn(`${time} [${LOGGER_LEVEL.properties[level].prefix}]`, ...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.WARNING, 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 }
 | |
|         );
 | |
|     }
 | |
| }
 | |
| 
 | |
| const logger = new Logger(LOGGER_LEVEL.DEBUG, LOGGER_LEVEL.DISABLED); |