logstack-zee
Version:
Complete Node.js logging solution with 6 integration methods, S3 bidirectional operations, advanced analytics, and multi-cloud storage support for enterprise-scale applications.
123 lines (122 loc) • 4.36 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useExistingCollection = useExistingCollection;
exports.createNewApiLogs = createNewApiLogs;
exports.hybridApproach = hybridApproach;
// Example 1: Using existing collection (your "apilogs" collection)
const index_1 = require("../index");
async function useExistingCollection() {
console.log('🔌 Example 1: Using existing "apilogs" collection');
await (0, index_1.init)({
dbUri: 'mongodb://localhost:27017/your-database-name',
uploadProvider: 'local',
// Configure to use your existing collection
apiLogs: {
existingCollection: {
name: 'apilogs', // Your existing collection name
timestampField: 'request_time', // Field to use for timestamp filtering
requiredFields: {
method: 'method',
path: 'path',
client_ip: 'client_ip',
user_id: 'user_id',
requestBody: 'requestBody',
responseStatus: 'responseStatus'
}
}
},
logging: {
level: 'info',
enableConsole: true
}
});
console.log('✅ Service initialized with existing collection');
}
// Example 2: Creating new API logs using package middleware
const express_1 = __importDefault(require("express"));
const index_2 = require("../index");
async function createNewApiLogs() {
console.log('🆕 Example 2: Creating new API logs collection');
await (0, index_1.init)({
dbUri: 'mongodb://localhost:27017/your-database-name',
uploadProvider: 's3',
s3: {
accessKeyId: 'your-access-key',
secretAccessKey: 'your-secret-key',
region: 'us-east-1',
bucket: 'your-bucket'
},
// Configure to create new collection
apiLogs: {
createNew: {
collectionName: 'new_api_logs', // Optional: custom name
enableMiddleware: true,
autoFields: true
}
},
logging: {
level: 'info',
enableConsole: true
}
});
// Setup Express app with logging middleware
const app = (0, express_1.default)();
// Add the API logging middleware
app.use((0, index_2.createApiLogMiddleware)());
// Your API routes
app.get('/api/users', (req, res) => {
res.json({ users: [] });
});
app.post('/api/users', (req, res) => {
res.json({ message: 'User created' });
});
app.listen(3000, () => {
console.log('🚀 Server running on port 3000 with API logging');
});
}
// Example 3: Hybrid approach - use existing for some operations, create new for others
async function hybridApproach() {
console.log('🔄 Example 3: Hybrid approach');
await (0, index_1.init)({
dbUri: 'mongodb://localhost:27017/your-database-name',
uploadProvider: 'gcs',
gcs: {
projectId: 'your-project-id',
keyFilename: 'path/to/service-account.json',
bucket: 'your-bucket'
},
// Primary source: existing collection
apiLogs: {
existingCollection: {
name: 'apilogs',
timestampField: 'request_time'
},
// Also enable new collection for additional logging
createNew: {
collectionName: 'supplementary_logs',
enableMiddleware: true
}
}
});
console.log('✅ Service initialized with hybrid approach');
}
// Run example if this file is executed directly
if (require.main === module) {
const example = process.argv[2] || 'existing';
switch (example) {
case 'existing':
useExistingCollection().catch(console.error);
break;
case 'new':
createNewApiLogs().catch(console.error);
break;
case 'hybrid':
hybridApproach().catch(console.error);
break;
default:
console.log('Usage: npm run example:flexible [existing|new|hybrid]');
}
}