trentm / node-bunyan

a simple and fast JSON logging module for node.js services
Other
7.15k stars 513 forks source link

Serializer on error and custom fields #677

Open GaikwadPratik opened 2 years ago

GaikwadPratik commented 2 years ago

Hi @trentm,

I'm trying to add below test case because I've something similar in my application. But the serializer function is never called.

test('custom serializer on error and params', function (t) {
    var records = [];

    function customSerializer(message) {
        console.log({ message });
        let _maskedMessage = { ...message };
        let _isString = true;
        if (typeof message !== "string") {
            _maskedMessage = JSON.stringify(message);
            _isString = false;
        }
        _maskedMessage = _maskedMessage.replace(/"password":(["])(?:(?=(\\?))\2.)*?\1/gmi, "\"password\": \"*******\"");
        if (_isString === true) {
            return _maskedMessage;
        }
        const _parsed = JSON.parse(_maskedMessage);
        return _parsed;     
    }

    var log = createLogger({ "msg": customSerializer, "_data": customSerializer, "_message": customSerializer, "realm": customSerializer, "params": customSerializer, "serviceAccount": customSerializer, "_msg": customSerializer }, records);
    const err = new Error("T");
    log.info(err, {password: "test"}, "Testing serialization");
    t.has(records[0].msg, "*****");
    t.end();
});

Are the serializers not applied for internal keywords? If that is not the case, what am I missing here?