Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added timestamps to the API consumption logs #3732

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider using native Date methods instead of moment.js

For simple timestamp formatting, native JavaScript methods like new Date().toISOString() or Intl.DateTimeFormat could be more performant alternatives to moment.js, which is a relatively heavy library.

Example implementation:

-const moment = require("moment");
+const formatTimestamp = () => {
+  return new Intl.DateTimeFormat('en-US', {
+    year: 'numeric',
+    month: '2-digit',
+    day: '2-digit',
+    hour: '2-digit',
+    minute: '2-digit',
+    second: '2-digit',
+    hour12: false
+  }).format(new Date()).replace(/(\d+)\/(\d+)\/(\d+)/, '$3-$1-$2');
+};

Committable suggestion was skipped due to low confidence.


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}`);
};
Comment on lines +33 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Align logTextWithTimestamp with existing logging patterns

The new function differs from other logging utilities in three ways:

  1. Missing environment check (logs in production)
  2. No return value
  3. Inconsistent error handling

Suggested implementation:

 const logTextWithTimestamp = (message) => {
-  console.log(`[${moment().format("YYYY-MM-DD HH:mm:ss")}] ${message}`);
+  if (process.env.NODE_ENV !== "production") {
+    console.log(`[${moment().format("YYYY-MM-DD HH:mm:ss")}] ${message}`);
+  }
+  return "log deactivated in prod and stage";
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const logTextWithTimestamp = (message) => {
console.log(`[${moment().format("YYYY-MM-DD HH:mm:ss")}] ${message}`);
};
const logTextWithTimestamp = (message) => {
if (process.env.NODE_ENV !== "production") {
console.log(`[${moment().format("YYYY-MM-DD HH:mm:ss")}] ${message}`);
}
return "log deactivated in prod and stage";
};


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