Comparing version 0.0.2 to 0.0.3
@@ -1,19 +0,4 @@ | ||
/** | ||
* properties that can be set are as follows | ||
* | ||
* fileLocation (required) - the path to where the log files should be written too | ||
* appName (optional) - this is to help distinguish between different app logs - this will be more useful when feather is able to write to system-wide logs via syslog or rsyslog | ||
* timestampFormat (optional / recommend using) - this is the date format appearing in the log file. The one used here is apache style and is widely used. | ||
* dateFormat (required) - this is how the file name will appear as feather uses this to create the file name. Again, this conforms with current convention so I recommend not changing it | ||
* host (optional) - this is the host and again it will be useful when feather writes to system log facilities such as syslog | ||
* rootLevel (optional) - if set then only logs up to that level will be logged, others will be ignored. | ||
*/ | ||
module.exports = { | ||
"fileLocation": "logs/feather/", | ||
"appName": "Feather", | ||
"timestampFormat" : "ddd, dd MMM yyyy HH:mm:ss", | ||
"dateFormat" : "yyyyMMdd", | ||
"host" : '172.0.0.1', | ||
"rootLevel" : "Warn" | ||
"timestampFormat" : "dddd, MMMM Do YYYY, h:mm:ss a", | ||
"level" : "Info" | ||
} |
@@ -1,119 +0,38 @@ | ||
require('datejs'); | ||
var GLOBAL_CONFIG = '../../../config/featherProperties' | ||
var sys = require('sys'); | ||
var fs = require('fs'); | ||
try { // Try loading configuration from the user application level | ||
var config = require(GLOBAL_CONFIG); | ||
} catch(e) { // if one does not exist then use the one set here | ||
config = require('../config') | ||
} | ||
var levels = require('../config/loggerLevels'); | ||
var appName = config.appName; | ||
var filename = config.fileLocation + new Date().toString(config.dateFormat); | ||
var Logger = exports; | ||
var util = require('util'); | ||
var moment = require('moment'); | ||
var config = require('loggerProperties') | ||
var timestamp = function() { | ||
return new Date().toString(config.timestampFormat); //TODO get the timestamp format from config too | ||
} | ||
Logger.LEVELS = [ | ||
'Emergency', | ||
'Alert', | ||
'Critical', | ||
'Error', | ||
'Warn', | ||
'Notice', | ||
'Info', | ||
'Debug' | ||
]; | ||
/** | ||
* calculate the root level and return a value | ||
* so if Info is stated in configuration then only log up to that level and no debug messages will be logged | ||
* | ||
* @return numerical value representing the log level to be used | ||
* | ||
* @param callback function | ||
*/ | ||
var getHighestLevelToLogToo = function(callback) { | ||
var rootLevel = config.rootLevel; | ||
if (rootLevel) { | ||
Object.keys(levels.alertLevels).forEach(function(key) { | ||
if (levels.alertLevels[key].toLowerCase() === rootLevel.toLowerCase()) { | ||
callback(null, key); | ||
} | ||
}); | ||
} else { // log everything | ||
Object.keys(levels.alertLevels).forEach(function(key) { | ||
if(levels.alertLevels[key].toLowerCase() === "debug") { | ||
callback(null, key); | ||
} | ||
}) | ||
} | ||
} | ||
Logger.LEVEL = Logger.LEVELS.indexOf(config.level); | ||
/** | ||
* Check if a given directory exists and if not, create it so feather can start logging | ||
* | ||
*/ | ||
var createRequiredDirectory = function() { | ||
var dirPath = ''; | ||
var dirs = config.fileLocation.split('/'); // get individual dirs from the give location in the config file. | ||
Logger.LEVELS.forEach(function(level, index) { | ||
var method = level.toLowerCase(); | ||
var target = (index > Logger.LEVELS.indexOf('Error')) | ||
? 'stdout' | ||
: 'stderr'; | ||
for(var i = 0; i < dirs.length; i++) { | ||
if(dirs[i] != '') { | ||
dirPath += (dirs[i] + '/'); | ||
try { | ||
if(!fs.lstatSync(dirPath)) { // check to see if directory already exists | ||
fs.mkdirSync(dirPath, '0755'); // if not then create it | ||
} | ||
} catch(error) { | ||
throw 'Failed to create directory ' + dirPath; | ||
} | ||
Logger[method] = function() { | ||
if (index > Logger.LEVEL) return; | ||
} | ||
} | ||
} | ||
var message = Logger.format.apply(Logger, arguments); | ||
process[target].write('[' + level + '] ' + message); | ||
}; | ||
}); | ||
/** | ||
* Go through the alert level JSON object and dynamically create a function which will log data for that alert code | ||
*/ | ||
var init = function() { | ||
createRequiredDirectory(); | ||
getHighestLevelToLogToo(function(error, alertLevelNumber) { | ||
Object.keys(levels.alertLevels).forEach(function(key) { | ||
Logger[levels.alertLevels[key].toLowerCase()] = function(message) { | ||
fs.open(filename, 'a', undefined, function(err, fd) { | ||
if (err) { | ||
throw err; | ||
} | ||
if (alertLevelNumber && key <= alertLevelNumber) { | ||
fs.write(fd, config.host + ' [' + timestamp() + | ||
'] ' + levels.alertLevels[key].toUpperCase() + | ||
': ' + appName + ': ' + message + '\n', undefined, undefined, | ||
function(err, written) { | ||
if (err) { | ||
throw err; | ||
} | ||
// sys.puts(written + ' bytes written'); | ||
fs.close(fd, function(err) { | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* Initialize logger | ||
*/ | ||
init(); | ||
/** | ||
* Tackle the following in order | ||
*/ | ||
//TODO - Create directories as required [DONE] | ||
//TODO - to read configuration from app level conf file. | ||
//TODO - Branch and rename to featherLog | ||
//TODO - allow log file locking so that one process can write to it at any one time. ~ careful that it does not slow things down though | ||
//TODO - start() stop() to yield time between the two | ||
//TODO - TCP / UDP connection | ||
Logger.format = function(/* template, arg1, arg2, ... */) { | ||
var message = util.format.apply(util, arguments); | ||
var timestamp = moment().format(config.timestampFormat); | ||
message = timestamp + ': ' + message + '\n'; | ||
return message; | ||
}; |
{ | ||
"name":"feather", | ||
"version":"0.0.2", | ||
"version":"0.0.3", | ||
"keywords": ["logger", "lightweight", "Unix style logging", "info debug warn"], | ||
@@ -11,5 +11,6 @@ "engines": { "node": "*"}, | ||
}, | ||
"repository": { "type": "git", "url": "git@github.com:holidayextras/feather.git"}, | ||
"repository": { "type": "git", "url": "git@github.com:holidayextras/featherLog.git"}, | ||
"dependencies":{ | ||
"datejs":"0.0.2" | ||
"utest" : "0.0.6", | ||
"utest" : "0.0.2" | ||
}, | ||
@@ -16,0 +17,0 @@ "licenses": [ |
var logger = require('../lib/logger'); | ||
var assert = require('assert'); | ||
var fail = []; | ||
var pass = []; | ||
var warn = []; | ||
var test = require('utest'); | ||
exports.runTest = function(cb) { | ||
var count = 0; | ||
var test = function(name, method, warning) { | ||
count++; | ||
try { | ||
method(); | ||
if (warning) { | ||
warn.push({test:count, method:name, message:warning}); | ||
} else { | ||
pass.push({test:count, method:name}); | ||
} | ||
} catch(e) { | ||
fail.push({test:count, method:name, exception:e}); | ||
test('Running tests', { | ||
'before' : function() { | ||
this.loggerLevels = ['info', 'warn', 'error', 'emergency', 'alert', 'critical', 'debug', 'notice']; | ||
}, | ||
'Test all available logger levels': function() { | ||
for (var i = 0; i < this.loggerLevels.length; i++) { | ||
test('all logger level \"' + this.loggerLevels[i] + '\", logs OK! Exception is thrown in event of a failure!', function() { | ||
try { | ||
logger[this.loggerLevels[i]]('Testing logger level ' + loggerLevels[i]); | ||
//also check log file for current date to ensure that all is logged properly! | ||
} catch (e) { | ||
throw(e); | ||
} | ||
}) | ||
} | ||
} | ||
var loggerLevels = ['info', 'warn', 'error', 'emergency', 'alert', 'critical', 'debug', 'notice'] | ||
for (var i = 0; i < loggerLevels.length; i++) { | ||
test('all logger level \"' + loggerLevels[i] + '\", logs OK! Exception is thrown in event of a failure!', function() { | ||
try { | ||
logger[loggerLevels[i]]('Testing logger level ' + loggerLevels[i]); | ||
//also check log file for current date to ensure that all is logged properly! | ||
} catch (e) { | ||
throw(e); | ||
} | ||
}) | ||
} | ||
test('Exception is thrown when logging with level that is not defined!', function() { | ||
}, | ||
'Exception is thrown when logging with level that is not defined!' : function() { | ||
assert.throws(function() { | ||
logger.foo('foobar'); | ||
}); | ||
}); | ||
cb(fail, warn, pass); | ||
} | ||
this.runTest(function(fail, warn, pass) { | ||
if (fail.size > 0 ) { | ||
fail.forEach(function(message) { | ||
console.log(message); | ||
}) | ||
}if (warn.size > 0 ) { | ||
fail.forEach(function(message) { | ||
console.log(message); | ||
}) | ||
}if (pass.size > 0 ) { | ||
fail.forEach(function(message) { | ||
console.log(message); | ||
}) | ||
} | ||
}) | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
27489
70
1
+ Addedutest@0.0.2
+ Addedutest@0.0.2(transitive)
- Removeddatejs@0.0.2
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removeddatejs@0.0.2(transitive)
- Removeddiff@4.0.2(transitive)
- Removedeyes@0.1.8(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedvows@0.8.3(transitive)
- Removedwrappy@1.0.2(transitive)