Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: yssk22/fluent-logger-node
base: dc3c26ac3a
...
head fork: yssk22/fluent-logger-node
compare: 03d8d97bbe
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
22 README.md
@@ -18,19 +18,33 @@ Fluent daemon should listen on TCP port.
// The 2nd argument can be omitted. Here is a defualt value for options.
logger.configure('tag', {
host: 'localhost',
- port: 24224
+ port: 24224,
timeout: 3.0
});
// send an event record with 'tag.label'
logger.emit('label', {record: 'this is a log'});
-## Build Status
+## Logging Library Support
-http://jenkins-yssk22.dotcloud.com/job/fluent-logger-node/
+### log4js
+
+Befre using [log4js] support, you should install it IN YOUR APPLICATION.
+
+
+ var log4js = require('log4js');
+ log4js.addAppender(require('fluent-logger').support.log4jsAppender('mytag', {
+ host: 'localhost',
+ port: 24224,
+ timeout: 3.0
+ }));
+
+ var logger = log4js.getLogger('foo');
+ logger.info('this log record is sent to fluent daemon');
## License
-Apache License, Version 2.0
+Apache License, Version 2.0.
[fluent-logger-python]: https://github.com/fluent/fluent-logger-python
+
View
7 lib/index.js
@@ -18,5 +18,12 @@ module.exports = {
createFluentSender: function(tag, options){
return new FluentSender(tag, options);
+ },
+
+ support: {
+ log4jsAppender: function(options){
+ var log4jsSupport = require('../lib/log4js');
+ return log4jsSupport.appender(options);
+ }
}
}
View
14 lib/log4js.js
@@ -2,6 +2,7 @@
* log4js appender support
*/
var sender = require('./sender');
+var util = require('util');
var DEFAULT_TAG = 'log4js';
function fluentAppender(tag, options){
@@ -19,15 +20,16 @@ function fluentAppender(tag, options){
logSender.end();
});
var appender = function(loggingEvent){
+ var data = util.format.apply(null, loggingEvent.data);
var rec = {
- startTime: loggingEvent.startTime,
- categoryName: loggingEvent.categoryName,
- data: loggingEvent.data,
- level: loggingEvent.level
+ timestamp: loggingEvent.startTime.getTime(),
+ category: loggingEvent.categoryName,
+ levelInt: loggingEvent.level.level,
+ levelStr: loggingEvent.level.levelStr,
+ data: data
};
- logSender.emit(rec);
+ logSender.emit(loggingEvent.level.levelStr, rec);
};
- appender.sender = logSender;
return appender;
}
View
106 lib/testHelper.js
@@ -1,35 +1,101 @@
+var net = require('net');
+var fs = require('fs');
var spawn = require('child_process').spawn;
var configPath = require('path').join(__dirname, '../test/fluent-config/fluent.conf');
var fluentdPath = process.env['FLUENTD_PATH'] || "fluentd";
var fluentd = null;
+
+function spawnFluentDaemon(callback){
+ var s = net.createServer();
+ // grab a random port
+ s.listen(function(){
+ var port = s.address().port;
+ var configPath = '/tmp/fluent-logger-node-test.' + port + '.conf';
+ s.on('close', function(){
+ var data = ["<source>",
+ "type forward",
+ "port " + port,
+ "</source>",
+ "<match debug.**>",
+ "type stdout",
+ "</match>"].join('\n');
+ fs.writeFileSync(configPath, data);
+ var fluentd = spawn(fluentdPath, ['-c', configPath]);
+ process.on('exit', function(){
+ if( fluentd && fluentd.exitCode === null ){
+ console.error('fluentd process remains. force to kill.');
+ try{
+ fluentd.kill();
+ }catch(e){
+ console.error(e);
+ }
+ }
+ });
+ callback(port, fluentd);
+ });
+ s.close();
+ });
+}
+
module.exports = {
- fluentd: function(done){
- if( fluentd ){
- done(null, fluentd);
- }else{
- var callbacked = false;
+ /**
+ * fluentd process utility
+ *
+ * fluentd(function(port, end){
+ * // send contents to port
+ * // ...
+ *
+ * end(function(receivedData){
+ * // check receivedData array
+ * receivedData[i].tag // -> tag
+ * receivedData[i].record // -> tag
+ * });
+ *
+ * });
+ *
+ */
+
+ fluentd: function(ready){
+ spawnFluentDaemon(function(port, fluentd){
+ var lines = [];
var s = "";
- fluentd = spawn(fluentdPath, ['-c', configPath]);
+ function finish(callback){
+ fluentd.kill();
+ fluentd.on('exit', function(){
+ callback(lines);
+ });
+ };
+
+
fluentd.stdout.on('data', function(data){
- // console.log(data.toString());
- if( !callbacked ){
- s += data.toString();
- if( s.match(/listening fluent socket/ig) ){
- callbacked = true;
- done(null, fluentd);
+ // process.stdout.write(data.toString());
+ s += data.toString();
+ if( s.indexOf('\n') > 0 ){
+ var l = s.split('\n'); // line0, line1, ..., chunk
+ var line = '';
+ for(var i=0; i < l.length - 1; i++){
+ line = l[i];
+ if( line.match(/listening fluent socket/ig) ){
+ process.nextTick(function(){
+ ready(port, finish);
+ });
+ }
+ if( line.match(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \+\d{4} (.+): (.+)$/) ){
+ try{
+ lines.push({
+ tag: RegExp.$1, data: JSON.parse(RegExp.$2)
+ });
+ }catch(e){
+ console.error(e + ' ' + RegExp.$2);
+ }
+ }
}
+ s = l[l.length-1];
}
});
fluentd.stderr.on('data', function(data){
console.error(data.toString());;
});
- }
+ });
}
};
-
-process.on('exit', function(){
- if( fluentd ){
- fluentd.kill();
- process.exit(0);
- }
-});
View
9 test/fluent-config/fluent.conf
@@ -1,9 +0,0 @@
-<source>
- type tcp
-</source>
-
-## match tag=debug.** and dump to console
-<match debug.**>
- type stdout
-</match>
-
View
40 test/test.log4js.js
@@ -5,12 +5,6 @@ var fluentd = require('../lib/testHelper').fluentd;
log4js.restoreConsole();
describe("log4js", function(){
- before(function(done){
- fluentd(function(err, fluentd){
- done();
- });
- });
-
describe('name', function(){
it('should be "fluent"', function(done){
expect(log4jsSupport.name).to.be.equal('fluent');
@@ -22,24 +16,28 @@ describe("log4js", function(){
var appender = null;
before(function(done){
log4js.clearAppenders();
- appender = log4jsSupport.appender('test-log4js');
- log4js.addAppender(appender);
done();
});
- after(function(done){
- appender.sender.end();
- done();
- });
-
- it('should', function(done){
- var logger = log4js.getLogger();
- logger.info('foobar');
- // FIXME
- setTimeout(function(){
- done();
- }, 1000);
+ it('should send log records', function(done){
+ fluentd(function(port, finish){
+ var appender = log4jsSupport.appender('debug', {port: port});
+ log4js.addAppender(appender);
+ var logger = log4js.getLogger('mycategory');
+ logger.info('foo %s', 'bar');
+ setTimeout(function(){
+ finish(function(data){
+ expect(data[0].tag).to.be.equal('debug.INFO');
+ expect(data[0].data).exist;
+ expect(data[0].data.data).to.be.equal('foo bar');
+ expect(data[0].data.category).to.be.equal('mycategory');
+ expect(data[0].data.timestamp).exist;
+ expect(data[0].data.levelInt).exist;
+ expect(data[0].data.levelStr).to.be.equal('INFO');
+ done();
+ });
+ }, 1000);
+ });
});
});
-
});
View
136 test/test.sender.js
@@ -1,84 +1,84 @@
var expect = require('chai').expect;
var sender = require('../lib/sender');
var fluentd = require('../lib/testHelper').fluentd;
+var async = require('async');
-describe("sender", function(){
- describe("FluentSernder", function(){
- before(function(done){
- fluentd(function(err, fluentd){
- done();
+describe("FluentSender", function(){
+ it('shoud send records', function(done){
+ fluentd(function(port, finish){
+ var s1 = new sender.FluentSender('debug', { port: port });
+ var emits = [];
+ for(var i=0; i<10; i++){
+ (function(k){
+ emits.push(function(done){ s1.emit('record', k, done); });
+ })(i);
+ }
+ emits.push(function(){
+ finish(function(data){
+ expect(data.length).to.be.equal(10);
+ for(var i=0; i<10; i++){
+ expect(data[i].tag).to.be.equal("debug.record");
+ expect(data[i].data).to.be.equal(i);
+ }
+ done();
+ });
});
+ async.series(emits);
});
+ });
- it('should send an object and close connection.', function(done){
- var s = new sender.FluentSender('debug');
- var record = {
- string: 'a',
- number: 1,
- object: {
- a: 'b'
- },
- array: [1,2,3],
- bool: true,
- null: null,
- undefined: undefined
- };
- s.end('test send object', record, function(){
- // FIXME make sure the connection is closed.
- done();
- });
- }); // should send an object;
-
- it('should use a queue and flush it after it ends.', function(done){
- var s = new sender.FluentSender('debug');
- var called = 0;
- s.emit('1st record', '1st record', function(){
- called++;
- expect(called).to.be.equal(1); // confirm callbacks are called.
- });
- s.emit('2nd record', '2nd record', function(){
- called++;
- expect(called).to.be.equal(2); // confirm callbacks are called.
- });
- s.end('last record', 'last record', function(){
- called++;
- expect(called).to.be.equal(3); // confirm callbacks are called.
- expect(s._sendQueue.length).to.be.equal(0);
- done();
- });
- expect(s._sendQueue.length).to.be.equal(3);
+ it('should raise error when connection fails', function(done){
+ var s = new sender.FluentSender('debug', {
+ host: 'localhost',
+ port: 65535
});
-
- it('should raise error when connection fails', function(done){
- var s = new sender.FluentSender('debug', {
- host: 'localhost',
- port: 65535
- });
- s.on('error', function(err){
- expect(err.code).to.be.equal('ECONNREFUSED');
- done();
- });
- s.emit('test connection error', 'foobar');
+ s.on('error', function(err){
+ expect(err.code).to.be.equal('ECONNREFUSED');
+ done();
});
+ s.emit('test connection error', 'foobar');
+ });
- it('should resume the connection automatically and flush the queue', function(done){
- var s = new sender.FluentSender('queuing', {
- host: 'localhost',
- port: 65535
- });
- s.on('error', function(err){
- expect(err.code).to.be.equal('ECONNREFUSED');
- expect(s._sendQueue.length, 1); // buffered queue
- // set the correct port.
- s.port = 24224;
- s.end('test queueing', 'done', function(){
- expect(s._sendQueue.length).to.be.equal(0);
+
+ it('should assure the sequence.', function(done){
+ fluentd(function(port, finish){
+ var s = new sender.FluentSender('debug', {port: port});
+ s.emit('1st record', '1st data');
+ s.emit('2nd record', '2nd data');
+ s.end('last record', 'last data', function(){
+ finish(function(data){
+ expect(data[0].tag).to.be.equal('debug.1st record');
+ expect(data[0].data).to.be.equal('1st data');
+ expect(data[1].tag).to.be.equal('debug.2nd record');
+ expect(data[1].data).to.be.equal('2nd data');
+ expect(data[2].tag).to.be.equal('debug.last record');
+ expect(data[2].data).to.be.equal('last data');
done();
});
- expect(s._sendQueue.length).to.be.equal(2);
});
- s.emit('test queueing', 'pended');
- expect(s._sendQueue.length).to.be.equal(1);
});
});
+
+ it('should resume the connection automatically and flush the queue', function(done){
+ var s = new sender.FluentSender('debug');
+ s.emit('1st record', '1st data');
+ s.on('error', function(err){
+ expect(err.code).to.be.equal('ECONNREFUSED');
+ fluentd(function(port, finish){
+ s.port = port;
+ s.emit('2nd record', '2nd data');
+ s.end('last record', 'last data', function(){
+ finish(function(data){
+ expect(data[0].tag).to.be.equal('debug.1st record');
+ expect(data[0].data).to.be.equal('1st data');
+ expect(data[1].tag).to.be.equal('debug.2nd record');
+ expect(data[1].data).to.be.equal('2nd data');
+ expect(data[2].tag).to.be.equal('debug.last record');
+ expect(data[2].data).to.be.equal('last data');
+ done();
+ });
+ });
+ });
+ });
+ });
});

No commit comments for this range

Something went wrong with that request. Please try again.