logolite
Version:
Lite Virtual Logger and Writer
410 lines (404 loc) • 11.8 kB
JavaScript
;
const os = require('os');
const idstream = require('idstream');
const getEnvOpt = require('../envtool').getEnvOpt;
const dbg = require('debug')('logolite:LogConfig');
//====================================================================
function Configuration() {
const a = this;
const b = {
DEFAULT_INSTANCE_ID: null,
DEFAULT_SECTOR: null,
TAGS_FIELD_NAME: null,
TEXT_FIELD_NAME: null,
ALWAYS_ENABLED: null,
ALWAYS_MUTED: null,
AUTO_DETECT_FOR: null,
DEBUGLOG_NAMES: null,
USE_BASE64_UUID: null,
TRACKING_DEPTH: null,
PREFIX_OF_INFO: null,
PREFIX_OF_EXTRA: null,
PREFIX_OF_TAGS: null,
IS_FULL_LOG_MODE: null,
IS_DEBUGLOG_ENABLED: null,
IS_MOCKLOGGER_ENABLED: null,
IS_TAGS_EMBEDDABLE: null,
IS_TEXT_EMBEDDABLE: null,
IS_TEMPLATE_APPLIED: null,
IS_TRACING_ID_PREDEFINED: null,
IS_INTERCEPTOR_ENABLED: null,
IS_STRINGIFY_ENABLED: null,
IS_STRINGIFY_PROTECTED: null
};
let c = true;
function d(a) {
if (getEnvOpt('NODE_ENV') !== 'test') c = false;
if (b.hasOwnProperty(a)) b[a] = null;
}
let e = {
DEFAULT_INSTANCE_ID: {
get: function () {
c && d('DEFAULT_INSTANCE_ID');
b.DEFAULT_INSTANCE_ID = b.DEFAULT_INSTANCE_ID || getEnvOpt('LOGOLITE_INSTANCE_ID') || a.getLogID();
return b.DEFAULT_INSTANCE_ID;
}
},
DEFAULT_SECTOR: {
get: function () {
c && d('DEFAULT_SECTOR');
b.DEFAULT_SECTOR = b.DEFAULT_SECTOR || getEnvOpt('LOGOLITE_DEFAULT_SECTOR') || 'logolite-default';
return b.DEFAULT_SECTOR;
}
},
TAGS_FIELD_NAME: {
get: function () {
c && d('TAGS_FIELD_NAME');
b.TAGS_FIELD_NAME = b.TAGS_FIELD_NAME || getEnvOpt('LOGOLITE_TAGS_FIELD_NAME') || '_tags_';
return b.TAGS_FIELD_NAME;
}
},
TEXT_FIELD_NAME: {
get: function () {
c && d('TEXT_FIELD_NAME');
b.TEXT_FIELD_NAME = b.TEXT_FIELD_NAME || getEnvOpt('LOGOLITE_TEXT_FIELD_NAME') || '_text_';
return b.TEXT_FIELD_NAME;
}
},
ALWAYS_ENABLED: {
get: function () {
c && d('ALWAYS_ENABLED');
if (b.ALWAYS_ENABLED === null) {
b.ALWAYS_ENABLED = stringToArray(getEnvOpt('LOGOLITE_ALWAYS_ENABLED'));
}
return b.ALWAYS_ENABLED;
}
},
ALWAYS_MUTED: {
get: function () {
c && d('ALWAYS_MUTED');
if (b.ALWAYS_MUTED === null) {
b.ALWAYS_MUTED = stringToArray(getEnvOpt('LOGOLITE_ALWAYS_MUTED'));
}
return b.ALWAYS_MUTED;
}
},
AUTO_DETECT_FOR: {
get: function () {
c && d('AUTO_DETECT_FOR');
b.AUTO_DETECT_FOR = b.AUTO_DETECT_FOR || getEnvOpt('LOGOLITE_AUTO_DETECT_FOR') || '';
return b.AUTO_DETECT_FOR;
}
},
DEBUGLOG_NAMES: {
get: function () {
c && d('DEBUGLOG_NAMES');
if (b.DEBUGLOG_NAMES === null) {
b.DEBUGLOG_NAMES = parseDebuglogLevels();
}
return b.DEBUGLOG_NAMES;
}
},
USE_BASE64_UUID: {
get: function () {
c && d('USE_BASE64_UUID');
if (b.USE_BASE64_UUID === null) {
b.USE_BASE64_UUID = getEnvOpt('LOGOLITE_BASE64_UUID') === 'true';
}
return b.USE_BASE64_UUID;
}
},
TRACKING_DEPTH: {
get: function () {
c && d('TRACKING_DEPTH');
if (b.TRACKING_DEPTH === null) {
let a = parseInt(getEnvOpt('LOGOLITE_TRACKING_DEPTH'));
b.TRACKING_DEPTH = isNaN(a) ? 2 : a;
}
return b.TRACKING_DEPTH;
}
},
PREFIX_OF_INFO: {
get: function () {
c && d('PREFIX_OF_INFO');
if (!b.PREFIX_OF_INFO) {
b.PREFIX_OF_INFO = getEnvOpt('LOGOLITE_PREFIX_OF_INFO') || '-I-';
b.PREFIX_OF_INFO = ' ' + b.PREFIX_OF_INFO + ' ';
}
return b.PREFIX_OF_INFO;
}
},
PREFIX_OF_EXTRA: {
get: function () {
c && d('PREFIX_OF_EXTRA');
if (!b.PREFIX_OF_EXTRA) {
b.PREFIX_OF_EXTRA = getEnvOpt('LOGOLITE_PREFIX_OF_EXTRA') || '-E-';
b.PREFIX_OF_EXTRA = ' ' + b.PREFIX_OF_EXTRA + ' ';
}
return b.PREFIX_OF_EXTRA;
}
},
PREFIX_OF_TAGS: {
get: function () {
c && d('PREFIX_OF_TAGS');
if (!b.PREFIX_OF_TAGS) {
b.PREFIX_OF_TAGS = getEnvOpt('LOGOLITE_PREFIX_OF_TAGS') || '-T-';
b.PREFIX_OF_TAGS = ' ' + b.PREFIX_OF_TAGS + ' ';
}
return b.PREFIX_OF_TAGS;
}
},
IS_FULL_LOG_MODE: {
get: function () {
c && d('IS_FULL_LOG_MODE');
if (b.IS_FULL_LOG_MODE === null) {
b.IS_FULL_LOG_MODE = getEnvOpt('LOGOLITE_FULL_LOG_MODE') !== 'false';
}
return b.IS_FULL_LOG_MODE;
}
},
IS_DEBUGLOG_ENABLED: {
get: function () {
c && d('IS_DEBUGLOG_ENABLED');
if (b.IS_DEBUGLOG_ENABLED === null) {
b.IS_DEBUGLOG_ENABLED = getEnvOpt('LOGOLITE_DEBUGLOG_ENABLED') === 'true';
}
return b.IS_DEBUGLOG_ENABLED;
}
},
IS_MOCKLOGGER_ENABLED: {
get: function () {
c && d('IS_MOCKLOGGER_ENABLED');
if (b.IS_MOCKLOGGER_ENABLED === null) {
b.IS_MOCKLOGGER_ENABLED = getEnvOpt('LOGOLITE_MOCKLOGGER_ENABLED') === 'true';
}
return b.IS_MOCKLOGGER_ENABLED;
}
},
IS_INTERCEPTOR_ENABLED: {
get: function () {
c && d('IS_INTERCEPTOR_ENABLED');
if (b.IS_INTERCEPTOR_ENABLED === null) {
b.IS_INTERCEPTOR_ENABLED = getEnvOpt('LOGOLITE_INTERCEPTOR_ENABLED') !== 'false';
}
return b.IS_INTERCEPTOR_ENABLED;
}
},
IS_TAGS_EMBEDDABLE: {
get: function () {
c && d('IS_TAGS_EMBEDDABLE');
if (b.IS_TAGS_EMBEDDABLE === null) {
b.IS_TAGS_EMBEDDABLE = getEnvOpt('LOGOLITE_TAGS_EMBEDDABLE') !== 'false';
}
return b.IS_TAGS_EMBEDDABLE;
}
},
IS_TEXT_EMBEDDABLE: {
get: function () {
c && d('IS_TEXT_EMBEDDABLE');
if (b.IS_TEXT_EMBEDDABLE === null) {
b.IS_TEXT_EMBEDDABLE = getEnvOpt('LOGOLITE_TEXT_EMBEDDABLE') === 'true';
}
return b.IS_TEXT_EMBEDDABLE;
}
},
IS_TEMPLATE_APPLIED: {
get: function () {
c && d('IS_TEMPLATE_APPLIED');
if (b.IS_TEMPLATE_APPLIED == null) {
b.IS_TEMPLATE_APPLIED = getEnvOpt('LOGOLITE_TEMPLATE_APPLIED') === 'true';
if (b.IS_TEMPLATE_APPLIED !== true) {
if (getEnvOpt('LOGOLITE_TEMPLATE_APPLIED') !== 'false') {
b.IS_TEMPLATE_APPLIED = !(getEnvOpt('DEBUG') == undefined);
}
}
dbg.enabled && dbg('IS_TEMPLATE_APPLIED <- %s', b.IS_TEMPLATE_APPLIED);
} else {
dbg.enabled && dbg('IS_TEMPLATE_APPLIED -> %s', b.IS_TEMPLATE_APPLIED);
}
return b.IS_TEMPLATE_APPLIED;
}
},
IS_TRACING_ID_PREDEFINED: {
get: function () {
c && d('IS_TRACING_ID_PREDEFINED');
if (b.IS_TRACING_ID_PREDEFINED === null) {
b.IS_TRACING_ID_PREDEFINED = getEnvOpt('LOGOLITE_TRACING_ID_PREDEFINED') === 'true';
}
return b.IS_TRACING_ID_PREDEFINED;
}
},
IS_STRINGIFY_ENABLED: {
get: function () {
c && d('IS_STRINGIFY_ENABLED');
if (b.IS_STRINGIFY_ENABLED === null) {
b.IS_STRINGIFY_ENABLED = getEnvOpt('LOGOLITE_STRINGIFY_DISABLED') !== 'true';
}
return b.IS_STRINGIFY_ENABLED;
}
},
IS_STRINGIFY_PROTECTED: {
get: function () {
c && d('IS_STRINGIFY_PROTECTED');
if (b.IS_STRINGIFY_PROTECTED === null) {
b.IS_STRINGIFY_PROTECTED = getEnvOpt('LOGOLITE_STRINGIFY_PROTECTED') !== 'false';
}
return b.IS_STRINGIFY_PROTECTED;
}
}
};
Object.keys(e).forEach(function (a) {
e[a]['set'] = function (a) {};
});
Object.defineProperties(this, e);
e = undefined;
this.sortLevels = function (a, b) {
b = b || true;
let c = [];
for (let d in a) {
if (a.hasOwnProperty(d)) c.push({
key: d,
value: a[d]
});
}
if (b) {
c.sort(function (c, a) {
return c.value - a.value;
});
} else {
c.sort(function (c, a) {
let b = c.value.toLowerCase(),
d = a.value.toLowerCase();
return b < d ? -1 : b > d ? 1 : 0;
});
}
return c; // array in format [{key:k1, value:v1}, {key:k2, value:v2}, ...]
};
this.get = function (a) {
if (typeof a === 'string') {
a = a.split(',').map(function (a) {
return a.trim();
});
}
if (a instanceof Array) {
if (a.length === 1) {
return b[a[0]];
}
let c = {};
Object.keys(b).forEach(function (d) {
if (a.indexOf(d) >= 0) {
c[d] = b[d];
}
});
return c;
}
};
this.set = function (a) {
a = a || {};
Object.keys(b).forEach(function (c) {
if (c in a) {
b[c] = a[c];
}
});
return this;
};
this.reset = function (a) {
a = a || {};
Object.keys(b).forEach(function (c) {
let d = b[c];
delete b[c];
b[c] = a[c] || null;
dbg.enabled && dbg(' - store[%s]: %s <- %s', c, d, b[c]);
});
return this;
};
this.isAlwaysEnabledFor = function (a) {
if (this.ALWAYS_ENABLED.indexOf('all') >= 0) return true;
return this.ALWAYS_ENABLED.indexOf(a) >= 0;
};
this.isAlwaysMutedFor = function (a) {
if (this.ALWAYS_MUTED.indexOf('all') >= 0) return true;
return this.ALWAYS_MUTED.indexOf(a) >= 0;
};
this.getLogID = function (a) {
if (a && a.uuid) {
return idstream.getId();
}
if (this.USE_BASE64_UUID) {
return idstream.getId('base64');
}
return idstream.getId('uniqid');
};
this.clone = function (a) {
if (a === undefined || a === null) return a;
if (a instanceof Array) return a.slice();
if (typeof a === 'object') return Object.assign({}, a);
return a;
};
this.stringify = function (a) {
if (a === undefined) a = null;
if (typeof a === 'string') return a;
let b = null;
if (this.IS_STRINGIFY_PROTECTED) {
try {
b = JSON.stringify(a);
} catch (a) {
b = JSON.stringify({
safeguard: 'JSON.stringify() error'
});
}
} else {
b = JSON.stringify(a);
}
return b;
};
this.getPackageInfo = function () {
try {
let a = require('app-root-path');
return require(a.resolve('./package.json'));
} catch (a) {
return require('./../package.json');
}
};
let f = null;
Object.defineProperties(this, {
libraryInfo: {
get: function () {
if (f == null) {
let b = a.getPackageInfo();
f = {
message: getEnvOpt('LOGOLITE_INFO_MESSAGE') || 'Application Information',
lib_name: b.name,
lib_version: b.version,
os_name: os.platform(),
os_version: os.release(),
os_arch: os.arch()
};
}
return f;
},
set: function (a) {}
}
});
}
module.exports = new Configuration();
//====================================================================
function parseDebuglogLevels() {
let a;
let b = getEnvOpt('LOGOLITE_DEBUGLOG_GREEDY') || getEnvOpt('LOGOLITE_DEBUGLOG_ABSORB') || getEnvOpt('LOGOLITE_DEBUGLOG_NAMES') || getEnvOpt('LOGOLITE_DEBUGLOG_NAME') || 'conlog';
if (b === 'null' || b === 'none') {
a = [];
} else {
a = stringToArray(b);
if (a.indexOf('conlog') < 0) {
a.push('conlog');
}
}
return a;
}
function stringToArray(a) {
a = a || '';
return a.split(',').map(function (a) {
return a.trim();
});
}