Skip to content
Open
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
log4js-logstash-tcp
===============
[![Build Status](https://travis-ci.org/Aigent/log4js-logstash-tcp.svg?branch=master)](https://travis-ci.org/Aigent/log4js-logstash-tcp)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FAigent%2Flog4js-logstash-tcp.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FAigent%2Flog4js-logstash-tcp?ref=badge_shield)
[![Build Status](https://app.travis-ci.com/Degola/log4js-logstash-tcp.svg?branch=master)](https://app.travis-ci.com/Degola/log4js-logstash-tcp)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FAigent%2Flog4js-logstash-tcp.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2FAigent%2Flog4js-logstash-tcp?ref=badge_shield)

This is a copy of the logstashUDP appender but instead sending via UDP send via TCP to avoid the maximum 64k bytes message size with the logstashUDP appender

Expand Down Expand Up @@ -51,6 +51,8 @@ Plain javascript
});

var log = log4js.getLogger('tests');
// adding context and add it as additional field to each log entry
log.addContext('customer', {id: 123, name: 'John Doe'});

log.error('hello hello');
```
Expand All @@ -59,4 +61,4 @@ Plain javascript


## License
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FAigent%2Flog4js-logstash-tcp.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FAigent%2Flog4js-logstash-tcp?ref=badge_large)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FAigent%2Flog4js-logstash-tcp.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2FAigent%2Flog4js-logstash-tcp?ref=badge_large)
120 changes: 120 additions & 0 deletions __tests__/basic-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const net = require('net');
const log4jsAppender = require('../index.js');
const layouts = require('log4js/lib/layouts.js');

const context = {};
beforeEach(() => {
return new Promise((resolve) => {
context.config = {
"category": "TEST",
"type": "log4js-logstashTCP",
"host": "localhost",
"port": 5050,
"fields": {
"instance": "MyAwsInstance",
"source": "myApp",
"environment": "development"
}
};

context.testServer = net.createServer({
});
context.testServer.on('listening', function () {
resolve();
});
context.testServer.listen(context.config.port, context.config.host);
})
})
afterEach(() => {
return new Promise((resolve) => {
context.testServer.close();
context.testServer.on('close', function () {
delete context.testServer;
resolve();
});
// context.testServer.forceClose();
})
})
describe('check basic usage', () => {
"use strict";

it('testing logger setup, configuration and sending log message', (done) => {
const message = 'Im awesome';
let log = null;

try {
log = log4jsAppender.configure(context.config, layouts);
} catch (e) {
return fail('configure shouldn\'t throw an error');
}

context.testServer.on('connection', function (con) {
con.on('data', function (data) {
expect(JSON.parse(data.toString())['message']).toBe(message);
log.shutdown(() => {
done();
})
});
});
log({startTime: new Date(), level: {levelStr: 'FOO'}, categoryName: 'BAR', data: [message]});
})

it('testing logger setup, configuration and sending log message with context', (done) => {
const message = 'Im awesome';
let log = null;

try {
log = log4jsAppender.configure(context.config, layouts);
} catch (e) {
return fail('configure shouldn\'t throw an error');
}

context.testServer.on('connection', function (con) {
con.on('data', function (data) {
const parsedData = JSON.parse(data.toString())
expect(parsedData['message']).toBe(message);
expect(parsedData['testContextField']).toBe('test context field value')
log.shutdown(() => {
done();
})
});
});
log({
startTime: new Date(),
level: {levelStr: 'FOO'},
categoryName: 'BAR',
data: [message],
context: {
testContextField: "test context field value"
}});
})
it('testing logger setup, configuration and sending log message with context but gets overwritten by log message', (done) => {
const message = 'Im awesome';
let log = null;

try {
log = log4jsAppender.configure(context.config, layouts);
} catch (e) {
return fail('configure shouldn\'t throw an error');
}

context.testServer.on('connection', function (con) {
con.on('data', function (data) {
const parsedData = JSON.parse(data.toString());
expect(parsedData['message']).toBe(message);
expect(parsedData['testContextField']).toBe('context value overwritten');
log.shutdown(() => {
done();
})
});
});
log({
startTime: new Date(),
level: {levelStr: 'FOO'},
categoryName: 'BAR',
data: [message, {testContextField: "context value overwritten"}],
context: {
testContextField: "test context field value"
}});
})
})
29 changes: 26 additions & 3 deletions classes/TcpConnectionPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ class TcpConnectionWrapper extends EventEmitter {

}

destroy() {
this.connection.end();
this.connection.destroy();
destroy(cb) {
const self = this;
this.connection.end(() => {
self.connection.destroy();
if (typeof cb === "function") {
cb();
}
});

this.emit("destroy");
}
Expand Down Expand Up @@ -101,6 +106,24 @@ class TcpConnectionPool {
return Math.floor( Math.random() * this.config.maxTcpConnections) % this.config.maxTcpConnections;
}

close(cb) {
const connections = [];
Object.keys(this.tcpConnections).forEach((index) => {
connections.push(this.tcpConnections[index]);
});

let completed = 0;
const complete = () => {
if (++completed >= connections.length) {
if (typeof cb === "function") {
cb();
}
}
};
for (const tcpConnectionWrapper of connections) {
tcpConnectionWrapper.destroy(complete);
}
}
}

module.exports.TcpConnectionPool = TcpConnectionPool;
Expand Down
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const util = require('util');

const TcpConnectionPool = require("./classes/TcpConnectionPool").TcpConnectionPool;

const tcpConnectionPool = new TcpConnectionPool();
Expand Down Expand Up @@ -34,11 +32,7 @@ function logstashTCP(config, layout) {
return true;
}

if ((!logUnderFields) && (argsValue === 'direct')) {
return true;
}

return false;
return (!logUnderFields) && (argsValue === 'direct');
}

function log(loggingEvent) {
Expand All @@ -64,6 +58,18 @@ function logstashTCP(config, layout) {
Object.keys(config.fields).forEach((key) => {
fields[key] = typeof config.fields[key] === 'function' ? config.fields[key](loggingEvent) : config.fields[key];
});
// adding context to log messages as separate fields added by logger.addContext(key, value)
if(loggingEvent.context) {
if(loggingEvent.data.length === 1 || !loggingEvent.data[1]) {
loggingEvent.data[1] = {}
}
if(typeof loggingEvent.data[1] === 'object') {
loggingEvent.data[1] = {
...loggingEvent.context,
...loggingEvent.data[1]
}
}
}

/* eslint no-prototype-builtins:1,no-restricted-syntax:[1, "ForInStatement"] */
if (loggingEvent.data.length > 1) {
Expand Down Expand Up @@ -96,7 +102,7 @@ function logstashTCP(config, layout) {
}

log.shutdown = function (cb) {
cb();
tcpConnectionPool.close(cb);
};

return log;
Expand Down
23 changes: 16 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "log4js-logstash-tcp",
"version": "2.0.3",
"author": "Aigent B.V.",
"version": "2.0.5",
"author": "Sebastian Lagemann <sebastian@slab.rocks> (https://slab.rocks)",
"description": "This is a copy of the logstashUDP appender but instead sending via UDP send via TCP to avoid the maximum 64k bytes message size with the logstashUDP appender.",
"keywords": [
"logstash",
Expand All @@ -12,6 +12,15 @@
{
"name": "Sebastian Lagemann",
"email": "sebastian@aigent.com"
},
{
"name": "Sebastian Lagemann",
"email": "sebastian@slab.rocks",
"url": "https://slab.rocks"
},
{
"name": "Lam Wei Li",
"url": "https://github.com/lamweili"
}
],
"licenses": [
Expand All @@ -22,17 +31,17 @@
],
"repository": {
"type": "git",
"url": "https://github.com/Aigent/log4js-logstash-tcp.git"
"url": "https://github.com/Degola/log4js-logstash-tcp"
},
"devDependencies": {
"log4js": "^2.4.1",
"nodeunit": "^0.11.2"
"jest": "^29.5.0",
"log4js": "^6.9.1"
},
"scripts": {
"test": "nodeunit tests"
"test": "jest --coverage --detectOpenHandles"
},
"engines": {
"node": "0.10.x"
"node": ">=6.4.0"
},
"main": "./index.js"
}
54 changes: 0 additions & 54 deletions tests/test.js

This file was deleted.