|
| 1 | +"use strict"; |
| 2 | +/** |
| 3 | + * Provides common resources for other modules in the pancloud SDK |
| 4 | + */ |
| 5 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 | +var logLevel; |
| 7 | +(function (logLevel) { |
| 8 | + logLevel[logLevel["DEBUG"] = 0] = "DEBUG"; |
| 9 | + logLevel[logLevel["INFO"] = 1] = "INFO"; |
| 10 | + logLevel[logLevel["ALERT"] = 2] = "ALERT"; |
| 11 | + logLevel[logLevel["ERROR"] = 3] = "ERROR"; |
| 12 | +})(logLevel = exports.logLevel || (exports.logLevel = {})); |
| 13 | +const LTYPES = { |
| 14 | + "panw.auth": "", |
| 15 | + "panw.config": "", |
| 16 | + "panw.dpi": "", |
| 17 | + "panw.dpi_hipreport": "", |
| 18 | + "panw.dpi_stats": "", |
| 19 | + "panw.gtp": "", |
| 20 | + "panw.gtpsum": "", |
| 21 | + "panw.hipmatch": "", |
| 22 | + "panw.sctp": "", |
| 23 | + "panw.sctpsum": "", |
| 24 | + "panw.system": "", |
| 25 | + "panw.threat": "", |
| 26 | + "panw.thsum": "", |
| 27 | + "panw.traffic": "", |
| 28 | + "panw.trsum": "", |
| 29 | + "panw.urlsum": "", |
| 30 | + "panw.userid": "", |
| 31 | + "tms.analytics": "", |
| 32 | + "tms.config": "", |
| 33 | + "tms.system": "", |
| 34 | + "tms.threat": "", |
| 35 | + "tms.traps": "" |
| 36 | +}; |
| 37 | +function isKnownLogType(t) { |
| 38 | + return LTYPES.hasOwnProperty(t); |
| 39 | +} |
| 40 | +exports.isKnownLogType = isKnownLogType; |
| 41 | +/** |
| 42 | + * Centralized logging capability for the whole pancloud SDK |
| 43 | + */ |
| 44 | +class sdkLogger { |
| 45 | + /** |
| 46 | + * |
| 47 | + * @param level only messages with a level equal or avobe this provided value will be loogged |
| 48 | + * @param stackTrace boolean value to toggle stacktrace logging |
| 49 | + */ |
| 50 | + constructor(level, stackTrace = true) { |
| 51 | + this.level = level; |
| 52 | + this.stackTrace = stackTrace; |
| 53 | + } |
| 54 | + error(e) { |
| 55 | + this.format(e.getSourceClass(), e.getErrorMessage(), logLevel.ERROR, e.name, e.getErrorCode(), undefined, e.stack); |
| 56 | + } |
| 57 | + alert(source, message, name) { |
| 58 | + this.format(source.className, message, logLevel.ALERT, name); |
| 59 | + } |
| 60 | + info(source, message, name) { |
| 61 | + this.format(source.className, message, logLevel.INFO, name); |
| 62 | + } |
| 63 | + debug(source, message, name, payload) { |
| 64 | + this.format(source.className, message, logLevel.DEBUG, name, undefined, payload); |
| 65 | + } |
| 66 | + format(source, message, level, name, code, payload, stack) { |
| 67 | + if (level >= this.level) { |
| 68 | + let output = { |
| 69 | + source, |
| 70 | + message |
| 71 | + }; |
| 72 | + let payloadOut = ''; |
| 73 | + if (name) { |
| 74 | + output['name'] = name; |
| 75 | + } |
| 76 | + if (code) { |
| 77 | + output['code'] = code; |
| 78 | + } |
| 79 | + if (stack) { |
| 80 | + output['stack'] = stack; |
| 81 | + } |
| 82 | + if (payload) { |
| 83 | + if (typeof payload == 'string') { |
| 84 | + payloadOut = payload; |
| 85 | + } |
| 86 | + else { |
| 87 | + let jsonText = JSON.stringify(payload); |
| 88 | + if (jsonText.length > 300) { |
| 89 | + payloadOut = jsonText.substr(0, 300) + ' ...'; |
| 90 | + } |
| 91 | + else { |
| 92 | + payloadOut = jsonText; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + let finalOutput = `PANCLOUD: ${JSON.stringify(output)}`; |
| 97 | + if (payloadOut != '') { |
| 98 | + finalOutput += ` payload=${payloadOut}`; |
| 99 | + } |
| 100 | + switch (level) { |
| 101 | + case logLevel.ERROR: { |
| 102 | + console.error(finalOutput); |
| 103 | + break; |
| 104 | + } |
| 105 | + case logLevel.ALERT: |
| 106 | + case logLevel.INFO: { |
| 107 | + console.info(finalOutput); |
| 108 | + break; |
| 109 | + } |
| 110 | + default: { |
| 111 | + console.info(finalOutput); |
| 112 | + } |
| 113 | + } |
| 114 | + if (this.stackTrace && stack) { |
| 115 | + console.error(stack); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | +/** |
| 121 | + * Instantiate a module-provided logger at load time |
| 122 | + */ |
| 123 | +exports.commonLogger = new sdkLogger(logLevel.INFO, false); |
| 124 | +/** |
| 125 | + * Developer might decide to change the loglevel of the logger object at runtime |
| 126 | + * @param newLevel the new log level |
| 127 | + */ |
| 128 | +function setLogLevel(newLevel) { |
| 129 | + exports.commonLogger.level = newLevel; |
| 130 | +} |
| 131 | +exports.setLogLevel = setLogLevel; |
| 132 | +/** |
| 133 | + * Changes the common logger variable to a user-provided object |
| 134 | + * @param logger user provided pancloudLogger compliant object to be used for SDK logging |
| 135 | + */ |
| 136 | +function setLogger(logger) { |
| 137 | + exports.commonLogger = logger; |
| 138 | +} |
| 139 | +exports.setLogger = setLogger; |
| 140 | +/** |
| 141 | + * Abstract function used to retry multiple times a user-provided operation |
| 142 | + * @param source class using the retrier. Its className property value will be used in logs generated by the retrier |
| 143 | + * @param n number of attempts |
| 144 | + * @param delay milliseconds to wait after a failed attempt |
| 145 | + * @param handler function that implements the operation |
| 146 | + * @param params additional arguments to be passed to the handler function |
| 147 | + */ |
| 148 | +async function retrier(source, n = 3, delay = 100, handler, ...params) { |
| 149 | + let a = n; |
| 150 | + let lastError = undefined; |
| 151 | + while (a > 0) { |
| 152 | + try { |
| 153 | + return await handler(...params); |
| 154 | + } |
| 155 | + catch (e) { |
| 156 | + exports.commonLogger.info(source, `Failed attempt ${a}`, 'RETRIER'); |
| 157 | + lastError = e; |
| 158 | + } |
| 159 | + await new Promise((resolve) => { |
| 160 | + setTimeout(resolve, delay); |
| 161 | + }); |
| 162 | + a--; |
| 163 | + } |
| 164 | + throw (lastError) ? lastError : new Error('reties exhausted'); |
| 165 | +} |
| 166 | +exports.retrier = retrier; |
0 commit comments