<commit_before>const fs = require("fs");
const zlib = require("zlib");
const path = require("path");
const util = require("util");
const Mail = require("./Mail");

class Logger {
    /**
     * Creates a new instance with a specified filename.
     * 
     * @param {String} filename A file that stores the logs.
     * @param {String} [action] An optional action name.
     */
    constructor(filename, action = "") {
        this.filename = filename;
        this.fileSize = 1024 * 1024 * 1024; // 1 MB.
        this.action = action || "default";

        // If this property is set, when the log file up to limit, send its 
        // contents to the receiver. Must configure mail information in 
        // `config.js` first.
        this.mailTo = "";
    }

    /** Outputs a message to the log file. */
    __output(level, ...msg) {
        // Get time info.
        var date = new Date,
            Y = date.getFullYear(),
            m = date.getMonth() + 1,
            d = date.getDate(),
            H = date.getHours(),
            i = date.getMinutes(),
            s = date.getSeconds(),
            ms = date.getMilliseconds();
        m = m >= 10 ? m : `0${m}`;
        d = d >= 10 ? d : `0${d}`;
        H = H >= 10 ? H : `0${H}`;
        i = i >= 10 ? i : `0${i}`;
        s = s >= 10 ? s : `0${s}`;
        ms = ms >= 100 ? ms : (ms >= 10 ? `0${ms}` : `00${ms}`);
        var timeStr = `${Y}-${m}-${d} ${H}:${i}:${s}.${ms}`;

        // Get log contents.
        msg = util.format(...msg);
        var logStr = `[${timeStr}] [${level}] ${this.action} - ${msg}\n`;

        if (fs.existsSync(this.filename)) {
            // File exists, test file size.
            var stat = fs.statSync(this.filename);
            if (stat.size + Buffer.byteLength(msg) >= this.fileSize) {
                // If size out of limit,
                if (this.mailTo) {
                    // send old logs as email to the receiver.
                    var host = Array.isArray(config.server.host) ?
                        config.server.host[0] :
                        config.server.host,
                        mail = new Mail(`[Log] of ${host}`),
                        contents = fs.readFileSync(this.filename, "utf8");
                    mail.to(this.mailTo).text(contents).send().then(info => {
                        // Rewrite the log file.
                        fs.writeFileSync(this.filename, logStr);
                    }).catch(err => {
                        this.error(err);
                    });<commit_msg>Log system support.<commit_after>const fs = require("fs");
const zlib = require("zlib");
const path = require("path");
const util = require("util");
const Mail = require("./Mail");
const { EOL } = require("os");

class Logger {
    /**
     * Creates a new instance with a specified filename.
     * 
     * @param {String} filename A file that stores the logs.
     * @param {String} [action] An optional action name.
     */
    constructor(filename, action = "") {
        this.filename = filename;
        this.fileSize = 1024 * 1024 * 1024; // 1 MB.
        this.action = action || "default";

        // If this property is set, when the log file up to limit, send its 
        // contents to the receiver. Must configure mail information in 
        // `config.js` first.
        this.mailTo = "";
    }

    /** Outputs a message to the log file. */
    __output(level, ...msg) {
        // Get time info.
        var date = new Date,
            Y = date.getFullYear(),
            m = date.getMonth() + 1,
            d = date.getDate(),
            H = date.getHours(),
            i = date.getMinutes(),
            s = date.getSeconds(),
            ms = date.getMilliseconds();
        m = m >= 10 ? m : `0${m}`;
        d = d >= 10 ? d : `0${d}`;
        H = H >= 10 ? H : `0${H}`;
        i = i >= 10 ? i : `0${i}`;
        s = s >= 10 ? s : `0${s}`;
        ms = ms >= 100 ? ms : (ms >= 10 ? `0${ms}` : `00${ms}`);
        var timeStr = `${Y}-${m}-${d} ${H}:${i}:${s}.${ms}`;

        // Get log contents.
        msg = util.format(...msg);
        var logStr = `[${timeStr}] [${level}] ${this.action} - ${msg}${EOL}`;

        if (fs.existsSync(this.filename)) {
            // File exists, test file size.
            var stat = fs.statSync(this.filename);
            if (stat.size + Buffer.byteLength(msg) >= this.fileSize) {
                // If size out of limit,
                if (this.mailTo) {
                    // send old logs as email to the receiver.
                    var host = Array.isArray(config.server.host) ?
                        config.server.host[0] :
                        config.server.host,
                        mail = new Mail(`[Log] of ${host}`),
                        contents = fs.readFileSync(this.filename, "utf8");
                    mail.to(this.mailTo).text(contents).send().then(info => {
                        // Rewrite the log file.
                        fs.writeFileSync(this.filename, logStr);
                    }).catch(err => {
                        this.error(err);
                    });
{'fraction_non_alphanumeric': 0.16108827559209238, 'fraction_numerical': 0.015267175572519083, 'mean_word_length': 1.4842002916869228, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1160810', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1480, 'n_words': 425}