Skip to content

Commit

Permalink
feat: alternative logger with filename
Browse files Browse the repository at this point in the history
  • Loading branch information
LewisBroadhurst committed Apr 11, 2024
1 parent d7a0c32 commit 3dbf6fa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/logging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,18 @@ const logger = createLogger({
],
});

/**
* @param {string} logLevel
* @param {string} filename
* @param {string} message
*/
function winstonLogger(logLevel, filename, message) {
if (logLevel === 'error') {
logger.error(`${filename}: ${message}`);
} else {
logger.info(`${filename}: ${message}`);
}
}

module.exports.logger = logger;
module.exports.winstonLogger = winstonLogger;
4 changes: 2 additions & 2 deletions src/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const cors = require('cors');
const app = express();
const rateLimit = require('express-rate-limit');
const lusca = require('lusca');
const { logger } = require('../logging/index');
const { winstonLogger } = require('../logging/index');

const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
Expand Down Expand Up @@ -46,7 +46,7 @@ const start = async () => {

await _httpServer.listen(uiPort);

logger.info(`Service Listening on ${uiPort}`);
winstonLogger('info', 'service/index.js', `Service Listening on ${uiPort}`);
app.emit('ready');

return app;
Expand Down
6 changes: 3 additions & 3 deletions src/ui/services/user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-len */
/* eslint-disable require-jsdoc */
import axios from 'axios';
const { logger } = require('../../logging/index');
const { logger, winstonLogger } = require('../../logging/index');
const { GIT_PROXY_UI_PORT: uiPort } = require('../../config/env').Vars;
const baseUrl = `http://localhost:${uiPort}/api/v1`;

Expand All @@ -28,7 +28,7 @@ const getUser = async (
.then((response) => {
const data = response.data;
setData(data);
logger.info(data);
winstonLogger('info', 'user.js', data);
setIsLoading(false);
})
.catch((error) => {
Expand All @@ -45,7 +45,7 @@ const createUser = async (data) => {
.post(url, data, { withCredentials: true })
.then(() => {})
.catch((error) => {
logger.error(error.response.data.message);
winstonLogger('error', 'user.js', error.response.data.message);
throw error;
});
};
Expand Down

0 comments on commit 3dbf6fa

Please sign in to comment.