pino-splunk-events
Version:
Pino transport layer to splunk-events
94 lines (79 loc) • 4.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toSplunkEvent = exports.isSplunkLog = exports.isPinoLog = exports.LOG_LABEL_TO_SPLUNK_SEVERITY = exports.LOG_VALUE_TO_LABEL = void 0;
var _isRecord = require("./utils/type/isRecord");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
const LOG_VALUE_TO_LABEL = {
10: 'trace',
20: 'debug',
30: 'info',
40: 'warn',
50: 'error',
60: 'fatal'
};
exports.LOG_VALUE_TO_LABEL = LOG_VALUE_TO_LABEL;
const LOG_LABEL_TO_SPLUNK_SEVERITY = {
trace: {
level: 'Debug',
type: 'Info'
},
debug: {
level: 'Debug',
type: 'Info'
},
info: {
level: 'Important',
type: 'Info'
},
warn: {
level: 'Important',
type: 'Warn'
},
error: {
level: 'Important',
type: 'Error'
},
fatal: {
level: 'Critical',
type: 'Error'
}
};
exports.LOG_LABEL_TO_SPLUNK_SEVERITY = LOG_LABEL_TO_SPLUNK_SEVERITY;
const isPinoLog = value => {
return (0, _isRecord.isRecord)(value) && typeof value.time === 'number' && typeof value.level === 'number' && value.level in LOG_VALUE_TO_LABEL;
};
exports.isPinoLog = isPinoLog;
const isSplunkLog = value => {
return isPinoLog(value) && (typeof value.msg === 'string' || typeof value.msg === 'undefined') && (typeof value.account === 'string' || typeof value.account === 'undefined') && (typeof value.workflowType === 'string' || typeof value.workflowType === 'undefined') && (typeof value.workflowInstance === 'string' || typeof value.workflowInstance === 'undefined');
};
exports.isSplunkLog = isSplunkLog;
const toSplunkEvent = log => {
const {
level,
time,
account,
workflowInstance,
workflowType
} = log,
eventData = _objectWithoutProperties(log, ["level", "time", "account", "workflowInstance", "workflowType"]);
const levelLabel = LOG_VALUE_TO_LABEL[level];
const severity = LOG_LABEL_TO_SPLUNK_SEVERITY[levelLabel];
return {
severity,
account,
workflow: {
type: workflowType !== null && workflowType !== void 0 ? workflowType : '',
instance: workflowInstance !== null && workflowInstance !== void 0 ? workflowInstance : ''
},
data: _objectSpread({
time: new Date(time).toISOString()
}, eventData)
};
};
exports.toSplunkEvent = toSplunkEvent;