Repository URL to install this package:
|
Version:
1.0.1 ▾
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const winstonLogger = require('./winstonLogger');
/**
*
*/
class Logger {
/**
*
* @param {*} name
* @param {*} env
*/
constructor(name, env = {}) {
const {
K8S_POD_NAME: podName,
K8S_POD_NAMESPACE: podNamespace,
K8S_POD_IP: podIp,
K8S_NODE_NAME: nodeName,
LOG_LEVEL: logLevel,
NODE_ENV: environment,
instance
} = env;
if (!podName) {
console.log('Cannot get Kubernetes Pod Name from environment variables. Logs will be stored in the project directory.');
}
this.name = name;
this.env = {
podName,
podNamespace,
podIp,
nodeName,
logLevel,
environment,
pid: `${process.pid}`
}; // The file to write the log to.
const logFilePath = podName ? `/app/logs/${podName}/app.log` : 'app.log';
this.instance = instance || winstonLogger.createLogger({
logFilePath,
logLevel,
environment
});
}
/**
*
*/
createBaseLogMessage() {
return _objectSpread({
service: this.name,
pid: this.env.pid
}, this.env);
}
/**
* Adds a log entry, provided that the severity is equal or higher to the current service log level.
* @param {string} severity The severity level of the log
* @param {string} message The custom message to add to the log
* @param {*} additionalProperties Additional properties to add to the log
*/
writeLog(severity, message, additionalProperties = {}) {
this.instance.log(severity, _objectSpread({}, additionalProperties, this.createBaseLogMessage(), {
rest: message
}));
}
/**
*
* @param {String} message
* @param {Object} additionalProperties
*/
debug(message, additionalProperties) {
this.writeLog('debug', message, additionalProperties);
}
/**
*
* @param {String} message
* @param {Object} additionalProperties
*/
info(message, additionalProperties) {
this.writeLog('info', message, additionalProperties);
}
/**
*
* @param {String} message
* @param {Object} additionalProperties
*/
warn(message, additionalProperties) {
this.writeLog('warn', message, additionalProperties);
}
/**
*
* @param {String} message
* @param {Object} additionalProperties
*/
error(message, additionalProperties) {
this.writeLog('error', message, additionalProperties);
}
}
module.exports = Logger;