Anthony
Anthony

Reputation: 14279

How to log nested objects with elastic (ELK) stack

I am trying to log a JSON value with nested properties to a logzio ELK stack via NodeJS

The code is:

var logger = require('logzio-nodejs').createLogger({
  token: config.LOGZIO_API_KEY
});


// sending text
logger.log('This is a log message');

// sending an object
var obj = {
  message: 'Some log message',
  param1: 'val1',
  param2: 'val2'
};

logger.log(obj);

This works fine, but if I want to log nested data such as:

// sending an object
var obj = {
  message: 'Some log message',
  param1: {
    a : 'b',
    c : 'd'
  },
  param2: 'val2'
};

This returns an error:

{"type":"mapper_parsing_exception","reason":"failed to parse [param1]","caused_by":{"type":"illegal_argument_exception","reason":"unknown property [a]"}}

What can I adjust that will allow me to log this nested object?

Upvotes: 2

Views: 911

Answers (1)

Val
Val

Reputation: 217334

This is because when you've logged param1 as a string, ES created a field of type string.

Thereafter, you tried to log param1 as an object and that was conflicting with the the string type.

However, you can definitely log a nested object but you have to give it another name that a field that already exists, say param3, that should work:

// sending an object
var obj = {
  message: 'Some log message',
  param3: {
    a : 'b',
    c : 'd'
  },
  param2: 'val2'
};

Upvotes: 2

Related Questions