KyleRoss / node-lambda-log

Basic logging mechanism for Node.js Lambda Functions and other AWS services, which produces logs in JSON format for easier reading through Cloudwatch Logs.
https://lambdalog.dev
MIT License
193 stars 16 forks source link

Replacer for the meta object? #30

Closed marcortw closed 4 years ago

marcortw commented 4 years ago

Is it possible to apply the replacer function to the meta object? I sometimes log whole objects using the meta object and it would be nice if I could mask mySecretValue with something else using the replacer function.

log.debug('Test debug log',{'mySecretKey':'mySecretValue'});

From the docs, I understand that the replacer is only applied if I would call it like this:

log.debug({'mySecretKey':'mySecretValue'}).toJSON(true);

Thanks for this neat little module, Marco

KyleRoss commented 4 years ago

The replacer function is utilized when logging to the console as well, so implementing the replacer function will work without you manually having to call .toJSON();. Given that, you would be able to utilize the replacer function to mask values within any passed in metadata. For example:

log.options.replacer = function(key, value) {
    if(key === 'mySecretKey') {
        return 'XXXXXX';
    }
    return value;
};

log.error('My message', { mySecretKey: 'mySecretValue' });
//=> { "_logLevel": "error", "msg": "My message", "mySecretKey": "XXXXXX", "_tags": ["log", "error"] }

I hope this helps!

marcortw commented 4 years ago

This definitely helps, thanks a lot for the quick response! I did set contradicting log.options in two different files during my tests, but it seems like only the first options got applied (which didn't have the replacer function set).