Permalink
Browse files

Working version - both exceptions and logs

  • Loading branch information...
martinrusev
martinrusev committed Nov 9, 2011
1 parent 5c4e352 commit 67e81e1d3b51fe77b207a70b12d7a63b4041fcb5
Showing with 94 additions and 20 deletions.
  1. +20 −0 example.js
  2. +70 −16 lib/amon.js
  3. +4 −4 package.json
View
@@ -0,0 +1,20 @@
+var Amon = require('./lib/amon').Amon;
+
+Amon.log('node.js unset message')
+Amon.log('node.js debug message', 'debug')
+Amon.log('node.js warning message', 'warning')
+Amon.log('node.js info message', 'info')
+
+process.addListener('uncaughtException', function(err) {
+ Amon.handle(err);
+});
+
+//try {
+ //throw new Error("Test Error 1");
+//} catch(error) {
+ //console.log("Error occurred ", error.message);
+ //Amon.handle(error);
+//}
+
+
+throw new Error("Test Error 2");
View
@@ -1,18 +1,49 @@
-var HTTP = require('http');
+var http = require('http');
var fs = require('fs');
+
+var default_host = '127.0.0.1'
+var default_port = 2464
+
+var Config = {
+ data: function() {
+
+ try {
+ f = fs.readFileSync('/etc/amon.conf','utf-8');
+ this.config = JSON.parse(f);
+
+ } catch(err) {
+ console.error("Could not open the Amon configuration file: %s", err);
+ this.config = false;
+ }
+
+ return this.config;
+
+ },
+
+ host: function(){
+ return this.data().web_app.host ? this.data().web_app.host : default_host;
+ },
+
+ port: function(){
+ return this.data().web_app.port ? this.data().web_app.port : default_port;
+ }
+
+};
+
+exports.Config = Config;
var Amon = {
- VERSION: 0.1,
- Host: "127.0.0.1",
- Port: 2464,
+ VERSION: 0.2,
+ host: Config.host(),
+ port: Config.port(),
handle: function(error) {
- var error_data = Amon.to_json(error);
+ var error_data = Amon.exception_data_to_json(error);
Amon.post('exception', error_data);
},
- error_json: function(error) {
+ exception_data_to_json: function(error) {
return JSON.stringify({
"application_environment": {
"application_root_directory": process.cwd(),
@@ -41,25 +72,48 @@ var Amon = {
});
},
- post: function(type, data) {
+ log: function(message, level){
+ level = level || "notset"
+ var log_data = JSON.stringify({
+ "message": message,
+ "level": level
+ });
+ Amon.post('log', log_data);
+
+ },
- var client = HTTP.createClient(Amon.Port, Amon.Host);
+ post: function(type, data) {
var headers = {
- 'Content-Length' : data.length
+ 'Content-Length' : data.length,
+ 'Content-Type': 'application/x-www-form-urlencoded'
};
- var request = client.request('POST', '/api/log', headers);
+ if(type == 'exception'){
+ var path = '/api/exception'
+ }
+ else {
+ var path = '/api/log'
+ }
+
+ var options = {
+ host: Amon.host,
+ port: Amon.port,
+ path: path,
+ method: 'POST',
+ headers: headers
+ };
+
+ var request = http.request(options, function(response) {
+ console.log('status: ' + response.statusCode);
+ });
request.write(data);
request.end();
- request.on('response', function (response) {
- if (response.statusCode === 200) {
- console.log("Error data successfully sent to exceptional");
- } else {
- console.log("Error sending to api.getexceptional.com :" + response.statusCode);
- }
+ request.on('error', function () {
+ console.log("Error sending log data to Amon ");
+ console.log("Please make sure that the web application is running on "+ Amon.host +':'+ Amon.port);
});
}
};
View
@@ -1,5 +1,5 @@
-{ "name" : "amon-node",
- "version" : "0.0.1",
+{ "name" : "amon",
+ "version" : "0.2",
"description" : "node.js module for Amon",
"homepage" : "http://amon.cx",
"author" : "Martin Rusev <martin@amon.cx>",
@@ -8,11 +8,11 @@
"url" : "http://github.com/martinrusev/amon-node.git"
},
"bugs" : {
- "web" : "http://github.com/martinrusev/amon-node/issues",
+ "url" : "http://github.com/martinrusev/amon-node/issues",
"mail" : "martin@amon.cx"
},
"main" : "./lib/amon",
- "engines" : { "node" : ">=0.2.0" },
+ "engines" : { "node" : ">=0.4.0" },
"scripts" : { "test" : "test/amon-test.js" },
"dependencies" : {},
"licenses" : [

0 comments on commit 67e81e1

Please sign in to comment.