pig-dam-core
Version:
Library that should be included in every Pig DAM project we build
120 lines (119 loc) • 4.18 kB
JavaScript
;
/**
* Date: 2019-07-12
* Time: 21:26
* @license MIT (see project's LICENSE file)
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogBase = void 0;
const _ = require("lodash");
const mutation_1 = require("../mutation");
const types_1 = require("../types");
/**
* This is only a base class implementation. The finer details of how logging is implemented will differ per
* platform. The idea is that this class encapsulates the broad strokes. There will be override points and
* they will clearly be labelled. A few notes:
* - when messages are errors we will attempt to extract useful logging information from them such as
* message, stack and metadata
*/
class LogBase {
constructor({ applicationId, environmentId, sortMetadata = true, threshold = types_1.Severity.DEBUG }) {
this.applicationId = applicationId;
this.environmentId = environmentId;
this.sortMetadata = sortMetadata;
this.threshold = threshold;
}
/********************* Public Interface *********************/
debug(message, { metadata, moduleId, stack, traceId }) {
this._processEntry(message, {
metadata,
moduleId,
severity: types_1.Severity.DEBUG,
stack,
traceId
});
}
error(message, { metadata, moduleId, stack, traceId }) {
this._processEntry(message, {
metadata,
moduleId,
severity: types_1.Severity.ERROR,
stack,
traceId
});
}
fatal(message, { metadata, moduleId, stack, traceId }) {
this._processEntry(message, {
metadata,
moduleId,
severity: types_1.Severity.FATAL,
stack,
traceId
});
}
info(message, { metadata, moduleId, stack, traceId }) {
this._processEntry(message, {
metadata,
moduleId,
severity: types_1.Severity.INFO,
stack,
traceId
});
}
warn(message, { metadata, moduleId, stack, traceId }) {
this._processEntry(message, {
metadata,
moduleId,
severity: types_1.Severity.WARN,
stack,
traceId
});
}
/*********************
* Private Interface
********************/
/**
* Processes the message and forwards it to `_logEntry`
* @private
*/
_processEntry(message, { metadata, moduleId, severity, stack, traceId }) {
if (types_1.testSeverity(severity, this.threshold)) {
if (typeof message === "function") {
message = message();
}
else if (message instanceof Error) {
// The error may not be a PigError be we don't care. We cast it so that TS doesn't bug
// us about referencing PigError properties.
const error = message;
message = error.message;
// We are going to put the whole error into metadata vs. formatting the error. Let's
// see how this works out. One thing I don't like is how stacks get indexed. We may want
// to groom these and include them as an array of strings
metadata = Object.assign({
error
}, error.metadata, metadata);
}
else if (stack) {
// this is unlikely but if its there then let's be consistent
metadata = Object.assign({
error: {
stack
}
}, metadata);
}
this._logEntry(message, severity, _.omitBy({
applicationId: this.applicationId,
environmentId: this.environmentId,
metadata: (this.sortMetadata)
? mutation_1.immutable.object.sort(metadata)
: metadata,
moduleId,
severity,
timestamp: Date.now(),
traceId
}, _.isUndefined));
}
}
}
exports.LogBase = LogBase;