const pino = require("pino");
const path = require("path");
const logFile = process.env.LOG_FILE || "log.txt";

function pinoLogger() {
    const bytesPerMB = 1024 * 1024;
    const fileBoom = pino.destination(path.join(path.dirname(require.main.filename), logFile));
    const consoleBoom = pino.destination(1);
    return pino({
        level: "info",
        mixin() {
            const usage = process.memoryUsage();
            return {
                rss: usage.rss / bytesPerMB, // Convert to MB
                external: usage.external / bytesPerMB, // Convert to MB
                heapTotal: usage.heapTotal / bytesPerMB, // Convert to MB
                heapUsed: usage.heapUsed / bytesPerMB, // Convert to MB
            }
        }
    }, pino.multistream([fileBoom, consoleBoom]))
}

const logger = pinoLogger();

module.exports = {
    logger
}