"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = exports.configureLogger = void 0;
const formatISO_1 = __importDefault(require("date-fns/formatISO"));
const getUnixTime_1 = __importDefault(require("date-fns/getUnixTime"));
const winston_1 = require("winston");
const envs_1 = require("./envs");
const tracing_1 = require("./tracing");
const utils_1 = require("./utils");
const traceFormat = winston_1.format.printf((options) => {
    var _a, _b;
    const time = new Date();
    const payload = {
        level: options.level,
        epochTimestamp: (0, getUnixTime_1.default)(time),
        isoTimestamp: (0, formatISO_1.default)(time),
        serverVendor: options.serverVendor,
        serverRegion: options.serverRegion,
        environment: options.environment,
        domain: options.domain,
        payload: options.payload,
        serviceName: options.serviceName,
        serviceKind: options.serviceKind,
        message: options.message,
    };
    const traceId = (_a = (0, tracing_1.retrieveTraceId)()) === null || _a === void 0 ? void 0 : _a.traceId;
    const spanId = (_b = (0, tracing_1.retrieveTraceId)()) === null || _b === void 0 ? void 0 : _b.spanId;
    if (traceId || spanId) {
        payload.reconciliationId = `${traceId}-${spanId}`;
    }
    // Raw buffers mess up redact, e.g: [134,229,5,195,68,136]
    // convert to JSON as the response body is sometimes rendered as string
    if ((0, utils_1.isJsonString)(payload.payload)) {
        payload.payload = JSON.parse(payload.payload);
    }
    return JSON.stringify(payload).replace(/\\"/g, '"');
});
const configureLogger = ({ serviceId, serviceName = (0, envs_1.maybeGetEnvParam)(envs_1.EnvVar.SERVICE_NAME), serverVendor = 'AWS', serverRegion = process.env.AWS_REGION, domain = process.env.DOMAIN, environment = (0, envs_1.maybeGetEnvParam)(envs_1.EnvVar.ENVIRONMENT), serviceKind = process.env.SERVICE_KIND || 'Primary', level = process.env.LOG_LEVEL || 'info', }) => {
    return (0, winston_1.createLogger)({
        level,
        format: winston_1.format.combine(winston_1.format.json(), traceFormat),
        defaultMeta: {
            serverVendor,
            serverRegion,
            domain,
            serviceName,
            environment,
            serviceKind,
            serviceId,
        },
        transports: [new winston_1.transports.Console()],
    });
};
exports.configureLogger = configureLogger;
exports.logger = (0, exports.configureLogger)({});
//# sourceMappingURL=logging.js.map