/**
 * @description: 日志存储
 */
const log4js = require('log4js');

const DingRobot = require('./dingRobot').DingRobot;

const robot = new DingRobot({ atMobiles: ['13777436732'] });


// log4js 配置
log4js.configure({
    appenders: {
        out: { type: 'stdout' },
        err: {
            type: 'dateFile',
            filename: 'logs/errlog/err',
            pattern: 'yyyy-MM-dd.log',
            alwaysIncludePattern: true,
            compress: true,
            backups: 7,
        },
        file: {
            type: 'dateFile',
            filename: 'logs/reqlog/req',
            pattern: 'yyyy-MM-dd.log',
            alwaysIncludePattern: true,
            compress: true,
            backups: 7,
        }
    },
    categories: {
        default: { appenders: ['out', 'file'], level: 'info' },
        err: { appenders: ['err'], level: 'error' },
    }
})

 class Logger {
    constructor(category) {
        this.logger = log4js.getLogger(category);
    }

    debug(content) {
        this.logger.debug(content)
    }

    info(content) {
        this.logger.info(content)
    }

    error(content) {
        this.logger.error(content);
        robot.send(content);
        //同时写入单独的错误日志文件
        const errLogger = log4js.getLogger('err');
        errLogger.error(`[ ${this.logger.category} ] ${JSON.stringify(content)}`);
    }
}

exports.Logger = Logger;