standardize log statements

This commit is contained in:
Alex Holliday committed 2024-10-24 15:28:13 +08:00
1 parent 6984eb8a70
commit a0248c81c2
12 files changed
+167 -115

No files matched your search

+13 -7
View File
@@ -44,9 +44,11 @@ class EmailService {
const templateContent = this.fs.readFileSync(templatePath, "utf8");
return this.compile(templateContent);
} catch (error) {
this.logger.error("Error loading Email templates", {
error,
service: this.SERVICE_NAME,
this.logger.error({
message: error.message,
service: SERVICE_NAME,
method: "loadTemplate",
stack: error.stack,
});
}
};
@@ -102,9 +104,11 @@ class EmailService {
const html = await this.mjml2html(mjml);
return html.html;
} catch (error) {
this.logger.error("Error building Email HTML", {
error,
this.logger.error({
message: error.message,
service: SERVICE_NAME,
method: "buildAndSendEmail",
stack: error.stack,
});
}
};
@@ -118,9 +122,11 @@ class EmailService {
});
return info;
} catch (error) {
this.logger.error("Error sending Email", {
error,
this.logger.error({
message: error.message,
service: SERVICE_NAME,
method: "sendEmail",
stack: error.stack,
});
}
};
+34 -24
View File
@@ -107,16 +107,19 @@ class JobQueue {
if (!maintenanceWindowActive) {
await this.networkService.getStatus(job);
} else {
this.logger.info(`Monitor ${monitorId} is in maintenance window`, {
this.logger.info({
message: `Monitor ${monitorId} is in maintenance window`,
service: SERVICE_NAME,
monitorId,
method: "createWorker",
});
}
} catch (error) {
this.logger.error(`Error processing job ${job.id}: ${error.message}`, {
this.logger.error({
message: error.message,
service: SERVICE_NAME,
jobId: job.id,
error: error,
method: "createWorker",
details: `Error processing job ${job.id}: ${error.message}`,
stack: error.stack,
});
}
},
@@ -197,14 +200,15 @@ class JobQueue {
while (workersToRemove > 0 && this.workers.length > 5) {
const worker = this.workers.pop();
workersToRemove--;
try {
await worker.close();
} catch (error) {
await worker.close().catch((error) => {
// Catch the error instead of throwing it
this.logger.error(errorMessages.JOB_QUEUE_WORKER_CLOSE, {
this.logger.error({
message: error.message,
service: SERVICE_NAME,
method: "scaleWorkers",
stack: error.stack,
});
}
});
}
return true;
}
@@ -282,20 +286,24 @@ class JobQueue {
*/
async deleteJob(monitor) {
try {
const deleted = await this.queue.removeRepeatable(monitor._id, {
const wasDeleted = await this.queue.removeRepeatable(monitor._id, {
every: monitor.interval,
});
if (deleted) {
this.logger.info(successMessages.JOB_QUEUE_DELETE_JOB, {
if (wasDeleted === true) {
this.logger.info({
message: successMessages.JOB_QUEUE_DELETE_JOB,
service: SERVICE_NAME,
jobId: monitor.id,
method: "deleteJob",
details: `Deleted job ${monitor._id}`,
});
const workerStats = await this.getWorkerStats();
await this.scaleWorkers(workerStats);
} else {
this.logger.error(errorMessages.JOB_QUEUE_DELETE_JOB, {
this.logger.error({
message: errorMessages.JOB_QUEUE_DELETE_JOB,
service: SERVICE_NAME,
jobId: monitor.id,
method: "deleteJob",
details: `Failed to delete job ${monitor._id}`,
});
}
} catch (error) {
@@ -315,12 +323,16 @@ class JobQueue {
delayed: await this.queue.getDelayedCount(),
repeatableJobs: (await this.queue.getRepeatableJobs()).length,
};
console.log(metrics);
this.logger.info({
message: metrics,
});
return metrics;
} catch (error) {
this.logger.error("Failed to retrieve job queue metrics", {
this.logger.error({
message: error.message,
service: SERVICE_NAME,
errorMsg: error.message,
method: "getMetrics",
stack: error.stack,
});
}
}
@@ -332,7 +344,7 @@ class JobQueue {
async obliterate() {
try {
let metrics = await this.getMetrics();
console.log(metrics);
this.logger.info({ message: metrics });
await this.queue.pause();
const jobs = await this.getJobs();
@@ -348,10 +360,8 @@ class JobQueue {
await this.queue.obliterate();
metrics = await this.getMetrics();
console.log(metrics);
this.logger.info(successMessages.JOB_QUEUE_OBLITERATE, {
service: SERVICE_NAME,
});
this.logger.info({ message: metrics });
this.logger.info({ message: successMessages.JOB_QUEUE_OBLITERATE });
return true;
} catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null;
+24 -19
View File
@@ -56,10 +56,12 @@ class NetworkService {
}
}
} catch (error) {
this.logger.error(error.message, {
method: "handleNotification",
this.logger.error({
message: error.message,
service: this.SERVICE_NAME,
monitorId: monitor._id,
method: "handleNotification",
details: `notification error for monitor: ${monitor._id}`,
stack: error.stack,
});
}
}
@@ -79,19 +81,18 @@ class NetworkService {
try {
monitor = await this.db.getMonitorById(_id);
} catch (error) {
this.logger.error({
message: error.message,
service: this.SERVICE_NAME,
method: "handleStatusUpdate",
stack: error.stack,
details: `monitor lookup error for monitor: ${_id}`,
});
return;
}
// Otherwise, try to update monitor status
try {
if (monitor === null || monitor === undefined) {
this.logger.error(`Null Monitor: ${_id}`, {
method: "handleStatusUpdate",
service: this.SERVICE_NAME,
jobId: job.id,
});
return;
}
if (monitor.status === undefined || monitor.status !== isAlive) {
const oldStatus = monitor.status;
monitor.status = isAlive;
@@ -102,10 +103,12 @@ class NetworkService {
}
}
} catch (error) {
this.logger.error(error.message, {
method: "handleStatusUpdate",
this.logger.error({
message: error.message,
service: this.SERVICE_NAME,
jobId: job.id,
method: "handleStatusUpdate",
stack: error.stack,
details: `status update error for monitor: ${_id}`,
});
}
}
@@ -391,11 +394,12 @@ class NetworkService {
case this.TYPE_HARDWARE:
return await this.handleHardware(job);
default:
this.logger.error(`Unsupported type: ${job.data.type}`, {
this.logger.error({
message: `Unsupported type: ${job.data.type}`,
service: this.SERVICE_NAME,
method: "getStatus",
jobId: job.id,
});
return false;
}
}
@@ -417,11 +421,12 @@ class NetworkService {
}
throw new Error();
} catch (error) {
this.logger.error(`Error writing check for ${data.monitorId}`, {
this.logger.error({
message: error.message,
service: this.SERVICE_NAME,
method: "logAndStoreCheck",
monitorId: data.monitorId,
error: error,
details: `Error writing check for ${data.monitorId}`,
stack: error.stack,
});
}
}