Repository URL to install this package:
Version:
2.0.11-8 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
const cleanObjectForSerialization_1 = require("./cleanObjectForSerialization");
const logLevelOrder = [
"silent",
"error",
"warning",
"info",
"debug",
];
function shouldLogToConsole(args) {
return (logLevelOrder.indexOf(args.logLevel) <=
logLevelOrder.indexOf(args.consoleLogLevel));
}
exports.shouldLogToConsole = shouldLogToConsole;
function toStringifiedLogglyMessage(logglyMessage, jsonStringify) {
let data;
if (typeof logglyMessage.data === "object") {
const { message } = logglyMessage.data;
data = {
message: typeof message === "string" ? message : undefined,
asJSON: jsonStringify(cleanObjectForSerialization_1.cleanObjectForSerialization(logglyMessage.data)),
};
}
else if (typeof logglyMessage.data !== "undefined") {
data = {
asJSON: jsonStringify(cleanObjectForSerialization_1.cleanObjectForSerialization(logglyMessage.data)),
};
}
let error;
if (typeof logglyMessage.error === "object") {
const { name, message, stack } = logglyMessage.error;
error = {
name: typeof name === "string" ? name : undefined,
message: typeof message === "string" ? message : undefined,
stack: typeof stack === "string" ? stack : undefined,
asJSON: jsonStringify(cleanObjectForSerialization_1.cleanObjectForSerialization(logglyMessage.error)),
};
}
else if (typeof logglyMessage.error !== "undefined") {
error = {
asJSON: jsonStringify(cleanObjectForSerialization_1.cleanObjectForSerialization(logglyMessage.error)),
};
}
return {
...logglyMessage,
error,
data,
};
}
exports.toStringifiedLogglyMessage = toStringifiedLogglyMessage;
function fromStringifiedLogglyMessage(logglyMessage, jsonParse) {
const { data, error, ...shared } = logglyMessage;
const result = shared;
if (data) {
result.data = jsonParse(data.asJSON);
}
if (error) {
const parsedJSON = error.asJSON ? jsonParse(error.asJSON) : {};
result.error = {
name: error.name,
message: error.message,
stack: error.stack,
...parsedJSON,
};
}
return result;
}
exports.fromStringifiedLogglyMessage = fromStringifiedLogglyMessage;
function isStringifiedLogglyMessage(message) {
return ((typeof message.data === "object" &&
typeof message.data.asJSON === "string") ||
(typeof message.error === "object" &&
typeof message.error.asJSON === "string"));
}
exports.isStringifiedLogglyMessage = isStringifiedLogglyMessage;
class Loggly {
constructor(args) {
this.args = args;
this.logglyData = {};
this.rateLimitedLog = _.throttle(this.log.bind(this), 500);
this.queue = [];
this.flushing = false;
this.flush = async () => {
if (!this.flushing && this.queue.length > 0) {
this.flushing = true;
const data = this.queue.splice(0, this.queue.length);
const body = data
.map(item => JSON.stringify({
...cleanObjectForSerialization_1.cleanObjectForSerialization(item),
...this.logglyData,
...this.deviceInfo,
}))
.join("\n");
try {
const response = await fetch(this.url, {
method: "post",
headers: { "content-type": "text/plain" },
body: body,
});
if (response.status !== 200) {
this.args.logger.log("Could not reach loggly!", response.status, response.statusText, await response.text());
}
this.flushing = false;
}
catch (error) {
this.flushing = false;
this.queue.splice(0, 0, ...data);
setTimeout(this.flush, 1000 * 60);
}
}
};
const { token, platform, env } = args;
const tag = `${platform}-${env}`;
this.url = `http://logs-01.loggly.com/bulk/${token}/tag/${tag}/`;
this.deviceInfo = {
os: args.os,
platform: args.platform,
};
}
async log(message) {
this.args.logger.log(message);
if (this.args.env !== "local") {
this.queue.push(message);
await this.flush();
}
}
}
exports.Loggly = Loggly;
//# sourceMappingURL=logglyHelpers.js.map