Skip to content

Commit 83d52e3

Browse files
alexk-blackopsalexk-blackops
authored andcommitted
project basic structure
1 parent 29e05fa commit 83d52e3

File tree

9 files changed

+204
-0
lines changed

9 files changed

+204
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var util = require('util'),
2+
access = require('./lib/access'),
3+
logger = require('./lib/logger'),
4+
api = require('./lib/api'),
5+
CONFIG = require('./lib/config'),
6+
sender = require('./sender');
7+
8+
module.exports = {
9+
CONFIG: CONFIG,
10+
11+
start: function () {
12+
13+
},
14+
15+
log: logger.methods.log,
16+
debug: logger.methods.debug,
17+
info: logger.methods.info,
18+
warn: logger.methods.warn,
19+
error: logger.methods.error,
20+
postLogs: api.postLogs
21+
}

lib/api.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var os = require('os'),
2+
send = require('./sender'),
3+
access = require('.access'),
4+
logger = require('./logger'),
5+
CONFIG = require('./config'),
6+
pkginfo = require('pkginfo')(module, 'name'),
7+
8+
options = {
9+
host: CONFIG.HOST,
10+
port: CONFIG.PORT,
11+
method: 'POST',
12+
headers: {
13+
Content-Type: 'application/json',
14+
X-Stackify-Key: CONFIG.LICENSE_KEY,
15+
X-Stackify-PV: 'V1'
16+
}
17+
}
18+
19+
module.exports = {
20+
21+
identifyApp: function identifyApp (licenseKey) {
22+
var options = options,
23+
data = {
24+
"DeviceName": os.hostname(),
25+
"AppName": module.exports.name
26+
},
27+
callback = function(data) {
28+
CONFIG.APP_DETAILS = data.toJSON();
29+
},
30+
fail = function() {
31+
setTimeout(function() {
32+
identifyApp(licenseKey)
33+
}, CONFIG.REQUEST_TIMER)
34+
}
35+
36+
options.path = CONFIG.IDENTIFY_PATH;
37+
38+
send(options, data, callback, fail);
39+
};
40+
41+
postLogs: function postLogs(messages, cb, fail) {
42+
var options = options,
43+
callback = cb || function(data) {
44+
logger.storage = logger.storage.slice(CONFIG.MSG_LIMIT)
45+
},
46+
fail = function() {
47+
setTimeout(function() {
48+
postLogs(messages, callback, fail)
49+
}, CONFIG.REQUEST_TIMER)
50+
},
51+
data = CONFIG.APP_DETAILS;
52+
53+
data.Msgs = messages;
54+
options.path = CONFIG.LOG_SAVE_PATH;
55+
56+
send(options, data, callback, fail);
57+
};
58+
}

lib/config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
'HOST': '1',
3+
'PORT': 1,
4+
'IDENTIFY_PATH': '1',
5+
'LOG_SAVE_PATH': '1',
6+
'LICENSE_KEY': '1',
7+
'MSG_LIMIT': 20, // number of messages
8+
'SCAN_TIMER': 30, // sec
9+
'REQUEST_TIMER': 30, // sec
10+
'REQUEST_ATTEMPTS': 20, // number of attempts if API call isn't succesful
11+
'ERROR_FLOOD_LIMIT': 100 // limit of the same error message per minute
12+
}

lib/error.js

Whitespace-only changes.

lib/logger.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var api = require('./api'),
2+
CONFIG = require('./config'),
3+
checkMeta = function(meta) {
4+
if (Object.prototype.toString.call(meta) === '[object Object]') {
5+
if (Object.keys(meta).length === 1) {
6+
try {
7+
JSON.stringify(meta);
8+
} catch(e) {
9+
return false;
10+
}
11+
return [Object.keys(meta)[0], meta[Object.keys(meta)[0]]];
12+
}
13+
}
14+
return false;
15+
}
16+
17+
module.exports.storage = storage = [];
18+
module.exports.flag = flag = true;
19+
module.exports.lastSent = lastSent = Date.now();
20+
module.exports.timeout = timeout = undefined;
21+
module.exports.methods = {
22+
23+
log: function log (level, msg, meta) {
24+
var rec = {
25+
level: level,
26+
msg: msg,
27+
timestamp: new Date().toUTCString()
28+
},
29+
metaIsValid = checkMeta(meta),
30+
msgs;
31+
32+
if (meta && metaIsValid) {
33+
rec[metaIsValid[0]] = metaIsValid[1];
34+
}
35+
36+
storage.push(rec);
37+
38+
if (storage.length >= CONFIG.MSG_LIMIT && CONFIG.APP_DETAILS) {
39+
msgs = storage.slice(0, CONFIG.MSG_LIMIT);
40+
api.postLogs(msgs);
41+
}
42+
},
43+
debug: function debug (msg) {
44+
this.log.call(this, 'debug', msg, meta);
45+
},
46+
info: function info(msg) {
47+
this.log.call(this, 'info', msg, meta);
48+
},
49+
warn: function warn (msg) {
50+
this.log.call(this, 'warn', msg, meta);
51+
},
52+
error: function error (msg) {
53+
this.log.call(this, 'error', msg, meta);
54+
}
55+
}

lib/sender.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module.exports = function sender(options, data, cb, fail) {
2+
3+
callback = function(response) {
4+
var str = '';
5+
6+
//another chunk of data has been recieved, so append it to `str`
7+
response.on('data', function (chunk) {
8+
str += chunk;
9+
});
10+
11+
//the whole response has been recieved, so we just print it out here
12+
response.on('end', function () {
13+
if (response.statusCode === 200) {
14+
cb(str);
15+
} else {
16+
if (fail) {
17+
fail();
18+
}
19+
}
20+
});
21+
},
22+
23+
req = http.request(options, callback);
24+
25+
req.write(data);
26+
req.end();
27+
};

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "stackify-logger",
3+
"version": "1.0.0",
4+
"description": "Stackify logging package",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "not specified yet"
8+
},
9+
"keywords": [
10+
"stackify",
11+
"logger"
12+
],
13+
"author": "Stackify",
14+
"license": "ISC",
15+
"dependencies": {
16+
"on-finished": "^2.1.1",
17+
"pkginfo": "^0.3.0"
18+
}
19+
}

test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var pkginfo = require('pkginfo')(module, 'name');
2+
var config = require('./lib/config');
3+
var stackify = require('./index');
4+
var test2 = require('./test2');
5+
stackify.warn('sdf');
6+
stackify.error('wefsdf');
7+
stackify.print();
8+
console.dir(module.exports.name);
9+
config.GHET = 'test';
10+
console.dir(config);
11+
test2();

0 commit comments

Comments
 (0)