Core Values (TRA-VAL-0001): - Adapt STR-VAL-0001 for Tractatus AI Safety Framework - Define 6 core values: Sovereignty, Transparency, Harmlessness, Human Judgment Primacy, Community, Biodiversity - Establish AI governance principles and decision framework - Document Te Tiriti commitment as strategic baseline - Create values alignment metrics and review process Database Utilities: - MongoDB connection with retry logic and health checks - Singleton pattern for connection management - Comprehensive error handling and reconnection Logger Utility: - Winston-based logging (console + file) - Request logging middleware - Error log separation - Configurable log levels JWT Utility: - Token generation and verification - Secure admin authentication - Header extraction methods Markdown Utility: - Markdown to HTML conversion with syntax highlighting - XSS protection via sanitization - Table of contents extraction - Front matter parsing - Slug generation Status: Core infrastructure utilities complete
73 lines
1.8 KiB
JavaScript
73 lines
1.8 KiB
JavaScript
/**
|
|
* Logger Utility
|
|
* Winston-based logging with file and console output
|
|
*/
|
|
|
|
const winston = require('winston');
|
|
const path = require('path');
|
|
|
|
const logLevel = process.env.LOG_LEVEL || 'info';
|
|
const logFile = process.env.LOG_FILE || path.join(__dirname, '../../logs/app.log');
|
|
|
|
// Define log format
|
|
const logFormat = winston.format.combine(
|
|
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
|
winston.format.errors({ stack: true }),
|
|
winston.format.printf(({ timestamp, level, message, stack }) => {
|
|
return stack
|
|
? `${timestamp} [${level.toUpperCase()}]: ${message}\n${stack}`
|
|
: `${timestamp} [${level.toUpperCase()}]: ${message}`;
|
|
})
|
|
);
|
|
|
|
// Create logger instance
|
|
const logger = winston.createLogger({
|
|
level: logLevel,
|
|
format: logFormat,
|
|
transports: [
|
|
// Console output (colored in development)
|
|
new winston.transports.Console({
|
|
format: winston.format.combine(
|
|
winston.format.colorize(),
|
|
logFormat
|
|
)
|
|
}),
|
|
|
|
// File output
|
|
new winston.transports.File({
|
|
filename: logFile,
|
|
maxsize: 5242880, // 5MB
|
|
maxFiles: 5,
|
|
}),
|
|
|
|
// Error file
|
|
new winston.transports.File({
|
|
filename: path.join(path.dirname(logFile), 'error.log'),
|
|
level: 'error',
|
|
maxsize: 5242880,
|
|
maxFiles: 5,
|
|
})
|
|
]
|
|
});
|
|
|
|
// Add request logging method
|
|
logger.request = (req, res, next) => {
|
|
const start = Date.now();
|
|
|
|
res.on('finish', () => {
|
|
const duration = Date.now() - start;
|
|
const message = `${req.method} ${req.originalUrl} ${res.statusCode} - ${duration}ms`;
|
|
|
|
if (res.statusCode >= 500) {
|
|
logger.error(message);
|
|
} else if (res.statusCode >= 400) {
|
|
logger.warn(message);
|
|
} else {
|
|
logger.info(message);
|
|
}
|
|
});
|
|
|
|
next();
|
|
};
|
|
|
|
module.exports = logger;
|