"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MagicLog = exports.CustomTransport = void 0;
const decorator_1 = require("@midwayjs/decorator");
const logger_1 = require("@midwayjs/logger");
class CustomTransport extends logger_1.EmptyTransport {
    constructor(opts) {
        super(opts);
        this.magicLog = opts.magicLog;
    }
    log(info, callback) {
        var _a;
        if (info.ctx) {
            if ((_a = info.ctx) === null || _a === void 0 ? void 0 : _a.jobId) {
                const { jobId, from } = info.ctx;
                this.magicLog.sendLog({
                    level: info.LEVEL,
                    message: `${info.timestamp} ${info.LEVEL} ${info.pid} [${jobId} ${from.name}}] ${info.message}`,
                });
            }
            else {
                const ctx = info.ctx;
                // format: '[$userId/$ip/$traceId/$use_ms $method $url]'
                const userId = ctx.userId || '-';
                const traceId = (ctx.tracer && ctx.tracer.traceId) || '-';
                const use = Date.now() - ctx.startTime;
                const label = userId +
                    '/' +
                    ctx.ip +
                    '/' +
                    traceId +
                    '/' +
                    use +
                    'ms ' +
                    ctx.method +
                    ' ' +
                    ctx.url;
                this.magicLog.sendLog({
                    level: info.LEVEL,
                    message: `${info.timestamp} ${info.LEVEL} ${info.pid} [${label}] ${info.message}`,
                });
            }
        }
        else {
            this.magicLog.sendLog({
                level: info.LEVEL,
                message: `${info.timestamp} ${info.LEVEL} ${info.pid} ${info.labelText}${info.message}`,
            });
        }
        callback();
    }
}
exports.CustomTransport = CustomTransport;
let MagicLog = class MagicLog {
    constructor() {
        this.streams = new Set();
    }
    async init() {
        this.magicLogger.add(new CustomTransport({
            magicLog: this,
        }));
        this.bullLogger.add(new CustomTransport({
            magicLog: this,
        }));
        this.magicLogger.info('初始化MagicLog');
    }
    sendLog(log) {
        this.streams.forEach(s => s.write(log));
    }
    addStream(stream) {
        this.streams.add(stream);
    }
    removeStream(stream) {
        this.streams.delete(stream);
    }
};
__decorate([
    (0, decorator_1.Logger)('magic'),
    __metadata("design:type", Object)
], MagicLog.prototype, "magicLogger", void 0);
__decorate([
    (0, decorator_1.Logger)('bullLogger'),
    __metadata("design:type", Object)
], MagicLog.prototype, "bullLogger", void 0);
MagicLog = __decorate([
    (0, decorator_1.Provide)(),
    (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
], MagicLog);
exports.MagicLog = MagicLog;
//# sourceMappingURL=magic.log.js.map