winston-opensearch v0.1.0
winston-opensearch
transport for the winston logging toolkit.
Most of code is from the winston-elasticsearch version 0.17.3
Features
- logstash compatible message structure.
- Thus consumable with kibana.
- Date pattern based index names.
- Custom transformer function to transform logged data into a different message structure.
- Buffering of messages in case of unavailability of ES. The limit is the memory as all unwritten messages are kept in memory.
Compatibility
I tested Winston 3.8.2, Opensearch 2.7 for AWS usage.
Installation
npm install --save winston winston-opensearchUsage
const winston = require('winston');
const { OpensearchTransport } = require('winston-opensearch');
const esTransportOpts = {
level: 'info',
};
const esTransport = new OpensearchTransport(esTransportOpts);
const logger = winston.createLogger({
transports: [esTransport],
});
// Compulsory error handling
logger.on('error', (error) => {
console.error('Error in logger caught', error);
});
esTransport.on('error', (error) => {
console.error('Error in logger caught', error);
});The winston API for logging can be used with one restriction: Only one JS object can only be logged and indexed as such. If multiple objects are provided as arguments, the contents are stringified.
Options
levelinfoMessages logged with a severity greater or equal to the given one are logged to ES; others are discarded.indexnone | whendataStreamistrue,logs-app-defaultThe index to be used. This option is mutually exclusive withindexPrefix.indexPrefixlogsThe prefix to use to generate the index name according to the pattern<indexPrefix>-<indexSuffixPattern>. Can be string or function, returning the string to use.indexSuffixPatternYYYY.MM.DDa Day.js compatible date/ time pattern.transformersee below A transformer function to transform logged data into a different message structure.useTransformertrueIf set totrue, the giventransformerwill be used (or the default). Set tofalseif you want to apply custom transformers during Winston'screateLogger.ensureIndexTemplatetrueIf set totrue, the givenindexTemplateis checked/ uploaded to ES when the module is sending the first log message to make sure the log messages are mapped in a sensible manner.indexTemplatesee fileindex-template-mapping.jsonthe mapping template to be ensured as parsed JSON.ensureIndexTemplateistrueandindexTemplateisundefinedflushInterval2000Time span between bulk writes in ms.retryLimit400Number of retries to connect to ES before giving up.healthCheckTimeout30sTimeout for one health check (health checks will be retried forever).healthCheckWaitForStatusyellowStatus to wait for when check upon health. See its API docs for supported options.healthCheckWaitForNodes>=1Nodes to wait for when check upon health. See its API docs for supported options.clientAn opensearch client instance. If given, theclientOptsare ignored.clientOptsAn object passed to the ES client. See its docs for supported options.waitForActiveShards1Sets the number of shard copies that must be active before proceeding with the bulk operation.pipelinenone Sets the pipeline id to pre-process incoming documents with. See the bulk API docs.bufferingtrueBoolean flag to enable or disable messages buffering. ThebufferLimitoption is ignored if set tofalse.bufferLimitnullLimit for the number of log messages in the buffer.sourcenone the source of the log message. This can be useful for microservices to understand from which service a log message origins.internalLoggerconsole.errorA logger of last resort to log internal errors.
Logging of OpenSearch Client
The default client and options will log through console.
Interdependencies of Options
When changing the indexPrefix and/or the transformer,
make sure to provide a matching indexTemplate.
Transformer
The transformer function allows mutation of log data as provided by winston into a shape more appropriate for indexing in Opensearch.
The default transformer generates a @timestamp and rolls any meta
objects into an object called fields.
Params:
logdataAn object with the data to log. Properties are:timestampnew Date().toISOString()The timestamp of the log entrylevelThe log level of the entrymessageThe message for the log entrymetaThe meta data for the log entry
Returns: Object with the following properties
@timestampThe timestamp of the log entryseverityThe log level of the entrymessageThe message for the log entryfieldsThe meta data for the log entry
The default transformer function's transformation is shown below.
Input A:
{
"message": "Some message",
"level": "info",
"meta": {
"method": "GET",
"url": "/sitemap.xml",
...
}
}Output A:
{
"@timestamp": "2019-09-30T05:09:08.282Z",
"message": "Some message",
"severity": "info",
"fields": {
"method": "GET",
"url": "/sitemap.xml",
...
}
}The default transformer can be imported and extended
Example
const { OpensearchTransformer } = require('winston-opensearch');
const esTransportOpts = {
transformer: (logData) => {
const transformed = OpensearchTransformer(logData);
transformed.fields.customField = 'customValue';
return transformed;
},
};
const esTransport = new OpensearchTransformer(esTransportOpts);Note that in current logstash versions, the only "standard fields" are
@timestamp and @version, anything else is just free.
A custom transformer function can be provided in the options initiation.
Events
error: in case of any error.
Example
An example assuming default settings.
Log Action
logger.info('Some message', {});Only JSON objects are logged from the meta field. Any non-object is ignored.
Generated Message
The log message generated by this module has the following structure:
{
"@timestamp": "2019-09-30T05:09:08.282Z",
"message": "Some log message",
"severity": "info",
"fields": {
"method": "GET",
"url": "/sitemap.xml",
"headers": {
"host": "www.example.com",
"user-agent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
"accept": "*/*",
"accept-encoding": "gzip,deflate",
"from": "googlebot(at)googlebot.com",
"if-modified-since": "Tue, 30 Sep 2019 11:34:56 GMT",
"x-forwarded-for": "66.249.78.19"
}
}
}Target Index
This message would be POSTed to the following endpoint:
http://localhost:9200/logs-2019.09.30/log/So the default mapping uses an index pattern logs-*.
2 years ago