mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 22:06:27 +00:00
80 lines
2.0 KiB
JavaScript
80 lines
2.0 KiB
JavaScript
const winston = require("winston");
|
|
const { combine, timestamp, printf, colorize, align, json } = winston.format;
|
|
|
|
const logLevels = {
|
|
fatal: 0,
|
|
error: 1,
|
|
warn: 2,
|
|
info: 3,
|
|
debug: 4,
|
|
trace: 5,
|
|
};
|
|
|
|
const logger = winston.createLogger({
|
|
levels: logLevels,
|
|
level: process.env.LOG_LEVEL || "info",
|
|
// format: combine(timestamp(), json()),
|
|
format: combine(
|
|
// colorize({ all: true }),
|
|
timestamp({
|
|
format: "YYYY-MM-DD HH:mm:ss.SSS",
|
|
}),
|
|
align(),
|
|
printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
|
|
),
|
|
defaultMeta: { service: "topuponline-service" },
|
|
transports: [
|
|
//
|
|
// - Write all logs with importance level of `error` or less to `error.log`
|
|
// - Write all logs with importance level of `info` or less to `combined.log`
|
|
//
|
|
new winston.transports.File({
|
|
filename: "application/logs/error.log",
|
|
level: "error",
|
|
}),
|
|
new winston.transports.File({
|
|
filename: "application/logs/debug.log",
|
|
level: "debug",
|
|
}),
|
|
new winston.transports.File({
|
|
filename: "application/logs/trace.log",
|
|
level: "info",
|
|
}),
|
|
new winston.transports.File({ filename: "application/logs/combined.log" }),
|
|
],
|
|
});
|
|
|
|
//
|
|
// Replaces the previous transports with those in the
|
|
// new configuration wholesale.
|
|
//
|
|
// const DailyRotateFile = require("winston-daily-rotate-file");
|
|
// logger.configure({
|
|
// level: "verbose",
|
|
// transports: [new DailyRotateFile()],
|
|
// });
|
|
|
|
//
|
|
// If we're not in production then log to the `console` with the format:
|
|
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
|
|
//
|
|
if (process.env.NODE_ENV != "production") {
|
|
logger.add(
|
|
new winston.transports.Console({
|
|
// format: winston.format.cli(),
|
|
format: combine(
|
|
colorize({ all: true }),
|
|
timestamp({
|
|
format: "YYYY-MM-DD HH:mm:ss.SSS",
|
|
}),
|
|
align(),
|
|
printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
|
|
),
|
|
})
|
|
);
|
|
}
|
|
|
|
module.exports = {
|
|
logger,
|
|
};
|