UNPKG

appdynamics

Version:

Performance Profiler and Monitor

173 lines (146 loc) 4.47 kB
/* Copyright (c) AppDynamics, Inc., and its affiliates 2015 All Rights Reserved */ 'use strict'; function isDebugEnabled(agent) { return agent && agent.opts && agent.opts.debug; } function Logger(agent) { this.agent = agent; // temporary stubs, until initialized /* istanbul ignore next */ this.logger = { trace: function (msg) { if (isDebugEnabled(agent)) console.log(msg); }, debug: function (msg) { if (isDebugEnabled(agent)) console.log(msg); }, info: function (msg) { if (isDebugEnabled(agent)) console.info(msg); }, warn: function (msg) { if (isDebugEnabled(agent)) console.warn(msg); }, error: function (msg) { console.error(msg); }, fatal: function (msg) { console.error(msg); }, env: function (msg) { if (isDebugEnabled(agent)) console.info(msg); }, isTraceEnabled: function () { return isDebugEnabled(agent); }, isDebugEnabled: function () { return isDebugEnabled(agent); }, isInfoEnabled: function () { return isDebugEnabled(agent); }, isWarnEnabled: function () { return isDebugEnabled(agent); }, isErrorEnabled: function () { return isDebugEnabled(agent); }, isFatalEnabled: function () { return isDebugEnabled(agent); } }; } exports.Logger = Logger; /* istanbul ignore next -- log4j mocked in unit tests */ Logger.prototype.init = function (config) { if (!config) { config = {}; } var rootPath = config.root_directory || this.agent.tmpDir; // Libagent logFile is already created in the libagent. Since, there is no // way to tap on to the file name created by boost::log used by libagent // show the log filePath till agent tmpDir. var fileName = this.agent.tmpDir; var consoleOnly = (this.agent.opts.logging && this.agent.opts.logging.logfiles && this.agent.opts.logging.logfiles.every(function (lf) { return lf.outputType == 'console'; })); // If console logging env variable is set and no custom logging config is provided // then don't create temp path for logs if (process.env.APPDYNAMICS_LOGGER_OUTPUT_TYPE === "console" && (!config.logfiles || config.logfiles.length == 0)) { consoleOnly = true; } if (!consoleOnly) { this.agent.recursiveMkDir(rootPath); } if (this.agent.opts.debug) { // force logging level to DEBUG and output location of logs config.level = 'DEBUG'; if (!consoleOnly) { console.log('[DEBUG] Appdynamics agent logs: ' + fileName); } } // libagent uses its own logging, and does not need the log4js backend to be initialized this.libAgentConnector.initLogger(); }; Logger.prototype.trace = function (msg) { if (this.libAgentConnector) { this.libAgentConnector.logTrace(msg); return; } this.logger.trace(msg); }; Logger.prototype.debug = function (msg) { if (this.libAgentConnector) { this.libAgentConnector.logDebug(msg); return; } this.logger.debug(msg); }; Logger.prototype.info = function (msg) { if (this.libAgentConnector) { this.libAgentConnector.logInfo(msg); return; } this.logger.info(msg); }; Logger.prototype.warn = function (msg) { if (this.libAgentConnector) { this.libAgentConnector.logWarn(msg); return; } this.logger.warn(msg); }; Logger.prototype.error = function (err) { if (this.libAgentConnector) { this.libAgentConnector.logError(err.stack ? err.stack : err); return; } this.logger.error(err.stack ? err.stack : err); }; Logger.prototype.fatal = function (msg) { if (this.libAgentConnector) { this.libAgentConnector.logFatal(msg); return; } this.logger.fatal(msg); }; Logger.prototype.env = function (msg) { if (this.libAgentConnector) { this.libAgentConnector.logEnv(msg); return; } this.logger.info(msg); }; Logger.prototype.setLibAgentConnector = function (libAgentConnector) { this.libAgentConnector = libAgentConnector; }; Logger.prototype.isTraceEnabled = function () { return this.logger.isTraceEnabled(); }; Logger.prototype.isDebugEnabled = function () { return this.logger.isDebugEnabled(); }; Logger.prototype.isInfoEnabled = function () { return this.logger.isInfoEnabled(); }; Logger.prototype.isWarnEnabled = function () { return this.logger.isWarnEnabled(); }; Logger.prototype.isErrorEnabled = function () { return this.logger.isErrorEnabled(); }; Logger.prototype.isFatalEnabled = function () { return this.logger.isFatalEnabled(); };