import winston from "winston";
const { combine, timestamp, simple, splat, printf, colorize } = winston.format;

winston.add;

const fmt = printf(({ timestamp, level, message, meta }) => {
	return `${timestamp} [${level}] ------ ${message} ${
		!!meta.durationMs ? "\n (took) " + meta.durationMs + "ms" : ""
	}`;
});

const cfmt = combine(colorize(), timestamp(), splat(), fmt);

const loggerTransports: any[] = [
	new winston.transports.Console({
		format: cfmt,
		handleExceptions: true,
		handleRejections: true,
		// json: false
	}),
];

process.env.NODE_ENV?.toLowerCase() == "development" &&
	loggerTransports.push(
		new winston.transports.File({
			filename: "/var/log/rockfic.debug.log",
			level: "debug",
			format: combine(timestamp(), splat(), fmt),
			handleExceptions: true,
			handleRejections: true,
		}),
	);

const logger = winston.createLogger({
	levels: winston.config.syslog.levels,
	transports: loggerTransports,
	format: cfmt,
});

// app.listen(7000, () => logger.debug("fuckyou"))

// console.log(api.stack)

export const log = logger;
