Skip to content

Commit ce9147a

Browse files
authored
Merge pull request #32 from log4js-node/fixed-lint
ci: tests will now pass
2 parents 95b7948 + 692386e commit ce9147a

File tree

9 files changed

+5245
-4568
lines changed

9 files changed

+5245
-4568
lines changed

.eslintrc

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
{
22
"root": true,
3-
"extends": "airbnb-base",
4-
"rules": {
5-
"comma-dangle": 0,
6-
"indent": 2,
7-
"object-shorthand": 0,
8-
"func-names": 0,
9-
"max-len": [1, 120, 2],
10-
"no-use-before-define": ["warn"],
11-
"no-param-reassign": 0,
3+
"extends": ["airbnb-base", "prettier"],
4+
"plugins": ["import"],
5+
"rules": {
6+
"comma-dangle": 0,
7+
"indent": 2,
8+
"object-shorthand": 0,
9+
"func-names": 0,
10+
"max-len": [1, 120, 2],
11+
"no-use-before-define": ["warn"],
12+
"no-param-reassign": 0,
1213
"strict": 0,
1314
"import/no-extraneous-dependencies": 1,
1415
"prefer-spread": 0,
15-
"prefer-rest-params": 0,
16-
"prefer-destructuring": 0
17-
},
16+
"prefer-rest-params": 0,
17+
"prefer-destructuring": 0
18+
},
1819
"parserOptions": {
1920
"ecmaVersion": 6
20-
}
21+
},
22+
"ignorePatterns": ["coverage/**/*", "commitlint.config.js"]
2123
}

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage
2+
.github
3+
.nyc_output

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ npm install log4js @log4js-node/logstash-http
88

99
## Configuration
1010

11-
* `type` - `@log4js-node/logstash-http`
12-
* `url` - `string` - logFaces receiver servlet URL
13-
* `application` - `string` (optional) - used to identify your application's logs
14-
* `logChannel` - `string` (optional) - also used to identify your application's logs [but in a more specific way]
15-
* `logType` - `string` (optional) - used for the `type` field in the logstash data
16-
* `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.
11+
- `type` - `@log4js-node/logstash-http`
12+
- `url` - `string` - logFaces receiver servlet URL
13+
- `application` - `string` (optional) - used to identify your application's logs
14+
- `logChannel` - `string` (optional) - also used to identify your application's logs [but in a more specific way]
15+
- `logType` - `string` (optional) - used for the `type` field in the logstash data
16+
- `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.
1818

1919
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.
2020

@@ -23,16 +23,23 @@ This appender will also pick up Logger context values from the events, and add t
2323
```javascript
2424
log4js.configure({
2525
appenders: {
26-
logstash: { type: '@log4js-node/logstash-http', url: 'http://localhost:9200/_bulk', application: 'logstash-log4js', logType: 'application', logChannel: 'node' }
26+
logstash: {
27+
type: "@log4js-node/logstash-http",
28+
url: "http://localhost:9200/_bulk",
29+
application: "logstash-log4js",
30+
logType: "application",
31+
logChannel: "node",
32+
},
2733
},
2834
categories: {
29-
default: { appenders: [ 'logstash' ], level: 'info' }
30-
}
35+
default: { appenders: ["logstash"], level: "info" },
36+
},
3137
});
3238

3339
const logger = log4js.getLogger();
34-
logger.addContext('requestId', '123');
35-
logger.info('some interesting log message');
36-
logger.error('something has gone wrong');
40+
logger.addContext("requestId", "123");
41+
logger.info("some interesting log message");
42+
logger.error("something has gone wrong");
3743
```
44+
3845
This example will result in two log events being sent to your `localhost:9200`. Both events will have a `context.requestId` property with a value of `123`.

lib/index.js

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
'use strict';
1+
"use strict";
22

33
/**
44
* logstashHTTP appender sends JSON formatted log events to logstashHTTP receivers.
55
*/
6-
const util = require('util');
7-
const axios = require('axios');
8-
9-
function wrapErrorsWithInspect(items) {
10-
return items.map((item) => {
11-
if ((item instanceof Error) && item.stack) {
12-
return {
13-
inspect: function () {
14-
return `${util.format(item)}\n${item.stack}`;
15-
}
16-
};
17-
}
18-
19-
return item;
20-
});
21-
}
6+
const util = require("util");
7+
const axios = require("axios");
228

239
function format(logData) {
24-
return util.format.apply(util, wrapErrorsWithInspect(logData));
10+
return util.format(...logData);
2511
}
2612

2713
/**
@@ -40,7 +26,7 @@ function logstashHTTPAppender(config) {
4026
const sender = axios.create({
4127
baseURL: config.url,
4228
timeout: config.timeout || 5000,
43-
headers: { 'Content-Type': 'application/x-ndjson' },
29+
headers: { "Content-Type": "application/x-ndjson" },
4430
withCredentials: true,
4531
// The user should pass in the correct Agent type for their url
4632
// since their url won't change after config this should be fine
@@ -62,25 +48,28 @@ function logstashHTTPAppender(config) {
6248
level: event.level.level / 100,
6349
level_name: event.level.levelStr,
6450
channel: config.logChannel,
65-
datetime: (new Date(event.startTime)).toISOString(),
51+
datetime: new Date(event.startTime).toISOString(),
6652
extra: {},
6753
},
6854
];
69-
const logstashJSON = `${JSON.stringify(logstashEvent[0])}\n${JSON.stringify(logstashEvent[1])}\n`;
55+
const logstashJSON = `${JSON.stringify(logstashEvent[0])}\n${JSON.stringify(
56+
logstashEvent[1]
57+
)}\n`;
7058

7159
// send to server
72-
sender.post('', logstashJSON)
73-
.catch((error) => {
74-
if (error.response) {
75-
// eslint-disable-next-line
76-
console.error(
77-
`log4js.logstashHTTP Appender error posting to ${config.url}: ${error.response.status} - ${JSON.stringify(error.response.data)}`
78-
);
79-
} else {
80-
// eslint-disable-next-line
81-
console.error(`log4js.logstashHTTP Appender error: ${error.message}`);
82-
}
83-
});
60+
sender.post("", logstashJSON).catch((error) => {
61+
if (error.response) {
62+
// eslint-disable-next-line
63+
console.error(
64+
`log4js.logstashHTTP Appender error posting to ${config.url}: ${
65+
error.response.status
66+
} - ${JSON.stringify(error.response.data)}`
67+
);
68+
} else {
69+
// eslint-disable-next-line
70+
console.error(`log4js.logstashHTTP Appender error: ${error.message}`);
71+
}
72+
});
8473
};
8574
}
8675

0 commit comments

Comments
 (0)