winston-elasticsearch
winston-elasticsearch copied to clipboard
Winston throws 'Elasticsearch error' with no other information
Our application sometimes receives a Elasticsearch error
from node_modules/winston-elasticsearch/bulk_writer.js
and specifically on line 134:
.then((res) => {
if (res && res.errors && res.items) {
const err = new Error('Elasticsearch error');
res.items.forEach((item, itemIndex) => {
const bodyData = body[itemIndex * 2 + 1];
The error appears to be:
elasticsearch indexing error {
type: 'mapper_parsing_exception',
reason: 'object mapping for [fields.stack] tried to parse field [null] as object, but found a concrete value'
}
Why and when is such an error usually thrown and what can we do to fix this? This error actually causes our whole container to shut-down. We're using NestJS.
Any news/information on this?
I was eventually able to fix this error by just catching all errors in our NestJS application and mutating the error format. It took quite some while to figure out where the exception was actually coming from.
It still throws this error occasionally. So this is still not fixed unfortunately.
We also encounter some issues with this since some weeks on multiple Nest 9 repositories, multiple logs are sent and it seems to loop on it and then crash
We eventually just disabled this package and our application never crashes again. Just waiting until it's fixed.
This case is described in the documentation. I spent a few hours trying to fix this issue and then just realized that the app fails because I don't handle 'error' events for elastic transport and logger instances.
...
if (config.log.useElasticTransport) {
const elasticsearchTransport = new ElasticsearchTransport({
...
});
elasticsearchTransport.on('error', console.error);
transports.push(elasticsearchTransport);
}
const instance = winston.createLogger({
transports,
});
instance.on('error', console.error);
return WinstonModule.createLogger({
instance,
});
So, to summarise, for the Nest.js application you need:
- Create Elacticsearch transport and add an error event listener;
- Create an instant of a Winston logger and pass transport inside;
- Add an error event listener to the logger instance;
Hope this information will be helpful.