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

feat: add support for customizing the agent #24

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ npm install log4js @log4js-node/logstash-http
* `logChannel` - `string` (optional) - also used to identify your application's logs [but in a more specific way]
* `logType` - `string` (optional) - used for the `type` field in the logstash data
* `timeout` - `integer` (optional, defaults to 5000ms) - the timeout for the HTTP request.
* `agent` - `http.Agent | https.Agent` (optional) - used to configure the requests being sent out if needed.

This appender will also pick up Logger context values from the events, and add them as `p_` values in the logFaces event. See the example below for more details.

5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -34,13 +34,18 @@ function format(logData) {
* "logChannel": "test", // channel of the application
* "url": "http://lfs-server/_bulk", // logstash receiver servlet URL
* }
* @param {import('../types').LogstashHTTPAppender} config
*/
function logstashHTTPAppender(config) {
const sender = axios.create({
baseURL: config.url,
timeout: config.timeout || 5000,
headers: { 'Content-Type': 'application/x-ndjson' },
withCredentials: true,
// The user should pass in the correct Agent type for their url
// since their url won't change after config this should be fine
httpAgent: config.agent,
httpsAgent: config.agent,
});

return function log(event) {
8 changes: 8 additions & 0 deletions test/tap/index-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { Agent } = require('http');
const test = require('tap').test;
const sandbox = require('@log4js-node/sandboxed-module');
const appender = require('../../lib');
@@ -64,18 +65,25 @@ test('logstashappender', (batch) => {
});

batch.test('when using HTTP receivers', (t) => {
const agent = new Agent();
const setup = setupLogging('myCategory', {
application: 'logstash-sample',
logType: 'application',
logChannel: 'sample',
url: 'http://localhost/receivers/rx1'
agent,
});

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' });
// For some reason with the sandboxed tests, the instanceof Agent doesn't exist.
assert.match(setup.fakeAxios.config, {
httpAgent: Object,
httpsAgent: Object,
});
assert.end();
});

7 changes: 7 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import type { Agent as httpAgent } from 'http';
import type { Agent as httpsAgent } from 'https';

export interface LogstashHTTPAppender extends Appender {
type: '@log4js-node/logstashHTTP';
url: string;
timeout ?: number; //defaults to 5000
application ?: string;
logChannel ?: string;
logType ?: string;
/** An http.Agent or https.Agent to allow configuring behavior as needed.
* Make sure you use the correct type base on your url
*/
agent?: httpAgent | httpsAgent;
}