id0Sch / log4js-json-layout

provides a slim and easy to use json-layout for log4js-node
MIT License
17 stars 8 forks source link

include not working? #10

Open jmbldwn opened 3 years ago

jmbldwn commented 3 years ago

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 commented 3 years ago

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...

id0Sch commented 3 years ago

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