mcp-quiz-server
Version:
🧠AI-Powered Quiz Management via Model Context Protocol (MCP) - Create, manage, and take quizzes directly from VS Code, Claude, and other AI agents.
142 lines (141 loc) • 5.65 kB
JavaScript
;
/**
* @moduleName: Logging Configuration
* @version: 1.0.0
* @since: 2025-07-24
* @lastUpdated: 2025-07-27
* @projectSummary: Comprehensive logging configuration with multiple providers including Google Cloud Logging
* @techStack: Node.js, Winston, Google Cloud Logging, TypeScript
* @dependency: winston, @google-cloud/logging, dotenv
* @interModuleDependency: audit-logger.ts, error-handler.ts
* @requirementsTraceability:
* {@link Requirements.REQ_COMPLIANCE_001} (Security Audit Logging)
* {@link Requirements.REQ_COMPLIANCE_002} (Data Privacy Compliance)
* {@link Requirements.REQ_COMPLIANCE_003} (Audit Trail Management)
* {@link Requirements.REQ_CONFIG_002} (Configurable Logging)
* @briefDescription: Configurable logging system supporting console, file, and Google Cloud Logging
* @methods: createLogger, configureGoogleCloudLogging, configureFileLogging
* @contributors: GitHub Copilot
* @examples: const logger = createAuditLogger(config)
* @vulnerabilitiesAssessment: Secure logging with PII filtering, credential protection
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultLoggingConfig = void 0;
exports.getEnvironmentLoggingConfig = getEnvironmentLoggingConfig;
exports.validateLoggingConfig = validateLoggingConfig;
exports.defaultLoggingConfig = {
enabled: true,
level: process.env.LOG_LEVEL || 'info',
format: 'json',
auditEnabled: process.env.AUDIT_LOGGING_ENABLED === 'true',
includeRequestBody: process.env.AUDIT_INCLUDE_REQUEST_BODY === 'true',
includeResponseBody: process.env.AUDIT_INCLUDE_RESPONSE_BODY === 'true',
maxRequestBodySize: parseInt(process.env.MAX_REQUEST_BODY_LOG_SIZE || '1024'),
console: {
enabled: true,
colorize: process.env.NODE_ENV === 'development',
},
file: {
enabled: process.env.FILE_LOGGING_ENABLED === 'true',
filename: process.env.LOG_FILE_PATH || './logs/application.log',
maxsize: parseInt(process.env.LOG_FILE_MAX_SIZE || '10485760'), // 10MB
maxFiles: parseInt(process.env.LOG_FILE_MAX_FILES || '5'),
auditFilename: process.env.AUDIT_LOG_FILE_PATH || './logs/audit.log',
},
googleCloud: {
enabled: process.env.GOOGLE_CLOUD_LOGGING_ENABLED === 'true',
projectId: process.env.GOOGLE_CLOUD_PROJECT_ID,
keyFilename: process.env.GOOGLE_CLOUD_KEY_FILE,
logName: process.env.GOOGLE_CLOUD_LOG_NAME || 'mcp-quiz-server',
auditLogName: process.env.GOOGLE_CLOUD_AUDIT_LOG_NAME || 'mcp-quiz-server-audit',
resourceType: 'generic_node',
resourceLabels: {
project_id: process.env.GOOGLE_CLOUD_PROJECT_ID || 'mcp-quiz-server',
location: process.env.DEPLOYMENT_REGION || 'unknown',
namespace: process.env.DEPLOYMENT_NAMESPACE || 'default',
},
},
piiFiltering: {
enabled: true,
fields: [
'password',
'token',
'secret',
'key',
'authorization',
'cookie',
'email', // depending on compliance requirements
'phone',
'ssn',
'credit_card',
],
},
sampling: {
enabled: process.env.LOG_SAMPLING_ENABLED === 'true',
rate: parseFloat(process.env.LOG_SAMPLING_RATE || '1.0'),
},
};
// Environment-specific overrides
function getEnvironmentLoggingConfig() {
const baseConfig = { ...exports.defaultLoggingConfig };
switch (process.env.NODE_ENV) {
case 'development':
return {
...baseConfig,
level: 'debug',
console: { enabled: true, colorize: true },
file: { ...baseConfig.file, enabled: false },
googleCloud: { ...baseConfig.googleCloud, enabled: false },
auditEnabled: false,
};
case 'test':
return {
...baseConfig,
level: 'error',
console: { enabled: false, colorize: false },
file: { ...baseConfig.file, enabled: false },
googleCloud: { ...baseConfig.googleCloud, enabled: false },
auditEnabled: false,
};
case 'production':
return {
...baseConfig,
level: 'info',
console: { enabled: true, colorize: false },
file: { ...baseConfig.file, enabled: true },
googleCloud: {
...baseConfig.googleCloud,
enabled: !!process.env.GOOGLE_CLOUD_PROJECT_ID,
},
auditEnabled: true,
};
default:
return baseConfig;
}
}
// Validation function
function validateLoggingConfig(config) {
const errors = [];
if (config.googleCloud.enabled) {
if (!config.googleCloud.projectId) {
errors.push('Google Cloud Project ID is required when Google Cloud Logging is enabled');
}
if (!config.googleCloud.logName) {
errors.push('Google Cloud Log Name is required');
}
}
if (config.file.enabled) {
if (!config.file.filename) {
errors.push('Log filename is required when file logging is enabled');
}
if (config.file.maxsize <= 0) {
errors.push('Log file max size must be positive');
}
}
if (config.sampling.enabled) {
if (config.sampling.rate < 0 || config.sampling.rate > 1) {
errors.push('Sampling rate must be between 0.0 and 1.0');
}
}
return errors;
}