"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class ZBLogger {
    constructor({ loglevel, color, namespace, stdout, id, taskType, colorise, } = { loglevel: 'INFO' }) {
        this.colorFn = color || (m => m);
        this.taskType = taskType;
        this.id = id;
        if (Array.isArray(namespace)) {
            namespace = namespace.join(' ');
        }
        this.loglevel = loglevel;
        this.stdout = stdout || console;
        this.colorise = colorise !== false;
    }
    info(message, ...optionalParameters) {
        this.log(message, optionalParameters);
    }
    error(message, ...optionalParameters) {
        if (this.loglevel === 'NONE') {
            return;
        }
        if (optionalParameters.length > 0) {
            return this.stdout.error(message, optionalParameters);
        }
        else {
            return this.stdout.error(message);
        }
    }
    debug(message, ...optionalParameters) {
        if (this.loglevel !== 'DEBUG') {
            return;
        }
        this.log(message, optionalParameters);
    }
    log(message, ...optionalParameters) {
        if (this.loglevel === 'NONE' || this.loglevel === 'ERROR') {
            return;
        }
        if (this.stdout === console) {
            let msg;
            if (this.colorise) {
                msg =
                    this.getMetadataString() +
                        ' > ' +
                        this.stringifyJSON(message);
                if (optionalParameters) {
                    msg +=
                        ' ' +
                            optionalParameters
                                .map(o => this.stringifyJSON(o))
                                .join(' ');
                }
            }
            this.stdout.info(this._colorise(msg));
        }
        else {
            this.stdout.info(message, optionalParameters);
        }
    }
    _colorise(message) {
        if (this.stdout === console && this.colorise) {
            // Only colorise console
            if (this.colorFn && typeof this.colorFn === 'function') {
                return this.colorFn(message);
            }
            else {
                return message;
            }
        }
        return message;
    }
    getMetadataString() {
        return '[ ' + this.getId() + this.getNamespace() + ' ]';
    }
    getId() {
        return `${this.taskType} ${this.id}`;
    }
    stringifyJSON(message) {
        let parsedMessage = message;
        if (message &&
            typeof message === 'object' &&
            !(message instanceof Error)) {
            try {
                parsedMessage = JSON.stringify(message, null, 2);
            }
            catch (e) {
                parsedMessage = message;
            }
        }
        if (message instanceof Error) {
            const getStackTrace = () => {
                const obj = {};
                Error.captureStackTrace(obj, getStackTrace);
                return obj.stack;
            };
            return getStackTrace();
        }
        return parsedMessage;
    }
    getNamespace() {
        return this.namespace ? ` ${this.namespace}` : '';
    }
}
exports.ZBLogger = ZBLogger;
//# sourceMappingURL=ZBLogger.js.map