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

feat: add custom LogService with Winston for error logging #3406

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { createLogger, format, transports } = require('winston');
const winstonTimestampColorize = require('winston-timestamp-colorize');

const customFormat = format.combine(
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winstonTimestampColorize(),
format.colorize({all:true}),
format.printf(({ level, timestamp, message, stack, tool_name, url }) => {

let logMessage = `${level} at ${timestamp}`;
logMessage += `\n${message}`;
if (tool_name) {
logMessage += `\n Tool: ${tool_name}`;
}
if (url) {
logMessage += `\n URL: ${url}`;
}
if (stack) {
logMessage += `\n Stack: ${stack}`;
}
Comment on lines +12 to +20
Copy link
Member

Choose a reason for hiding this comment

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

We need to setup custom logger for whole repo, not just for only one particular script

Copy link
Author

Choose a reason for hiding this comment

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

okay @akshatnema it will take some time i will do it in the coming days.Is there anything which i need to take care of from your side.

return logMessage;
}),

);
Comment on lines +4 to +24
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Well-structured logging format with room for improvement

The custom format provides comprehensive logging with timestamps, colors, and structured output. However, there are a few improvements needed:

  1. Follow JavaScript naming conventions by using camelCase:
-format.printf(({ level, timestamp, message, stack, tool_name, url }) => {
+format.printf(({ level, timestamp, message, stack, toolName, url }) => {
   let logMessage = `${level} at ${timestamp}`;
   logMessage += `\n${message}`;
-  if (tool_name) {
-    logMessage += `\n   Tool: ${tool_name}`;
+  if (toolName) {
+    logMessage += `\n   Tool: ${toolName}`;
   }
  1. Remove unnecessary empty line at line 9
📝 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 customFormat = format.combine(
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winstonTimestampColorize(),
format.colorize({all:true}),
format.printf(({ level, timestamp, message, stack, tool_name, url }) => {
let logMessage = `${level} at ${timestamp}`;
logMessage += `\n${message}`;
if (tool_name) {
logMessage += `\n Tool: ${tool_name}`;
}
if (url) {
logMessage += `\n URL: ${url}`;
}
if (stack) {
logMessage += `\n Stack: ${stack}`;
}
return logMessage;
}),
);
const customFormat = format.combine(
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winstonTimestampColorize(),
format.colorize({all:true}),
format.printf(({ level, timestamp, message, stack, toolName, url }) => {
let logMessage = `${level} at ${timestamp}`;
logMessage += `\n${message}`;
if (toolName) {
logMessage += `\n Tool: ${toolName}`;
}
if (url) {
logMessage += `\n URL: ${url}`;
}
if (stack) {
logMessage += `\n Stack: ${stack}`;
}
return logMessage;
}),
);
🧰 Tools
🪛 eslint

[error] 5-5: Delete ·

(prettier/prettier)


[error] 6-6: Delete ·

(prettier/prettier)


[error] 7-7: Replace all:true}),· with ·all:·true·}),

(prettier/prettier)


[error] 8-8: Identifier 'tool_name' is not in camel case.

(camelcase)


[error] 8-9: Delete ⏎··

(prettier/prettier)


[error] 12-12: Delete ··

(prettier/prettier)


[error] 12-12: Identifier 'tool_name' is not in camel case.

(camelcase)


[error] 13-13: Delete ··

(prettier/prettier)


[error] 13-13: Identifier 'tool_name' is not in camel case.

(camelcase)


[error] 14-14: Delete ··

(prettier/prettier)


[error] 15-15: Delete ··

(prettier/prettier)


[error] 16-16: Replace ········ with ······

(prettier/prettier)


[error] 17-17: Delete ··

(prettier/prettier)


[error] 18-18: Delete ··

(prettier/prettier)


[error] 19-19: Delete ··

(prettier/prettier)


[error] 20-20: Delete ··

(prettier/prettier)


[error] 21-21: Delete ··

(prettier/prettier)


[error] 22-23: Delete ,⏎

(prettier/prettier)


const logger = createLogger({
level: 'info',
format: customFormat,
transports: [
new transports.Console({
format: format.combine(customFormat)
}),
new transports.File({ filename: 'logs/error.log', level: 'error' }),
new transports.File({ filename: 'logs/combined.log' }),
],
});

module.exports = logger;
Loading
Loading