Rewrite logger

This commit is contained in:
Alex Holliday committed 2024-10-24 15:27:34 +08:00
1 parent 0649002d27
commit 6984eb8a70
1 file changed
+89 -21
+89 -21
View File
@@ -1,23 +1,91 @@
import winston from "winston"; import { createLogger, format, transports } from "winston";
/**
* @module class Logger {
* @example constructor() {
* logger.info("Registered a new user!") const consoleFormat = format.printf(
* logger.warn("User not found!") ({ level, message, service, method, details, timestamp }) => {
* logger.error("Cannot save") if (message instanceof Object) {
* @example message = JSON.stringify(message, null, 2);
* "Specify service and ID in the log if applicable." }
* logger.error("Descriptive Message",{"service":"monitor","monitorId":"123456"}) let msg = `${timestamp} ${level}:`;
* logger.error("Incorrect Credentials",{"service":"Auth","userId":"654321"}) service && (msg += ` [${service}]`);
* logger.error("User not found!",{"service":"Auth"}) method && (msg += `(${method})`);
*/ message && (msg += ` ${message}`);
const logger = winston.createLogger({ details && (msg += ` (details: ${details})`);
level: "info", return msg;
format: winston.format.combine(winston.format.timestamp(), winston.format.json()), }
transports: [ );
new winston.transports.Console(),
new winston.transports.File({ filename: "app.log" }), this.logger = createLogger({
], level: "info",
}); format: format.combine(format.timestamp()),
transports: [
new transports.Console({
format: format.combine(
format.colorize(),
format.prettyPrint(),
format.json(),
consoleFormat
),
}),
new transports.File({
format: format.combine(format.json()),
filename: "app.log",
}),
],
});
}
/**
* Logs an informational message.
* @param {Object} config - The configuration object.
* @param {string} config.message - The message to log.
* @param {string} config.service - The service name.
* @param {string} config.method - The method name.
* @param {Object} config.details - Additional details.
*/
info(config) {
this.logger.info(config.message, {
service: config.service,
method: config.method,
details: config.details,
});
}
/**
* Logs a warning message.
* @param {Object} config - The configuration object.
* @param {string} config.message - The message to log.
* @param {string} config.service - The service name.
* @param {string} config.method - The method name.
* @param {Object} config.details - Additional details.
*/
warn(config) {
this.logger.warn(config.message, {
service: config.service,
method: config.method,
details: config.details,
});
}
/**
* Logs an error message.
* @param {Object} config - The configuration object.
* @param {string} config.message - The message to log.
* @param {string} config.service - The service name.
* @param {string} config.method - The method name.
* @param {Object} config.details - Additional details.
*/
error(config) {
this.logger.error(config.message, {
service: config.service,
method: config.method,
details: config.details,
stack: config.stack,
});
}
}
const logger = new Logger();
export { Logger };
export default logger; export default logger;