Skip to content

Commit

Permalink
Update logger listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
neolao committed Mar 1, 2013
1 parent 2d197b0 commit c6ffc25
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
2 changes: 2 additions & 0 deletions logger/ConsoleListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ proto.log = function(level, message)
seconds = '0' + seconds;
}
generatedMessage = '[' + year + '/' + month + '/' + day + ' ' + hours + ':' + minutes + ':' + seconds + ']';
generatedMessage += '[' + level.toUpperCase() + ']';
generatedMessage += ' ' + message;

// Display the message
console.log(generatedMessage);
};

Expand Down
68 changes: 62 additions & 6 deletions logger/FileListener.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
var neolao = require('../index'),
AbstractListener = require('./AbstractListener');
AbstractListener = require('./AbstractListener'),
path = require('path'),
fs = require('fs');



/**
* File listener of the logger
*
* @class neolao/logger/FileListener
* @class neolao/logger/FileListener
* @param String filePath The file path
* @param String level The filtered level
*/
module.exports = function()
module.exports = function(filePath, level)
{
this._filePath = filePath;
this._level = level;
};
module.exports.extends(AbstractListener);
proto = module.exports.prototype;


/**
* The file path
*
* @type String
*/
proto._filePath = null;

/**
* The filtered level
*
* @type String
*/
proto._level = null;


/**
* Get the representation string
*
Expand All @@ -32,14 +54,22 @@ proto.toString = function()
*/
proto.log = function(level, message)
{
var date = new Date(),
// Skip of the levels do not match
if (this._level && this._level !== level) {
return;
}


var self = this,
date = new Date(),
year = date.getFullYear(),
month = date.getMonth() + 1,
day = date.getDate(),
hours = date.getHours(),
minutes = date.getMinutes(),
seconds = date.getSeconds(),
generatedMessage;
generatedMessage,
directoryPath;

// Add the date
if (month < 10) {
Expand All @@ -58,8 +88,34 @@ proto.log = function(level, message)
seconds = '0' + seconds;
}
generatedMessage = '[' + year + '/' + month + '/' + day + ' ' + hours + ':' + minutes + ':' + seconds + ']';
generatedMessage += ' ' + message;
generatedMessage += ' ' + message + "\n";

// Create the directory if necessary
directoryPath = path.dirname(this._filePath);
fs.exists(directoryPath, function(exists) {
if (!exists) {
// Create the directory and append the message into the file
fs.mkdir(directoryPath, 0777, function(error) {
self._append(generatedMessage);
});
} else {
// The directory exists
// Append the message into the file
self._append(generatedMessage);
}
});

};

/**
* Append a message into the file
*
* @param String message The message
*/
proto._append = function(message)
{
fs.appendFile(this._filePath, message, 'utf8', function(error) {
});

};

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "neolao",
"description" : "Neolao library",
"version" : "0.1.1",
"version" : "0.1.2",
"author" : "neolao <contact@neolao.com>",
"private" : false,
"repository" : "git@github.com:neolao/nodejs.git",
Expand Down

0 comments on commit c6ffc25

Please sign in to comment.