"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StandardLogger = void 0;
/**
 * Stdout logger
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of liza.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU 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/>.
 *
 * Standard out logger implementing PSR-3 standards
 */
var PsrLogger_1 = require("./PsrLogger");
var StandardLogger = /** @class */ (function () {
    /**
     * Initialize logger
     *
     * @param _console
     * @param _ts_ctor - a timestamp constructor
     * @param _env     - The environment ( dev, test, demo, live )
     * @param _service - service that is logging
     */
    function StandardLogger(_console, _ts_ctor, _env, _service) {
        if (_service === void 0) { _service = 'quote-server'; }
        this._console = _console;
        this._ts_ctor = _ts_ctor;
        this._env = _env;
        this._service = _service;
    }
    /**
     * Log at a debug level
     *
     * @param msg     - the message to log
     * @param context - additional message context
     */
    StandardLogger.prototype.debug = function (msg, context) {
        this._console.info(this._format(PsrLogger_1.LogLevel.DEBUG, msg, context));
    };
    /**
     * Log at a debug level
     *
     * @param msg     - the message to log
     * @param context - additional message context
     */
    StandardLogger.prototype.info = function (msg, context) {
        this._console.info(this._format(PsrLogger_1.LogLevel.INFO, msg, context));
    };
    /**
     * Log at a debug level
     *
     * @param msg     - the message to log
     * @param context - additional message context
     */
    StandardLogger.prototype.notice = function (msg, context) {
        this._console.log(this._format(PsrLogger_1.LogLevel.NOTICE, msg, context));
    };
    /**
     * Log at a debug level
     *
     * @param msg     - the message to log
     * @param context - additional message context
     */
    StandardLogger.prototype.warning = function (msg, context) {
        this._console.warn(this._format(PsrLogger_1.LogLevel.WARNING, msg, context));
    };
    /**
     * Log at a debug level
     *
     * @param msg     - the message to log
     * @param context - additional message context
     */
    StandardLogger.prototype.error = function (msg, context) {
        this._console.error(this._format(PsrLogger_1.LogLevel.ERROR, msg, context));
    };
    /**
     * Log at a debug level
     *
     * @param msg     - the message to log
     * @param context - additional message context
     */
    StandardLogger.prototype.critical = function (msg, context) {
        this._console.error(this._format(PsrLogger_1.LogLevel.CRITICAL, msg, context));
    };
    /**
     * Log at a debug level
     *
     * @param msg     - the message to log
     * @param context - additional message context
     */
    StandardLogger.prototype.alert = function (msg, context) {
        this._console.error(this._format(PsrLogger_1.LogLevel.ALERT, msg, context));
    };
    /**
     * Log at a debug level
     *
     * @param msg     - the message to log
     * @param context - additional message context
     */
    StandardLogger.prototype.emergency = function (msg, context) {
        this._console.error(this._format(PsrLogger_1.LogLevel.EMERGENCY, msg, context));
    };
    /**
     * Log a message
     *
     * @param msg     - the message to log
     * @param context - additional message context
     */
    StandardLogger.prototype.log = function (level, msg, context) {
        this._console.error(this._format(level, msg, context));
    };
    /**
     * Get structured log Record<string, unknown>
     *
     * @param msg   - the string or Record<string, unknown> to log
     * @param level - the log level
     * @param context - additional message context
     *
     * @returns a structured logging Record<string, unknown>
     */
    StandardLogger.prototype._format = function (level, msg, context) {
        if (context === void 0) { context = {}; }
        var str;
        if (msg !== null && typeof msg === 'object') {
            str = JSON.stringify(msg);
        }
        else {
            str = msg;
        }
        var ts = this._ts_ctor();
        var tsFormatted = new Date(ts * 1000).toISOString();
        var structured_log = {
            message: str,
            timestamp: tsFormatted,
            service: this._service,
            env: this._env,
            severity: PsrLogger_1.LogLevel[level],
        };
        if (Object.keys(context).length > 0) {
            structured_log['context'] = context;
        }
        return JSON.stringify(structured_log);
    };
    return StandardLogger;
}());
exports.StandardLogger = StandardLogger;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiU3RhbmRhcmRMb2dnZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cm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