diff --git a/lib/appenders/logstashHTTP.js b/lib/appenders/logstashHTTP.js deleted file mode 100644 index b04862c..0000000 --- a/lib/appenders/logstashHTTP.js +++ /dev/null @@ -1,97 +0,0 @@ -/** - * This appender is deprecated, please apply any bugfixes or changes - * to https://github.com/log4js-node/logstash-http - * logstashHTTP appender sends JSON formatted log events to logstashHTTP receivers. - * - * HTTP require 'axios', see 'https://www.npmjs.com/package/axios' - * - * Make sure your project have relevant dependancy installed before using this appender. - */ -/* eslint global-require:0 */ - -'use strict'; - -const util = require('util'); -const axios = require('axios'); - -/** - * - * For HTTP (browsers or node.js) use the following configuration params: - * { - * "type": "logstashHTTP", // must be present for instantiation - * "application": "logstash-test", // name of the application - * "logType": "application", // type of the application - * "logChannel": "test", // channel of the application - * "url": "http://lfs-server/_bulk", // logstash receiver servlet URL - * } - */ -function logstashHTTPAppender(config) { - const sender = axios.create({ - baseURL: config.url, - timeout: config.timeout || 5000, - headers: { 'Content-Type': 'application/x-ndjson' }, - withCredentials: true, - }); - - const appender = function log(event) { - const logstashEvent = [ - { - index: { - _index: config.application, - _type: config.logType, - }, - }, - { - message: format(event.data), // eslint-disable-line - context: event.context, - level: event.level.level / 100, - level_name: event.level.levelStr, - channel: config.logChannel, - datetime: (new Date(event.startTime)).toISOString(), - extra: {}, - }, - ]; - const logstashJSON = `${JSON.stringify(logstashEvent[0])}\n${JSON.stringify(logstashEvent[1])}\n`; - - // send to server - sender.post('', logstashJSON) - .catch((error) => { - if (error.response) { - console.error(`log4js.logstashHTTP Appender error posting to ${config.url}: ${error.response.status} - ${error.response.data}`); - return; - } - console.error(`log4js.logstashHTTP Appender error: ${error.message}`); - }); - }; - - appender.deprecated = '@log4js-node/logstash-http'; - - return appender; -} - -function configure(config) { - return logstashHTTPAppender(config); -} - -function format(logData) { - const data = Array.isArray(logData) - ? logData - : Array.prototype.slice.call(arguments); - return util.format.apply(util, wrapErrorsWithInspect(data)); -} - -function wrapErrorsWithInspect(items) { - return items.map((item) => { - if ((item instanceof Error) && item.stack) { - return { - inspect: function () { - return `${util.format(item)}\n${item.stack}`; - } - }; - } - - return item; - }); -} - -module.exports.configure = configure; diff --git a/test/tap/logstashHTTP-test.js b/test/tap/logstashHTTP-test.js deleted file mode 100644 index fde2ecd..0000000 --- a/test/tap/logstashHTTP-test.js +++ /dev/null @@ -1,115 +0,0 @@ -'use strict'; - -const test = require('tap').test; -const sandbox = require('@log4js-node/sandboxed-module'); -const appender = require('../../lib/appenders/logstashHTTP'); - -function setupLogging(category, options) { - const fakeAxios = { - create: function (config) { - this.config = config; - return { - post: function (emptyString, event) { - fakeAxios.args = [emptyString, event]; - return { - catch: function (cb) { - fakeAxios.errorCb = cb; - } - }; - } - }; - } - }; - - const fakeConsole = { - log: () => {}, - error: function (msg) { - this.msg = msg; - } - }; - - const log4js = sandbox.require('../../lib/log4js', { - requires: { - axios: fakeAxios - }, - globals: { - console: fakeConsole - } - }); - - options.type = 'logstashHTTP'; - log4js.configure({ - appenders: { http: options }, - categories: { default: { appenders: ['http'], level: 'trace' } } - }); - - return { - logger: log4js.getLogger(category), - fakeAxios: fakeAxios, - fakeConsole: fakeConsole - }; -} - -test('logstashappender', (batch) => { - batch.test('should export a configure function', (t) => { - t.type(appender.configure, 'function'); - t.end(); - }); - - batch.test('when using HTTP receivers', (t) => { - const setup = setupLogging('myCategory', { - application: 'logstash-sample', - logType: 'application', - logChannel: 'sample', - url: 'http://localhost/receivers/rx1' - }); - - t.test('axios should be configured', (assert) => { - assert.equal(setup.fakeAxios.config.baseURL, 'http://localhost/receivers/rx1'); - assert.equal(setup.fakeAxios.config.timeout, 5000); - assert.equal(setup.fakeAxios.config.withCredentials, true); - assert.same(setup.fakeAxios.config.headers, { 'Content-Type': 'application/x-ndjson' }); - assert.end(); - }); - - setup.logger.addContext('foo', 'bar'); - setup.logger.addContext('bar', 'foo'); - setup.logger.warn('Log event #1'); - - t.test('an event should be sent', (assert) => { - const packet = setup.fakeAxios.args[1].split('\n'); - const eventHeader = JSON.parse(packet[0]); - const eventBody = JSON.parse(packet[1]); - assert.equal(eventHeader.index._index, 'logstash-sample'); - assert.equal(eventHeader.index._type, 'application'); - - assert.equal(eventBody.channel, 'sample'); - assert.equal(eventBody.message, 'Log event #1'); - assert.equal(eventBody.level_name, 'WARN'); - assert.equal(eventBody.context.foo, 'bar'); - assert.equal(eventBody.context.bar, 'foo'); - - // Assert timestamp, up to hours resolution. - const date = new Date(eventBody.datetime); - assert.equal( - date.toISOString().substring(0, 14), - new Date().toISOString().substring(0, 14) - ); - assert.end(); - }); - - t.test('errors should be sent to console.error', (assert) => { - setup.fakeAxios.errorCb({ response: { status: 500, data: 'oh no' } }); - assert.equal( - setup.fakeConsole.msg, - 'log4js.logstashHTTP Appender error posting to http://localhost/receivers/rx1: 500 - oh no' - ); - setup.fakeAxios.errorCb(new Error('oh dear')); - assert.equal(setup.fakeConsole.msg, 'log4js.logstashHTTP Appender error: oh dear'); - assert.end(); - }); - t.end(); - }); - - batch.end(); -});