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

winston.add;

const fmt = printf(({ timestamp, level, message, label, durationMs }) => {
	return `${timestamp} [${label || "misc"}] ${message} ${
		!!durationMs ? " (took " + durationMs + "ms)" : ""
	}`;
});

const cfmt = combine(json(), timestamp(), fmt);

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

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

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

export const log = logger;