Skip to content

Commit

Permalink
[FEAT] Added Winston logger
Browse files Browse the repository at this point in the history
  • Loading branch information
GeebToo committed May 13, 2016
1 parent 3814c58 commit 4ef5077
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 deletions.
1 change: 1 addition & 0 deletions config.json.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"logLevel" : "warn",
"consumer_key": "clientID",
"consumer_secret": "clientSecret",
"access_token_key": "tokenKey",
Expand Down
40 changes: 25 additions & 15 deletions get_tweet_woker.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
var amqp = require('amqplib/callback_api');
var Twitter = require('twitter');
var winston = require('winston');
var config = require('./config.json');

var transports = [];
transports.push(new (winston.transports.Console)({
timestamp: true,
colorize: true,
handleExceptions: false
}));
var logger = new (winston.Logger)({
level: config.logLevel,
transports: transports
});

var client = new Twitter({
consumer_key: config.consumer_key,
consumer_secret: config.consumer_secret,
access_token_key: config.access_token_key,
access_token_secret: config.access_token_secret,
});
var rabbitHost = config.rabbitmq_host;

var phrase = "JE VEUX";

var tweetId = 0;
var queue = config.rabbitmq_queue;
var queueOptions = {
Expand All @@ -27,21 +37,21 @@ function init() {
amqp.connect(rabbitHost + '?heartbeat=60', function(err, conn) {

if (err) {
console.error('[AMQP]', err.message);
return setTimeout(start, 1000);
logger.error('[AMQP]', err.message);
return setTimeout(init, 1000);
}

conn.on('error', function(err) {
if (err.message !== 'Connection closing') {
console.error('[AMQP] conn error', err.message);
logger.error('[AMQP] conn error', err.message);
}
});
conn.on('close', function() {
console.error('[AMQP] reconnecting');
return setTimeout(start, 1000);
logger.error('[AMQP] reconnecting');
return setTimeout(init, 1000);
});

console.log('[AMQP] connected');
logger.info('[AMQP] connected');
amqpConn = conn;
whenConnected();
});
Expand All @@ -61,11 +71,11 @@ function startPublisher() {
if (closeOnErr(err)) return;

ch.on('error', function(err) {
console.error('[AMQP] channel error', err.message);
logger.error('[AMQP] channel error', err.message);
});

ch.on('close', function() {
console.log('[AMQP] channel closed');
logger.info('[AMQP] channel closed');
});

//Read old messages if the worker lost connection
Expand All @@ -90,13 +100,13 @@ function publish(exchange, routingKey, content) {
persistent: true
}, function(err, ok) {
if (err) {
console.error('[AMQP] publish', err);
logger.error('[AMQP] publish', err);
offlinePubQueue.push([exchange, routingKey, content]);
pubChannel.connection.close();
}
});
} catch (e) {
console.error('[AMQP] publish', e.message);
logger.error('[AMQP] publish', e.message);
offlinePubQueue.push([exchange, routingKey, content]);
}
}
Expand All @@ -106,7 +116,7 @@ function closeOnErr(err) {
return false;
}

console.error('[AMQP] error', err);
logger.error('[AMQP] error', err);
amqpConn.close();
return true;
}
Expand Down Expand Up @@ -136,8 +146,8 @@ setInterval(function() {
publish('', queue, new Buffer(phrase + tweetSplit[0]));
}
} else {
console.log('[TWEETER] same tweet : ' + tweets.statuses[i].text);
console.log(tweets.statuses.length + ' i = ' + i);
logger.debug('[TWEETER] same tweet : ' + tweets.statuses[i].text);
logger.info(tweets.statuses.length + ' i = ' + i);
}

if (i == tweets.statuses.length - 1) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"amqplib": "^0.4.1",
"moment": "^2.12.0",
"twitter": "^1.2.5"
"twitter": "^1.2.5",
"winston": "^2.2.0"
}
}
37 changes: 20 additions & 17 deletions read_tweet_woker.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
var amqp = require('amqplib/callback_api');
var Twitter = require('twitter');
var winston = require('winston');
var config = require('./config.json');

var client = new Twitter({
consumer_key: config.consumer_key,
consumer_secret: config.consumer_secret,
access_token_key: config.access_token_key,
access_token_secret: config.access_token_secret,
var transports = [];
transports.push(new (winston.transports.Console)({
timestamp: true,
colorize: true,
handleExceptions: false
}));
var logger = new (winston.Logger)({
level: config.logLevel,
transports: transports
});
var rabbitHost = config.rabbitmq_host;

var queue = config.rabbitmq_queue;
var queueOptions = {
durable: true
Expand All @@ -31,21 +34,21 @@ function init() {
amqp.connect(rabbitHost + '?heartbeat=60', function(err, conn) {

if (err) {
console.error('[AMQP]', err.message);
return setTimeout(start, 1000);
logger.error('[AMQP]', err.message);
return setTimeout(init, 1000);
}

conn.on('error', function(err) {
if (err.message !== 'Connection closing') {
console.error('[AMQP] conn error', err.message);
logger.error('[AMQP] conn error', err.message);
}
});
conn.on('close', function() {
console.error('[AMQP] reconnecting');
return setTimeout(start, 1000);
logger.error('[AMQP] reconnecting');
return setTimeout(init, 1000);
});

console.log('[AMQP] connected');
logger.info('[AMQP] connected');
amqpConn = conn;
startWorker();
});
Expand All @@ -60,10 +63,10 @@ function startWorker() {
}

ch.on('error', function(err) {
console.error('[AMQP] channel error', err.message);
logger.error('[AMQP] channel error', err.message);
});
ch.on('close', function() {
console.log('[AMQP] channel closed');
logger.info('[AMQP] channel closed');
});

ch.prefetch(1);
Expand All @@ -76,7 +79,7 @@ function startWorker() {
ch.consume(queue, processMsg, {
noAck: false
});
console.log('Worker is started');
logger.info('Worker is started');
});

function processMsg(msg) {
Expand Down Expand Up @@ -122,7 +125,7 @@ function closeOnErr(err) {
return false;
}

console.error('[AMQP] error', err);
logger.error('[AMQP] error', err);
amqpConn.close();
return true;
}
Expand Down

0 comments on commit 4ef5077

Please sign in to comment.