Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream's write function mutates external record objects making it unusable for other bunyan streams #2

Closed
racbart opened this issue Apr 21, 2018 · 1 comment · Fixed by #3

Comments

@racbart
Copy link
Contributor

racbart commented Apr 21, 2018

LogDNA stream deletes properties from record object and such modified object is then passed to any other raw bunyan stream declared after LogDNA stream, resulting in incomplete data logged with other streams.

This is because in JS objects are passed to functions as references. This means that modifying arguments inside the function actually modifies external object, not a local copy (because there is no local copy). Stream driver should not mutate received arguments in any way.

Example:

const bunyan = require('bunyan');

/* Basic stream which will get raw data and print it to the console */
class SimpleStream {
  write(record) {
    console.log(record);
  };
}

let LogDNAStream = require('logdna-bunyan').BunyanStream;
let logDNA = new LogDNAStream({
  key: '...apikey...'
});

var logger = bunyan.createLogger({
  name: 'test',
  streams: [
    { stream: new SimpleStream(), type: 'raw' }, // first: basic stream
    { stream: logDNA, type: 'raw' },             // second: logdna
    { stream: new SimpleStream(), type: 'raw' }, // third: basic stream
  ]
});

logger.warn('This is a warning');

The above logger instance uses three streams. The first prints to the console, the second is a LogDNAStream, the third prints to the console.

Expected behaviour: object printed to the console with first and third stream is the same.

Actual behaviour:

{ name: 'test',
  hostname: 'hostname',
  pid: 3296,
  level: 40,
  msg: 'This is a warning',
  time: 2018-04-21T10:36:05.294Z,
  v: 0 }
{ hostname: 'hostname',
  pid: 3296,
  time: 2018-04-21T10:36:05.294Z,
  v: 0 }

Result: LogDNAStream stream deleted some properties and later streams receive incomplete record. Actually, the most important fields are deleted, level and the message itself, which makes the log record useless.

@racbart
Copy link
Contributor Author

racbart commented Apr 21, 2018

I created pull request with a fix for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant