8000 Merge pull request #30 from ZachHaber/agent · log4js-node/logFaces-HTTP@60e07f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 60e07f5

Browse files
authored
Merge pull request #30 from ZachHaber/agent
Add support for agent
2 parents 6c0de4d + 816aa1c commit 60e07f5
< 10000 div class="d-none">

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ npm install log4js @log4js-node/logfaces-http
1414
- `timeout` - `integer` (optional, defaults to 5000ms) - the timeout for the HTTP request.
1515
- `configContext` - function (optional) returning a global context object accessible to all appenders. Properties from configContext added as `p_` values in the logFaces event.
1616
- `hostname` - `string` (optional) - used to add the hostname `h` property to the logFaces event.
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. Note that Logger context may override the same properties defined in `configContext`.
1920

lib/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ function getErrorStack(logData) {
3232
* "application": "LFS-TEST", // name of the application (domain)
3333
* "url": "http://lfs-server/logs", // logFaces receiver servlet URL
3434
* }
35+
* @param {import('../types').LogFacesHTTPAppender} config
3536
*/
3637
function logFacesAppender(config) {
3738
const sender = axios.create({
3839
baseURL: config.url,
3940
timeout: config.timeout || 5000,
4041
headers: { 'Content-Type': 'application/json' },
4142
withCredentials: true,
43+
// The user should pass in the correct Agent type for their url
44+
// since their url won't change after config this should be fine
45+
httpAgent: config.agent,
46+
httpsAgent: config.agent,
4247
});
4348

4449
const { configContext } = config;

test/tap/index-test.js

Lines changed: 8 additions & 0 deletions
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');
45
const sandbox = require('@log4js-node/sandboxed-module');
56
const appender = require('../../lib');
@@ -65,10 +66,12 @@ test('logFaces appender', (batch) => {
6566
});
6667

6768
batch.test('when using HTTP receivers', (t) => {
69+
const agent = new Agent();
6870
const setup = setupLogging('myCategory', false, {
6971
application: 'LFS-HTTP',
7072
url: 'http://localhost/receivers/rx1',
7173
hostname: 'localhost',
74+
agent,
7275
});
7376

7477
t.test('axios should be configured', (assert) => {
@@ -81,6 +84,11 @@ test('logFaces appender', (batch) => {
8184
assert.same(setup.fakeAxios.config.headers, {
8285
'Content-Type': 'application/json',
8386
});
87+
// For some reason with the sandboxed tests, the instanceof Agent doesn't exist.
88+
assert.match(setup.fakeAxios.config, {
89+
httpAgent: Object,
90+
httpsAgent: Object,
91+
});
8492
assert.end();
8593
});
8694

types/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import type { Agent as httpAgent } from 'http';
2+
import type { Agent as httpsAgent } from 'https';
3+
14
export interface LogFacesHTTPAppender {
25
type: '@log4js-node/logfaces-http';
36
/** logFaces receiver servlet URL */
@@ -14,6 +17,10 @@ export interface LogFacesHTTPAppender {
1417
hostname?: string;
1518
/** The shared global config to include in your logs */
1619
configContext?: () => Record<string, string | number>;
20+
/** An http.Agent or https.Agent to allow configuring behavior as needed.
21+
* Make sure you use the correct type base on your url
22+
*/
23+
agent?: httpAgent | httpsAgent;
1724
}
1825

1926
// Add the LogFacesHTTPAppender to the list of appenders in log4js for better type support

0 commit comments

Comments
 (0)
0