Skip to content

Commit b6fdbf8

Browse files
committed
feat: add support for customizing the agent
1 parent e46fe2e commit b6fdbf8

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ npm install log4js @log4js-node/logstash-http
1414
* `logChannel` - `string` (optional) - also used to identify your application's logs [but in a more specific way]
1515
* `logType` - `string` (optional) - used for the `type` field in the logstash data
1616
* `timeout` - `integer` (optional, defaults to 5000ms) - the timeout for the HTTP request.
17+
* `agent` - `http.Agent | https.Agent` (optional) - used to configure the requests being sent out if needed.
1718

1819
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.
1920

lib/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,18 @@ function format(logData) {
3434
* "logChannel": "test", // channel of the application
3535
* "url": "http://lfs-server/_bulk", // logstash receiver servlet URL
3636
* }
37+
* @param {import('../types').LogstashHTTPAppender} config
3738
*/
3839
function logstashHTTPAppender(config) {
3940
const sender = axios.create({
4041
baseURL: config.url,
4142
timeout: config.timeout || 5000,
4243
headers: { 'Content-Type': 'application/x-ndjson' },
4344
withCredentials: true,
45+
// The user should pass in the correct Agent type for their url
46+
// since their url won't change after config this should be fine
47+
httpAgent: config.agent,
48+
httpsAgent: config.agent,
4449
});
4550

4651
return function log(event) {

test/tap/index-test.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

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

6667
batch.test('when using HTTP receivers', (t) => {
68+
const agent = new Agent();
6769
const setup = setupLogging('myCategory', {
6870
application: 'logstash-sample',
6971
logType: 'application',
7072
logChannel: 'sample',
7173
url: 'http://localhost/receivers/rx1'
74+
agent,
7275
});
7376

7477
t.test('axios should be configured', (assert) => {
7578
assert.equal(setup.fakeAxios.config.baseURL, 'http://localhost/receivers/rx1');
7679
assert.equal(setup.fakeAxios.config.timeout, 5000);
7780
assert.equal(setup.fakeAxios.config.withCredentials, true);
7881
assert.same(setup.fakeAxios.config.headers, { 'Content-Type': 'application/x-ndjson' });
82+
// For some reason with the sandboxed tests, the instanceof Agent doesn't exist.
83+
assert.match(setup.fakeAxios.config, {
84+
httpAgent: Object,
85+
httpsAgent: Object,
86+
});
7987
assert.end();
8088
});
8189

types/index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
import type { Agent as httpAgent } from 'http';
2+
import type { Agent as httpsAgent } from 'https';
3+
14
export interface LogstashHTTPAppender extends Appender {
25
type: '@log4js-node/logstashHTTP';
36
url: string;
47
timeout ?: number; //defaults to 5000
58
application ?: string;
69
logChannel ?: string;
710
logType ?: string;
11+
/** An http.Agent or https.Agent to allow configuring behavior as needed.
12+
* Make sure you use the correct type base on your url
13+
*/
14+
agent?: httpAgent | httpsAgent;
815
}

0 commit comments

Comments
 (0)