UNPKG

gov_auth_logging_lib

Version:

logging library for cerbos

55 lines (46 loc) 1.44 kB
const express = require('express'); const { DataSource } = require('typeorm'); const { EndpointLogger, LogEntry } = require('express-endpoint-logger'); // Create Express app const app = express(); // Setup database connection const dataSource = new DataSource({ type: "postgres", host: "localhost", port: 5432, username: "your_username", password: "your_password", database: "your_database", entities: [LogEntry], synchronize: true // Only use in development! }); // Initialize everything async function startApp() { // Connect to database await dataSource.initialize(); // Create logger instance const logger = new EndpointLogger({ dataSource, excludePaths: ['/health'], // Optional: paths to not log includeMetadata: true // Optional: include request details }); // Add logger middleware - this will log all requests app.use(logger.logActivity()); // Your regular routes app.get('/api/users', (req, res) => { res.json({ users: ['John', 'Jane'] }); }); // Route to view logs app.get('/logs', async (req, res) => { const logs = await logger.getLogs({ take: 10, // limit skip: 0 // offset }); res.json(logs); }); // Start server app.listen(3000, () => { console.log('Server running on port 3000'); }); } startApp().catch(console.error);