create-arktos
Version:
🚀 A modern Node.js backend boilerplate with TypeScript, Express, JWT authentication, Prisma ORM, PostgreSQL, and Resend email service. Includes complete authentication flow, security middleware, and database management.
76 lines (67 loc) • 1.96 kB
text/typescript
import winston from 'winston';
import path from 'path';
const logLevel = process.env.LOG_LEVEL || 'info';
const logFile = process.env.LOG_FILE || 'logs/app.log';
// Ensure logs directory exists
import fs from 'fs';
const logDir = path.dirname(logFile);
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true });
}
const logFormat = winston.format.combine(
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
winston.format.errors({ stack: true }),
winston.format.json(),
winston.format.prettyPrint()
);
const consoleFormat = winston.format.combine(
winston.format.colorize(),
winston.format.timestamp({
format: 'HH:mm:ss',
}),
winston.format.printf(({ timestamp, level, message, ...meta }) => {
return `${timestamp} [${level}]: ${message} ${Object.keys(meta).length ? JSON.stringify(meta, null, 2) : ''}`;
})
);
const logger = winston.createLogger({
level: logLevel,
format: logFormat,
defaultMeta: { service: 'arktos-backend' },
transports: [
// File transport
new winston.transports.File({
filename: logFile,
handleExceptions: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
}),
// Error file transport
new winston.transports.File({
filename: path.join(logDir, 'error.log'),
level: 'error',
handleExceptions: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
}),
],
});
// Console transport for non-production environments
if (process.env.NODE_ENV !== 'production') {
logger.add(
new winston.transports.Console({
format: consoleFormat,
handleExceptions: true,
})
);
}
// Handle uncaught exceptions and unhandled rejections
process.on('uncaughtException', (error) => {
logger.error('Uncaught Exception:', error);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
export default logger;