Skip to content
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
31 changes: 31 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"globals": "^15.14.0",
"husky": "^9.1.7",
"jest": "^29.7.0",
"jest-mock-extended": "^4.0.0",
"lint-staged": "^15.4.3",
"prettier": "^3.4.2",
"prisma": "^6.4.1",
Expand Down
97 changes: 53 additions & 44 deletions scripts/test-logging.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#!/usr/bin/env ts-node

import "dotenv/config";
import express from 'express';
import { traceIdMiddleware } from '../src/middlewares/traceId.middleware';
import { requestLoggerMiddleware } from '../src/middlewares/requestLogger.middleware';
import { globalLogger, createLogger } from '../src/services/logger.service';
import fs from 'fs';
import path from 'path';
import express from "express";
import { traceIdMiddleware } from "../src/middlewares/traceId.middleware";
import { requestLoggerMiddleware } from "../src/middlewares/requestLogger.middleware";
import {
globalLogger,
createLogger,
} from "../src/modules/shared/application/services/LoggerService";
import fs from "fs";
import path from "path";

// Ensure logs directory exists
const logsDir = path.join(process.cwd(), 'logs');
const logsDir = path.join(process.cwd(), "logs");
if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir, { recursive: true });
}
Expand All @@ -18,67 +21,71 @@ const app = express();
const PORT = 3001; // Use different port to avoid conflicts

// Create a logger for this test
const testLogger = createLogger('LOGGING_TEST');
const testLogger = createLogger("LOGGING_TEST");

// Middleware setup
app.use(express.json());
app.use(traceIdMiddleware);
app.use(requestLoggerMiddleware);

// Test routes
app.get('/', (req, res) => {
testLogger.info('Health check endpoint accessed', req);
res.json({
message: 'Winston logging test server is running!',
app.get("/", (req, res) => {
testLogger.info("Health check endpoint accessed", req);
res.json({
message: "Winston logging test server is running!",
traceId: req.traceId,
timestamp: new Date().toISOString()
timestamp: new Date().toISOString(),
});
});

app.post('/test-log', (req, res) => {
testLogger.info('Test log endpoint accessed', req, {
app.post("/test-log", (req, res) => {
testLogger.info("Test log endpoint accessed", req, {
requestBody: req.body,
customData: 'This is a test log entry'
customData: "This is a test log entry",
});
res.json({
message: 'Log entry created successfully',

res.json({
message: "Log entry created successfully",
traceId: req.traceId,
loggedData: req.body
loggedData: req.body,
});
});

app.get('/test-error', (req, res) => {
const testError = new Error('This is a test error for logging');
testLogger.error('Test error occurred', testError, req, {
errorContext: 'Intentional test error',
additionalInfo: 'This error was generated for testing purposes'
app.get("/test-error", (req, res) => {
const testError = new Error("This is a test error for logging");
testLogger.error("Test error occurred", testError, req, {
errorContext: "Intentional test error",
additionalInfo: "This error was generated for testing purposes",
});
res.status(500).json({
error: 'Test error generated',

res.status(500).json({
error: "Test error generated",
traceId: req.traceId,
message: 'Check the logs for error details'
message: "Check the logs for error details",
});
});

app.post('/test-sensitive-data', (req, res) => {
app.post("/test-sensitive-data", (req, res) => {
// This will test our data sanitization
testLogger.info('Testing sensitive data sanitization', req);
res.json({
message: 'Sensitive data logged (check logs for sanitization)',
traceId: req.traceId
testLogger.info("Testing sensitive data sanitization", req);

res.json({
message: "Sensitive data logged (check logs for sanitization)",
traceId: req.traceId,
});
});

// Start the test server
app.listen(PORT, () => {
globalLogger.info(`Winston logging test server started on port ${PORT}`, undefined, {
environment: process.env.NODE_ENV || 'development',
testMode: true
});

globalLogger.info(
`Winston logging test server started on port ${PORT}`,
undefined,
{
environment: process.env.NODE_ENV || "development",
testMode: true,
}
);

console.log(`\nπŸš€ Winston Logging Test Server Started!`);
console.log(`πŸ“ Server running at: http://localhost:${PORT}`);
console.log(`πŸ“ Logs directory: ${logsDir}`);
Expand All @@ -89,7 +96,9 @@ app.listen(PORT, () => {
console.log(` POST /test-sensitive-data - Test data sanitization`);
console.log(`\nπŸ“Š Example requests:`);
console.log(` curl http://localhost:${PORT}/`);
console.log(` curl -X POST http://localhost:${PORT}/test-log -H "Content-Type: application/json" -d '{"username":"test","password":"secret123","data":"normal"}'`);
console.log(
` curl -X POST http://localhost:${PORT}/test-log -H "Content-Type: application/json" -d '{"username":"test","password":"secret123","data":"normal"}'`
);
console.log(` curl http://localhost:${PORT}/test-error`);
console.log(`\nπŸ“ Check the logs directory for output files:`);
console.log(` - logs/combined.log (all logs)`);
Expand All @@ -98,8 +107,8 @@ app.listen(PORT, () => {
});

// Graceful shutdown
process.on('SIGINT', () => {
globalLogger.info('Shutting down Winston logging test server');
console.log('\nπŸ‘‹ Winston logging test server stopped');
process.on("SIGINT", () => {
globalLogger.info("Shutting down Winston logging test server");
console.log("\nπŸ‘‹ Winston logging test server stopped");
process.exit(0);
});
Loading