"use strict";
/**
 *  Access Logger
 *
 *  Copyright (C) 2010-2020 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.accessLoggerConfigBuilder = exports.routeAccessEmitter = exports.accessLogger = void 0;
var fs = require("fs");
var morgan = require("morgan");
/**
 * The RSG Access Logger
 *
 * This is a wrapper around the `morgan` library.
 */
exports.accessLogger = function (format) {
    var config = exports.accessLoggerConfigBuilder(format);
    return morgan(config.format, config.options);
};
/**
 * Emit basic info about a request
 *
 * For use as middleware with express
 */
exports.routeAccessEmitter = function (emitter) { return function (req, _res, next) {
    emitter.emit('request-received', {
        http_method: req.method.toUpperCase(),
        path: req.originalUrl,
    });
    next();
}; };
/**
 * Builds a config that can be used with the access loger
 */
exports.accessLoggerConfigBuilder = function (format) {
    var access_log_format = format || 'combined';
    var options = {};
    // we will write the logs to the console unless told otherwise
    if (process.env.DULLAHAN_LOG_PATH_ACCESS) {
        var accessLogStream = fs.createWriteStream(process.env.DULLAHAN_LOG_PATH_ACCESS, {
            flags: 'a',
        });
        options.stream = accessLogStream;
    }
    return {
        format: access_log_format,
        options: options,
    };
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQWNjZXNzTG9nZ2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiQWNjZXNzTG9nZ2VyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQTs7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