UNPKG

domotz-remote-pawn

Version:

Domotz Agent

137 lines (129 loc) 4.49 kB
/** * This file is part of Domotz Agent. * * @license * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. * * @copyright Copyright (C) Domotz Inc */ /** * The Custom Driver Console. * Can be used for writing log messages during custom driver manual executions * @example * // appends the "Hello world" string to the execution logs array at info-level * console.info("Hello world"); * @namespace console */ const PASS_REMOVER = /pass: \'.*?\'/; const PASSWORD_REMOVER_1 = /password: \'.*?\'/; const PASSWORD_REMOVER_2 = /"password":".*?"/; const SNMP_READ_REMOVER = /snmp_read_community: \'.*?\'/; const SNMP_WRITE_REMOVER = /snmp_write_community: \'.*?\'/; /** * Creates the logger object * @constructor * @private * @param {(info|debug|warning|error)} level - The log level * @param {number} maxSize - The maximum number of log entries * @return {object} - The console Object */ const logger = function (level, maxSize) { var log = []; function nop() {} function create(prefix) { return function () { var msg = arguments[0]; var format = require('util').format; for (var i = 1; i < arguments.length; i++) { msg = format(msg, arguments[i]); } if (typeof msg === 'string') { msg = msg.replace(SNMP_READ_REMOVER, "snmp_read_community: '*****'"); msg = msg.replace(SNMP_WRITE_REMOVER, "snmp_write_community: '*****'"); msg = msg.replace(PASS_REMOVER, "pass: '*****'"); msg = msg.replace(PASSWORD_REMOVER_1, "password: '*****'"); msg = msg.replace(PASSWORD_REMOVER_2, '"password":"*****"'); } log.push(prefix + ' ' + msg); if (log.length > maxSize) { log.shift(); } }; } var logger = { /** * Write the given message to the sandbox log (debug-level). * Prefixed by 'D:' in the sandbox logs * @function debug * @memberof console * @param {string} message - The message to log */ debug: create('D:'), /** * Write the given message to the sandbox log (info-level). * Prefixed by 'I:' in the sandbox logs * @function info * @memberof console * @param {string} message - The message to log */ info: create('I:'), /** * Write the given message to the sandbox log (warning-level). * Prefixed by 'W:' in the sandbox logs * @function warn * @memberof console * @param {string} message - The message to log */ warn: create('W:'), /** * Write the given message to the sandbox log (error-level). * Prefixed by 'E:' in the sandbox logs * @function error * @memberof console * @param {string} message - The message to log */ error: create('E:'), /** * Returns the log message entries * @function get * @memberof console * @return {Array.<string>} - An array of log messages */ get: function () { return log; }, }; switch (level.toLowerCase()) { case 'info': logger.debug = nop; break; case 'warning': logger.debug = nop; logger.info = nop; break; case 'error': logger.debug = nop; logger.info = nop; logger.warn = nop; break; } logger.log = logger.debug; logger.verbose = logger.debug; logger.warning = logger.warn; logger.decorate = function (correlationId) { return logger; }; return logger; }; module.exports.logger = logger;