log4js-json-layout icon indicating copy to clipboard operation
log4js-json-layout copied to clipboard

include not working?

Open jmbldwn opened this issue 4 years ago • 2 comments

I must be missing something. I can only include startTime, categoryName, level, and data in my json output.

It appears to be because the formatter code selects the fields from output instead of data:

    // Only include fields specified in 'include' field
    // if field is specified
    if (config.include && config.include.length) {
      const newOutput = {};
      _.forEach(config.include, (key) => {
        if (_.has(output, key)) {
          newOutput[key] = output[key];
        }
      });
      return newOutput;
    }

data is set earlier as a clone of the formatter input:

  function formatter(raw) {
    const data = _.clone(raw);
...

and output specifically only selects a few fields:

    const output = {
      startTime: data.startTime,
      categoryName: data.categoryName,
      level: data.level.levelStr,
    };

It would appear that this bit of code at line 105:

        if (_.has(output, key)) {
          newOutput[key] = output[key];
        }

should be:

        if (_.has(data, key)) {
          newOutput[key] = data[key];
        }

Am I missing something? I assume I'm doing something wrong...

jmbldwn avatar Mar 17 '21 04:03 jmbldwn

This fix wouldn't be entirely right; it butchers the level field. A more thoughtful fix is needed, unless I'm just not using this right...

jmbldwn avatar Mar 17 '21 05:03 jmbldwn

Feel free to submit a pull request and I'll review it.

id0Sch avatar Apr 12 '21 15:04 id0Sch