// Example: How to integrate the logging client in your service import express from 'express'; import { createLoggingClient } from '../src/client/index.js'; // Initialize logging client const logger = createLoggingClient({ serviceName: 'api-gateway', redisHost: 'localhost', redisPort: 6379, logLevel: 'info' }); // Create Express app const app = express(); // Use logging middleware app.use(logger.expressMiddleware()); // Example route with logging app.get('/api/users/:id', async (req, res) => { try { // Log the operation await logger.info('Fetching user', { userId: req.params.id, requestedBy: req.user?.id }); // Simulate fetching user const user = { id: req.params.id, name: 'John Doe' }; // Send metric await logger.metric('user_fetch_success', 1, { userId: req.params.id }); res.json(user); } catch (error) { // Log error await logger.error(error, { userId: req.params.id, operation: 'user_fetch' }); res.status(500).json({ error: 'Internal server error' }); } }); // Example of logging different levels async function exampleLogging() { // Debug logging await logger.debug('Debug message', { detail: 'some debug info' }); // Info logging await logger.info('User logged in', { userId: 'user123' }); // Warning logging await logger.warn('API rate limit approaching', { userId: 'user123', current: 95, limit: 100 }); // Error logging try { throw new Error('Database connection failed'); } catch (error) { await logger.error(error, { database: 'mongodb', host: 'localhost' }); } // Metrics await logger.metric('api_requests', 1, { endpoint: '/api/users', method: 'GET' }); await logger.metric('response_time', 234.5, { endpoint: '/api/users', method: 'GET' }); await logger.metric('memory_usage', process.memoryUsage().heapUsed / 1024 / 1024, { unit: 'MB' }); } // Run examples exampleLogging(); // Start server app.listen(3000, () => { logger.info('API Gateway started', { port: 3000 }); }); // Graceful shutdown process.on('SIGTERM', async () => { await logger.info('Shutting down service'); await logger.close(); process.exit(0); });