- Updated `next.config.js` in both web-admin and web-client to enable React strict mode, set output to standalone, and expose environment variables for API URL. - Enhanced `auth-sdk` with detailed comments for JWT verification, decoding, and token management functions. - Improved `http-client` with comprehensive documentation for HTTP client configuration and methods. - Expanded `logger` functionality with detailed configuration options and logging formats. - Enhanced `tracing` setup with detailed comments for distributed tracing configuration. - Updated `types` definitions for authentication and user data transfer objects with comprehensive comments. - Improved `auth-service` with detailed comments in controllers, services, and middlewares for better clarity and maintainability. - Added health check endpoints in `health.controller.ts` for service monitoring. - Enhanced error handling and logging throughout the application for better debugging and user feedback.
110 lines
3.7 KiB
TypeScript
110 lines
3.7 KiB
TypeScript
import winston from 'winston';
|
|
|
|
/**
|
|
* EN: Configuration interface for logger setup
|
|
* VI: Interface cấu hình cho việc thiết lập logger
|
|
*/
|
|
export interface LoggerConfig {
|
|
/** EN: Logging level (error, warn, info, debug) / VI: Mức độ ghi log (error, warn, info, debug) */
|
|
level?: string;
|
|
/** EN: Service name for log identification / VI: Tên service để xác định log */
|
|
serviceName?: string;
|
|
/** EN: Enable console output / VI: Bật output console */
|
|
enableConsole?: boolean;
|
|
/** EN: Enable file output / VI: Bật output file */
|
|
enableFile?: boolean;
|
|
/** EN: Directory for log files / VI: Thư mục cho các file log */
|
|
logDir?: string;
|
|
}
|
|
|
|
/**
|
|
* EN: Default logger configuration with environment variable fallbacks
|
|
* VI: Cấu hình logger mặc định với fallback từ biến môi trường
|
|
*/
|
|
const defaultConfig: Required<LoggerConfig> = {
|
|
level: process.env.LOG_LEVEL || 'info',
|
|
serviceName: process.env.SERVICE_NAME || 'microservice',
|
|
enableConsole: true,
|
|
enableFile: false,
|
|
logDir: './logs',
|
|
};
|
|
|
|
/**
|
|
* EN: Create a Winston logger instance with custom configuration
|
|
* VI: Tạo instance Winston logger với cấu hình tùy chỉnh
|
|
*
|
|
* @param config - Logger configuration options / Tùy chọn cấu hình logger
|
|
* @returns Configured Winston logger instance / Instance Winston logger đã cấu hình
|
|
*/
|
|
export const createLogger = (config: LoggerConfig = {}) => {
|
|
// EN: Merge user config with defaults
|
|
// VI: Hợp nhất config người dùng với mặc định
|
|
const finalConfig = { ...defaultConfig, ...config };
|
|
|
|
// EN: Array to hold Winston transport instances
|
|
// VI: Mảng chứa các instance transport của Winston
|
|
const transports: winston.transport[] = [];
|
|
|
|
// EN: Add console transport if enabled
|
|
// VI: Thêm console transport nếu được bật
|
|
if (finalConfig.enableConsole) {
|
|
transports.push(
|
|
new winston.transports.Console({
|
|
format: winston.format.combine(
|
|
winston.format.timestamp(),
|
|
winston.format.colorize(),
|
|
winston.format.printf(({ timestamp, level, message, service, ...meta }) => {
|
|
// EN: Format log message with timestamp, level, service tag, and metadata
|
|
// VI: Định dạng thông điệp log với timestamp, level, service tag, và metadata
|
|
const serviceTag = service ? `[${service}]` : '';
|
|
const metaStr = Object.keys(meta).length ? JSON.stringify(meta) : '';
|
|
return `${timestamp} ${level} ${serviceTag} ${message} ${metaStr}`;
|
|
})
|
|
),
|
|
})
|
|
);
|
|
}
|
|
|
|
// EN: Add file transports if enabled
|
|
// VI: Thêm file transports nếu được bật
|
|
if (finalConfig.enableFile) {
|
|
transports.push(
|
|
new winston.transports.File({
|
|
filename: `${finalConfig.logDir}/error.log`,
|
|
level: 'error',
|
|
format: winston.format.combine(
|
|
winston.format.timestamp(),
|
|
winston.format.json()
|
|
),
|
|
}),
|
|
new winston.transports.File({
|
|
filename: `${finalConfig.logDir}/combined.log`,
|
|
format: winston.format.combine(
|
|
winston.format.timestamp(),
|
|
winston.format.json()
|
|
),
|
|
})
|
|
);
|
|
}
|
|
|
|
// EN: Create and return Winston logger instance
|
|
// VI: Tạo và trả về instance Winston logger
|
|
return winston.createLogger({
|
|
level: finalConfig.level,
|
|
defaultMeta: {
|
|
service: finalConfig.serviceName,
|
|
},
|
|
transports,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* EN: Default logger instance using default configuration
|
|
* VI: Instance logger mặc định sử dụng cấu hình mặc định
|
|
*/
|
|
export const logger = createLogger();
|
|
|
|
// EN: Default export for convenience
|
|
// VI: Export default để thuận tiện
|
|
export default logger;
|