"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function procConsole() {
    // @ts-ignore
    return process.console;
}
function init() {
    const app = require('./app').app;
    const config = require('./config').config;
    // Hack to allow proper logging of Error.
    Object.defineProperty(Error.prototype, 'message', {
        configurable: true,
        enumerable: true
    });
    Object.defineProperty(Error.prototype, 'stack', {
        configurable: true,
        enumerable: true
    });
    const scribe = require('scribe-js')({
        rootPath: config.server.logging.directory,
    });
    function addLogger(name, color, active) {
        if (active) {
            procConsole().addLogger(name, color, {
                logInConsole: false
            });
        }
        else {
            // @ts-ignore
            procConsole()[name] = function () {
                this._reset(); // forget tags, etc. for this logging event
            };
        }
    }
    addLogger('debug', 'grey', config.server.logging.debug);
    addLogger('profile', 'blue', config.server.logging.profile);
    if (config.server.logging.logRequests) {
        app.use(scribe.express.logger());
    }
    if (config.server.internal.active) {
        const prefix = config.server.rootPath === '/' ? '' : config.server.rootPath;
        app.use(prefix + '/internal/logs', scribe.webPanel());
    }
    // Hack to allow correct logging of node.js Error objects.
    // See: https://github.com/bluejamesbond/Scribe.js/issues/70
    Object.defineProperty(Error.prototype, 'toJSON', {
        configurable: true,
        value: function () {
            const alt = {};
            const storeKey = (key) => {
                alt[key] = this[key];
            };
            Object.getOwnPropertyNames(this).forEach(storeKey, this);
            return alt;
        }
    });
    // @ts-ignore
    for (const key of Object.keys(procConsole())) {
        // @ts-ignore
        module.exports[key] = procConsole()[key];
    }
}
const logger = {
    init,
    tag(...tags) {
        return {
            debug(...args) {
                procConsole().tag(...tags).debug(...args);
            },
            info(...args) {
                procConsole().tag(...tags).info(...args);
            },
            warn(...args) {
                procConsole().tag(...tags).warn(...args);
            },
            error(...args) {
                procConsole().tag(...tags).error(...args);
            },
            profile(...args) {
                procConsole().tag(...tags).profile(...args);
            },
        };
    }
};
exports.default = logger;
//# sourceMappingURL=logger.js.map