UNPKG

loganalysis

Version:

Collects stdout and stderr messages from Node.js applications and sends them to the IBM SmartCloud Analytics - Log Analysis service.

143 lines (122 loc) 4.39 kB
/* * © Copyright IBM Corp. 2014 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var scala = require('./scala'); var logger = require('./logger').newLogger('console_config'); const INFO_LEVEL = 'I'; const ERROR_LEVEL = 'E'; function ConsoleAutoConfig() { this.nativeStderrWrite = process.stderr.write; this.nativeStdoutWrite = process.stdout.write; this.scalaClients = scala.createCloudAwareScalaClients(); this.initialized = false; } ConsoleAutoConfig.prototype.setUp = function () { //Use initialized flag to avoid duplicated setUp invocation if (this.initialized) { return; } var scalaClients = this.scalaClients; if (scalaClients.length == 0) { logger.debug('No scala clients is detected, skip the console auto-config initialization'); return; } if (!this.nativeStdoutWrite) { this.nativeStdoutWrite = process.stdout.write; } var oldStdoutWrite = this.nativeStdoutWrite; process.stdout.write = function () { if (arguments.length >= 1) { for (var i = 0, length = scalaClients.length; i < length; i++) { scalaClients[i].emit(arguments[0], INFO_LEVEL); } oldStdoutWrite.apply(process.stdout, arguments); } }; logger.debug('process.stdout.write is overriden'); if (!this.nativeStderrWrite) { this.nativeStderrWrite = process.stderr.write; } var oldStderrWrite = this.nativeStderrWrite; process.stderr.write = function () { if (arguments.length >= 1) { for (var i = 0, length = scalaClients.length; i < length; i++) { scalaClients[i].emit(arguments[0], ERROR_LEVEL); } } oldStderrWrite.apply(process.stderr, arguments); }; logger.debug('process.stderr.write is overriden'); logger.info('Initialization SUCCESS'); this.initialized = true; }; ConsoleAutoConfig.prototype.cleanUp = function () { logger.debug('Entering ConsoleAutoConfig.prototype.cleanUp'); if (!this.initialized) { return; } if (this.nativeStdoutWrite) { process.stdout.write = this.nativeStdoutWrite; this.nativeStdoutWrite= null; } if (this.nativeStderrWrite) { process.stderr.write = this.nativeStderrWrite; this.nativeStderrWrite = null; } for (var i = 0, length = this.scalaClients.length; i < length; i++) { this.scalaClients[i].cleanUp(); } this.initialized = false; logger.debug('Exiting ConsoleAutoConfig.prototype.cleanUp'); }; ConsoleAutoConfig.prototype.getNativeStdoutWrite = function () { return this.nativeStdoutWrite; }; ConsoleAutoConfig.prototype.getNativeStderrWrite = function () { return this.nativeStderrWrite; }; ConsoleAutoConfig.prototype.getActiveScalaClients = function () { return this.scalaClients; }; ConsoleAutoConfig.prototype.addScalaClient = function(scalaClient) { logger.debug('Entering ConsoleAutoConfig.prototype.addScalaClient'); for (var i = 0, length = this.scalaClients.length; i < length; i++) { if(this.scalaClients[i] === scalaClient) { return; } } this.scalaClients.push(scalaClient); if(!this.initialized) { logger.debug('One scala client is added, will set up the console auto-config'); this.setUp(); } logger.debug('Exiting ConsoleAutoConfig.prototype.addScalaClient'); }; ConsoleAutoConfig.prototype.removeScalaClient = function(scalaClient) { logger.debug('Entering ConsoleAutoConfig.prototype.removeScalaClient'); for (var i = 0, length = this.scalaClients.length; i < length; i++) { if(this.scalaClients[i] === scalaClient) { this.scalaClients.splice(i, 1); break; } } if(this.scalaClients.length == 0 && this.initialized) { logger.debug('No scala clients found, will clean up the console auto-config'); this.cleanUp(); } logger.debug('Exiting ConsoleAutoConfig.prototype.removeScalaClient'); }; var consoleAutoConfig = new ConsoleAutoConfig(); module.exports = consoleAutoConfig;