NumminorihSF / ami-io

Use node.js or io.js to manage Asterisk through AMI
MIT License
30 stars 16 forks source link

Turn off Logger #7

Closed JasonScopServ closed 7 years ago

JasonScopServ commented 7 years ago

Hi I'm trying to turn off the logger. I tried ami.useLogger but this doesn't seem to work. Please if you can give me any advice on this it would be much appreciated, I would like to do all the logging myself, right now everything just gets printed to the console and I can't see anything because it's too busy.

Thanks.

NumminorihSF commented 7 years ago

How do you try to turn logger off? Put code example here, please.

JasonScopServ commented 7 years ago

`ami.useLogger(console); I know this is probably wrong...

Seafnox commented 7 years ago
/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 (Seafnox) Kirill Warp
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

function Logger(level){
  this.level = level || "Debug";
  this.useFile = false;
  this.useConsole = true;
  this.fs = require("fs");
  return this;
}

Logger.prototype.setLevel = function(level){
  this.level = level;
};

Logger.prototype.disableConsole = function(disable) {
  this.useConsole = disable;
};

Logger.prototype.levels = ['Fatal', 'Error', 'Warn', 'Info', 'Debug', 'Trace'];

Logger.prototype.buildMsg = function() {
  var date = new Date();
  var time = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
  var args = [];
  for (var i = 0; i < arguments[0].length; i++) {
    if (typeof arguments[0][i] === 'object') {
      try {
        args.push(JSON.stringify(arguments[0][i]));
      } catch (e) {
        if (args instanceof Array) {
          args.push("Array is too big: " + arguments[0][i].length);
        } else {
          args.push("Object is too big: " + Object.keys(arguments[0][i]).join(","));
        }
        args.push(e.stack);
      }
    } else {
      args.push(arguments[0][i]);
    }
  }
  return time + " " + args.join(", ");
};

Logger.prototype.checkAndSend = function(checkedLevel, args, consoleLevel) {
  if (this.levels.indexOf(checkedLevel) <= this.levels.indexOf(this.level)) {
    if (this.useConsole) {
      if (consoleLevel === 'Error') {
        console.error(this.buildMsg(args));
      } else {
        console.log(this.buildMsg(args));
      }
    }
  }
};

Logger.prototype.fatal = function() { this.checkAndSend('Fatal', arguments, 'Error')};

Logger.prototype.error = function() { this.checkAndSend('Error', arguments, 'Error')};

Logger.prototype.warn = function() { this.checkAndSend('Warn', arguments, 'Error')};

Logger.prototype.info = function() { this.checkAndSend('Info', arguments)};

Logger.prototype.debug = function() { this.checkAndSend('Debug', arguments)};

Logger.prototype.trace = function() { this.checkAndSend('Trace', arguments)};

if (module.parent){
  module.exports = Logger;
}
else {
  var logger = new Logger();
}
Seafnox commented 7 years ago

for usage jous run logger var logger = new (require(config.paths.cftools + '/modules/LoggerCap.js'))("Error");

and put in amiio client

AmiIo.createClient({logger: logger});

Seafnox commented 7 years ago

NumminorihSF, this man is right in something. You can allow to deny logging in your library, or allow to set log level. like:

AmiIo.createClient({logger: false}); or AmiIo.createClient({logLevel: "Error"}); or AmiIo.createClient({logLevel: false});

NumminorihSF commented 7 years ago

I added silent logger object to package. But in most cases it is not problem of this package, how you log info.