Skip to content

Commit

Permalink
Merge pull request #3732 from airqo-platform/hf-timestamps
Browse files Browse the repository at this point in the history
added timestamps to the API consumption logs
  • Loading branch information
Baalmart authored Oct 23, 2024
2 parents 7986491 + fe659de commit a2bea53
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/device-registry/utils/create-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ const EventModel = require("@models/Event");
const ReadingModel = require("@models/Reading");
const SignalModel = require("@models/Signal");
const DeviceModel = require("@models/Device");
const { logObject, logElement, logText } = require("./log");
const {
logObject,
logElement,
logText,
logTextWithTimestamp,
} = require("./log");
const constants = require("@config/constants");
const generateFilter = require("./generate-filter");
const isEmpty = require("is-empty");
Expand Down Expand Up @@ -2624,7 +2629,7 @@ const createEvent = {
}

if (errors.length > 0 && isEmpty(eventsAdded)) {
console.log(
logTextWithTimestamp(
"API: failed to store measurements, most likely DB cast errors or duplicate records"
);
return {
Expand All @@ -2634,7 +2639,7 @@ const createEvent = {
status: httpStatus.INTERNAL_SERVER_ERROR,
};
} else {
console.log("API: successfully added the events");
logTextWithTimestamp("API: successfully added the events");
return {
success: true,
message: "successfully added the events",
Expand All @@ -2643,6 +2648,7 @@ const createEvent = {
};
}
} catch (error) {
logTextWithTimestamp(`API: Internal Server Error ${error.message}`);
logger.error(`🐛🐛 Internal Server Error ${error.message}`);
next(
new HttpError(
Expand Down
14 changes: 13 additions & 1 deletion src/device-registry/utils/log.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const moment = require("moment");

const logText = (message) => {
if (process.env.NODE_ENV !== "production") {
console.log(message);
Expand Down Expand Up @@ -28,4 +30,14 @@ const logError = (error) => {
return "log deactivated in prod and stage";
};

module.exports = { logText, logElement, logObject, logError };
const logTextWithTimestamp = (message) => {
console.log(`[${moment().format("YYYY-MM-DD HH:mm:ss")}] ${message}`);
};

module.exports = {
logText,
logTextWithTimestamp,
logElement,
logObject,
logError,
};

0 comments on commit a2bea53

Please sign in to comment.