Skip to content

Commit

Permalink
add option to set/unset user-agent header (#96)
Browse files Browse the repository at this point in the history
* add option to set/unset user-agent header

* add unit test and fix indentation

* update test for user-agent

* update indentation

* update description of the field

* fix typo

* refactor assign headers

* update version add github action npm-publish

* update version in package-lock.json

* upgrade moment to 2.29.2
  • Loading branch information
resdenia authored Jun 26, 2022
1 parent 2d6c1d0 commit ab2a44a
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 35 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
on:
release:
types: [published]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 17
- run: npm install
- run: npm test
- uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.NPM_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.history
.prettierignore
node_modules
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ logger.log(obj);
* **addTimestampWithNanoSecs** - Add a timestamp with nano seconds granularity. This is needed when many logs are sent in the same millisecond, so you can properly order the logs in kibana. The added timestamp field will be `@timestamp_nano` Default: `false`
* **compress** - If true the the logs are compressed in gzip format. Default: `false`
* **internalLogger** - set internal logger that supports the function log. Default: console.
* **setUserAgent** - Set `false` to send logs without user-agent field in request header. Default:`true`.
* **extraFields** - Adds your own custom fields to each log. Add in JSON Format, for example: `extraFields : { field_1: "val_1", field_2: "val_2" , ... }`.


Expand All @@ -59,6 +60,12 @@ A few notes are worth mentioning regarding the use of the UDP protocol:


## Update log
**2.0.4**
- Add parameter to manage User-agent

**2.0.3**
- Add verbose logging to use in Azure serverless function

**2.0.2**
- Updated required fields for typescript

Expand Down
43 changes: 22 additions & 21 deletions lib/logzio-nodejs.d.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
interface ILoggerOptions {
token: string;
host?: string;
type?: string;
sendIntervalMs?: number;
bufferSize?: number;
debug?: boolean;
numberOfRetries?: number;
supressErrors?: boolean;
addTimestampWithNanoSecs?: boolean;
compress?: boolean;
internalLogger?: { log(message: string, ...args: any[]): any } & Record<string, any>;
protocol?: string;
port?: string;
timeout?: number;
sleepUntilNextRetry?: number;
callback?: (err: Error, bulk: object) => void;
extraFields?: {};
token: string;
host?: string;
type?: string;
sendIntervalMs?: number;
bufferSize?: number;
debug?: boolean;
numberOfRetries?: number;
supressErrors?: boolean;
addTimestampWithNanoSecs?: boolean;
compress?: boolean;
internalLogger?: { log(message: string, ...args: any[]): any } & Record<string, any>;
protocol?: string;
setUserAgent?: boolean;
port?: string;
timeout?: number;
sleepUntilNextRetry?: number;
callback?: (err: Error, bulk: object) => void;
extraFields?: {};
}

interface ILogzioLogger extends ILoggerOptions {
jsonToString(json: string): string;
log(msg: any, obj?: object): void;
close(): void;
sendAndClose(callback?: (error: Error, bulk: object) => void): void;
jsonToString(json: string): string;
log(msg: any, obj?: object): void;
close(): void;
sendAndClose(callback?: (error: Error, bulk: object) => void): void;
}

export function createLogger(options: ILoggerOptions): ILogzioLogger;
Expand Down
5 changes: 3 additions & 2 deletions lib/logzio-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class LogzioLogger {
timeout,
sleepUntilNextRetry = 2 * 1000,
callback = this._defaultCallback,
setUserAgent = true,
extraFields = {},
}) {
if (!token) {
Expand All @@ -71,7 +72,7 @@ class LogzioLogger {
this.compress = compress;
this.internalLogger = internalLogger;
this.sleepUntilNextRetry = sleepUntilNextRetry;

this.setUserAgent = setUserAgent;
this.timer = null;
this.closed = false;

Expand Down Expand Up @@ -258,8 +259,8 @@ class LogzioLogger {
headers: {
host: this.host,
accept: '*/*',
'user-agent': USER_AGENT,
'content-type': 'text/plain',
...(this.setUserAgent ? { 'user-agent': USER_AGENT } : {}),
},
};

Expand Down
19 changes: 10 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "logzio-nodejs",
"description": "A nodejs implementation for sending logs to Logz.IO cloud service Copy of logzio-nodejs",
"version": "2.0.3",
"version": "2.0.4",
"author": "Gilly Barr <gilly@logz.io>",
"contributors": [
{
Expand All @@ -23,7 +23,11 @@
{
"name": "Roni Shaham",
"email": "ronish31@gmail.com"
}
},
{
"name": "Anton Kolomiiets",
"email": "resdenia@gmail.com"
}
],
"repository": {
"type": "git",
Expand All @@ -39,7 +43,7 @@
"dependencies": {
"json-stringify-safe": "5.0.1",
"lodash.assign": "4.2.0",
"moment": "^2.24.0",
"moment": "2.29.2",
"request": "^2.88.0",
"request-promise": "^4.2.4"
},
Expand Down
19 changes: 19 additions & 0 deletions test/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ describe('logger', () => {
logger.close();
});

it('sends log without user-agent header', (done) => {
const logger = createLogger({
bufferSize: 1,
callback: onDone,
setUserAgent:false
});
sinon.spy(logger, '_tryToSend');

const logMsg = 'hello there from test';
logger.log(logMsg);

function onDone() {
assert.equal(logger._tryToSend.getCall(0).args[0].headers['user-agent'], undefined);
logger._tryToSend.restore();
logger.close();
done();
}
});

it('should send a log with an object as additional param', (done) => {
const logger = createLogger({
bufferSize: 1,
Expand Down

0 comments on commit ab2a44a

Please sign in to comment.