loganalysis
Version:
Collects stdout and stderr messages from Node.js applications and sends them to the IBM SmartCloud Analytics - Log Analysis service.
145 lines (118 loc) • 3.82 kB
JavaScript
/*
* © 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.
*/
/**
* The logger utilities here will only log the message to native console, thinking of the scenario below :
* a. In case some errors occurred while connecting to the remote scala server, and the codes log some error messages
* to the stand error output stream, while the process.stderr.write method is overridden, so it could send the error messages to
* scala recursively
* b. scala is for node.js/application runtime logging retention, it may not make much sense to record the messages from
* loganalysis module there.
*/
const NATIVE_STDERR_WRITE = process.stderr.write;
const NATIVE_STDOUT_WRITE = process.stdout.write;
const ALSO_LOG_TO_FILE = false;
const DEFAULT_PARENT_LOGGER_NAME = 'logger_log_analysis_level';
var fs = require('fs');
var createdLoggers = {};
function getLogFile() {
var file = 'scala_node_js_dc.log';
var id = 1;
while (true) {
if (fs.existsSync(file)) {
file = 'scala_node_js_dc.log.' + id++;
} else {
break;
}
}
return file;
}
const LOG_FILE = getLogFile();
function Logger(name) {
this.name = name;
this.logToFile = false;
var level = process.env['logger_' + name + '_level'];
if(!level) {
level = process.env[DEFAULT_PARENT_LOGGER_NAME];
}
if(!level) {
level = 'info';
}
this.setLevel(level);
createdLoggers[name] = this;
}
Logger.prototype.setLogToFile = function(log_to_file) {
this.logToFile = log_to_file;
}
Logger.prototype.writeLog = function (writer, writeto, msg) {
var str = this.name + '[' + this.logLevel + ']: ' + msg + '\n';
writer.call(writeto, str);
if (this.logToFile === true) {
fs.appendFileSync(LOG_FILE, str);
}
}
Logger.prototype.info = function (msg) {
if (this.infoEnabled) {
this.writeLog(NATIVE_STDOUT_WRITE, process.stdout, msg);
}
};
Logger.prototype.error = function (msg) {
if (this.errorEnabled) {
this.writeLog(NATIVE_STDERR_WRITE, process.stderr, msg);
}
};
Logger.prototype.warn = function (msg) {
if (this.warnEnabled) {
this.writeLog(NATIVE_STDOUT_WRITE, process.stdout, msg);
}
};
Logger.prototype.debug = function (msg) {
if (this.debugEnabled) {
this.writeLog(NATIVE_STDOUT_WRITE, process.stdout, msg);
}
};
Logger.prototype.isDebugEnabled = function () {
return this.debugEnabled;
};
Logger.prototype.isInfoEnabled = function () {
return this.infoEnabled;
};
Logger.prototype.isErrorEnabled = function () {
return this.errorEnabled;
};
Logger.prototype.isWarnEnabled = function () {
return this.warnEnabled;
};
Logger.prototype.getLevel = function () {
return this.logLevel;
};
Logger.prototype.setLevel = function(level) {
if(level === this.logLevel) {
return;
}
if(level != 'info' && level != 'debug' && level != 'warn' && level != 'error') {
return;
}
this.logLevel = level;
this.debugEnabled = this.logLevel == 'debug';
this.infoEnabled = this.debugEnabled || (this.logLevel == 'info');
this.warnEnabled = this.infoEnabled || (this.logLevel == 'warn');
this.errorEnabled = this.warnEnabled || (this.logLevel == 'error');
};
module.exports.newLogger = function (name) {
return new Logger(name);
};
module.exports.loggers = createdLoggers;
module.exports.Logger = Logger;