Skip to content

Commit

Permalink
[FEATURE]: refactor codes and add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Aug 18, 2023
1 parent 7c3e1eb commit 3e26df9
Show file tree
Hide file tree
Showing 29 changed files with 760 additions and 458 deletions.
215 changes: 211 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"homepage": "https://github.com/Behzad-rabiei/tc-discordBot#readme",
"dependencies": {
"@sentry/node": "^7.51.2",
"@togethercrew.dev/db": "^2.4.95",
"@togethercrew.dev/db": "^2.4.96",
"@togethercrew.dev/tc-messagebroker": "^0.0.40",
"babel-jest": "^29.5.0",
"bullmq": "^3.14.0",
Expand All @@ -36,6 +36,7 @@
"mongodb": "^5.4.0",
"mongoose": "^6.11.1",
"node-fetch": "^2.6.7",
"pino": "^8.15.0",
"redis": "^4.6.6"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const envVarsSchema = Joi.object()
REDIS_HOST: Joi.string().required().description('Reids host'),
REDIS_PORT: Joi.string().required().description('Reids port'),
REDIS_PASSWORD: Joi.string().required().description('Reids password').allow(''),
LOG_LEVEL: Joi.string().required().description('Min allowed log level'),
})
.unknown();

Expand Down Expand Up @@ -51,4 +52,7 @@ export default {
dsn: envVars.SENTRY_DSN,
env: envVars.SENTRY_ENV,
},
logger: {
level: envVars.LOG_LEVEL,
},
};
18 changes: 18 additions & 0 deletions src/config/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pino, { Bindings } from 'pino';
import config from './index';

export default pino({
level: config.logger.level,
formatters: {
level: label => {
return { level: label.toUpperCase() };
},
},
timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`,
bindings: (bindings: Bindings) => {
return {
pid: bindings.pid,
host: bindings.hostname,
};
},
});
10 changes: 7 additions & 3 deletions src/database/connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Connection } from 'mongoose';
import parentLogger from '../config/logger';

const logger = parentLogger.child({ module: 'Connection' });

/**
* Closes a given Mongoose connection.
* @param {Connection} connection - The Mongoose connection object to be closed.
Expand All @@ -8,8 +12,8 @@ import { Connection } from 'mongoose';
export async function closeConnection(connection: Connection) {
try {
await connection.close();
console.log('The connection to the database has been successfully closed.');
} catch (err) {
console.log('Error closing connection to the database:', err);
logger.info({ database: connection.name }, 'The connection to database has been successfully closed');
} catch (error) {
logger.error({ database: connection.name, error }, 'Failed to close the connection to the database');
}
}
Loading

0 comments on commit 3e26df9

Please sign in to comment.