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

ci: tests will now pass #32

Merged
merged 6 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
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
28 changes: 15 additions & 13 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
{
"root": true,
"extends": "airbnb-base",
"rules": {
"comma-dangle": 0,
"indent": 2,
"object-shorthand": 0,
"func-names": 0,
"max-len": [1, 120, 2],
"no-use-before-define": ["warn"],
"no-param-reassign": 0,
"extends": ["airbnb-base", "prettier"],
"plugins": ["import"],
"rules": {
"comma-dangle": 0,
"indent": 2,
"object-shorthand": 0,
"func-names": 0,
"max-len": [1, 120, 2],
"no-use-before-define": ["warn"],
"no-param-reassign": 0,
"strict": 0,
"import/no-extraneous-dependencies": 1,
"prefer-spread": 0,
"prefer-rest-params": 0,
"prefer-destructuring": 0
},
"prefer-rest-params": 0,
"prefer-destructuring": 0
},
"parserOptions": {
"ecmaVersion": 6
}
},
"ignorePatterns": ["coverage/**/*", "commitlint.config.js"]
}
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage
.github
.nyc_output
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ npm install log4js @log4js-node/logstash-http

## Configuration

* `type` - `@log4js-node/logstash-http`
* `url` - `string` - logFaces receiver servlet URL
* `application` - `string` (optional) - used to identify your application's logs
* `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.
- `type` - `@log4js-node/logstash-http`
- `url` - `string` - logFaces receiver servlet URL
- `application` - `string` (optional) - used to identify your application's logs
- `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.

Expand All @@ -23,16 +23,23 @@ This appender will also pick up Logger context values from the events, and add t
```javascript
log4js.configure({
appenders: {
logstash: { type: '@log4js-node/logstash-http', url: 'http://localhost:9200/_bulk', application: 'logstash-log4js', logType: 'application', logChannel: 'node' }
logstash: {
type: "@log4js-node/logstash-http",
url: "http://localhost:9200/_bulk",
application: "logstash-log4js",
logType: "application",
logChannel: "node",
},
},
categories: {
default: { appenders: [ 'logstash' ], level: 'info' }
}
default: { appenders: ["logstash"], level: "info" },
},
});

const logger = log4js.getLogger();
logger.addContext('requestId', '123');
logger.info('some interesting log message');
logger.error('something has gone wrong');
logger.addContext("requestId", "123");
logger.info("some interesting log message");
logger.error("something has gone wrong");
```

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`.
55 changes: 22 additions & 33 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
'use strict';
"use strict";

/**
* logstashHTTP appender sends JSON formatted log events to logstashHTTP receivers.
*/
const util = require('util');
const axios = require('axios');

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;
});
}
const util = require("util");
const axios = require("axios");

function format(logData) {
return util.format.apply(util, wrapErrorsWithInspect(logData));
return util.format(...logData);
}

/**
Expand All @@ -40,7 +26,7 @@ function logstashHTTPAppender(config) {
const sender = axios.create({
baseURL: config.url,
timeout: config.timeout || 5000,
headers: { 'Content-Type': 'application/x-ndjson' },
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
Expand All @@ -62,25 +48,28 @@ function logstashHTTPAppender(config) {
level: event.level.level / 100,
level_name: event.level.levelStr,
channel: config.logChannel,
datetime: (new Date(event.startTime)).toISOString(),
datetime: new Date(event.startTime).toISOString(),
extra: {},
},
];
const logstashJSON = `${JSON.stringify(logstashEvent[0])}\n${JSON.stringify(logstashEvent[1])}\n`;
const logstashJSON = `${JSON.stringify(logstashEvent[0])}\n${JSON.stringify(
logstashEvent[1]
)}\n`;

// send to server
sender.post('', logstashJSON)
.catch((error) => {
if (error.response) {
// eslint-disable-next-line
console.error(
`log4js.logstashHTTP Appender error posting to ${config.url}: ${error.response.status} - ${JSON.stringify(error.response.data)}`
);
} else {
// eslint-disable-next-line
console.error(`log4js.logstashHTTP Appender error: ${error.message}`);
}
});
sender.post("", logstashJSON).catch((error) => {
if (error.response) {
// eslint-disable-next-line
console.error(
`log4js.logstashHTTP Appender error posting to ${config.url}: ${
error.response.status
} - ${JSON.stringify(error.response.data)}`
);
} else {
// eslint-disable-next-line
console.error(`log4js.logstashHTTP Appender error: ${error.message}`);
}
});
};
}

Expand Down
Loading