add logger
- User
add logger
Assistant
Sure, let's add a logger to both functions. I'll use
winstonas the logger, but you can replace it with any logger of your choice.First, install
winstonif you haven't already:npm install winstonThen, create a
logger.jsfile to configure the logger:// logger.js const winston = require('winston'); const logger = winston.createLogger({ level: 'info', format: winston.format.combine( winston.format.timestamp(), winston.format.printf(({ timestamp, level, message }) => { return `${timestamp} [${level.toUpperCase()}]: ${message}`; }) ), transports: [ new winston.transports.Console(), // You can add more transports like File, etc. ], }); module.exports = logger;Now, let's modify your functions to use this logger:
const logger = require('./logger'); // Adjust the path as needed export const getAllPayins = async (req, res) => { try { const { _id: userId, role } = req.user; const { page = 1, limit = 10 } = req.query; const currentPage = parseInt(page, 10); const perPage = parseInt(limit, 10); if (currentPage <= 0 || perPage <= 0) { logger.warn("Invalid pagination parameters. Page and limit must be greater than zero."); return res.status(400).json({ success: false, message: "Invalid pagination parameters. Page and limit must be greater than zero.", }); } const skip = (currentPage - 1) * perPage; let filters = role === "admin" ? { deleted: false } : { userId, deleted: false }; const [payins, totalCount] = await Promise.all([ PayinModel.find(filters) .populate('userId') .skip(skip) .limit(perPage) .sort({ createdAt: -1 }), PayinModel.countDocuments(filters), ]); const totalPages = Math.ceil(totalCount / perPage); logger.info(`Fetched ${payins.length} payins for user ${userId}`); return res.status(200).json({ success: true, message: payins.length === 0 ? "No Payins found." : "Payins fetched successfully.", pagination: { currentPage, totalPages, perPageItems: perPage, totalItems: totalCount, }, data: payins, }); } catch (error) { logger.error(`GET PAYIN ERROR: ${error.message}`); return res.status(500).json({ success: false, message: `Server error while fetching payin: ${error.message}`, }); } }; export const getPayinById = async (req, res) => { try { const { _id: userId } = req.user; const { id } = req.params; if (!id) { logger.warn("Payin ID is required."); return res.status(400).json({ success: false, message: "Payin ID is required.", }); } const payin = await PayinModel.findOne({ _id: id, userId, deleted: false, }) .populate('userId'); if (!payin) { logger.warn(`Payin not found for ID: ${id}`); return res.status(404).json({ success: false, message: "Payin not found.", }); } logger.info(`Fetched payin with ID: ${id} for user ${userId}`); return res.status(200).json({ success: true, message: "Payin fetched successfully.", data: payin, }); } catch (error) { logger.error(`GET PAYIN BY ID ERROR: ${error.message}`); return res.status(500).json({ success: false, message: `Server error while fetching payin: ${error.message}`, }); } };In this example, I've added
logger.warnfor warnings andlogger.errorfor errors. You can adjust the logging levels as needed.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.