@aws-lambda-powertools/logger
Version:
The logging package for the Powertools for AWS Lambda (TypeScript) library
81 lines (80 loc) • 2.6 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogItem = void 0;
const lodash_merge_1 = __importDefault(require("lodash.merge"));
/**
* LogItem is a class that holds the attributes of a log item.
*
* It is used by {@link LogFormatter} to store the attributes of a log item and to add additional attributes to it.
*/
class LogItem {
/**
* The attributes of the log item.
*/
attributes = {};
/**
* Constructor for LogItem.
*
* Attributes are added in the following order:
* - Standard keys provided by the logger (e.g. `message`, `level`, `timestamp`)
* - Persistent attributes provided by developer, not formatted (done later)
* - Ephemeral attributes provided as parameters for a single log item (done later)
*
* @param params - The parameters for the LogItem.
*/
constructor(params) {
this.setAttributes(params.attributes);
}
/**
* Add attributes to the log item.
*
* @param attributes - The attributes to add to the log item.
*/
addAttributes(attributes) {
(0, lodash_merge_1.default)(this.attributes, attributes);
return this;
}
/**
* Get the attributes of the log item.
*/
getAttributes() {
return this.attributes;
}
/**
* Prepare the log item for printing.
*
* This operation removes empty keys from the log item, see {@link removeEmptyKeys | removeEmptyKeys()} for more information.
*/
prepareForPrint() {
this.attributes = this.removeEmptyKeys(this.getAttributes());
}
/**
* Remove empty keys from the log item, where empty keys are defined as keys with
* values of `undefined`, empty strings (`''`), or `null`.
*
* @param attributes - The attributes to remove empty keys from.
*/
removeEmptyKeys(attributes) {
const newAttributes = {};
for (const key in attributes) {
if (attributes[key] !== undefined &&
attributes[key] !== '' &&
attributes[key] !== null) {
newAttributes[key] = attributes[key];
}
}
return newAttributes;
}
/**
* Replace the attributes of the log item.
*
* @param attributes - The attributes to set for the log item.
*/
setAttributes(attributes) {
this.attributes = attributes;
}
}
exports.LogItem = LogItem;
;