Skip to content
This repository has been archived by the owner on Sep 17, 2020. It is now read-only.

Commit

Permalink
Working version - both exceptions and logs
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrusev committed Nov 9, 2011
1 parent 5c4e352 commit 67e81e1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 20 deletions.
20 changes: 20 additions & 0 deletions example.js
@@ -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");
86 changes: 70 additions & 16 deletions lib/amon.js
@@ -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(),
Expand Down Expand Up @@ -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);
});
}
};
Expand Down
8 changes: 4 additions & 4 deletions package.json
@@ -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>",
Expand All @@ -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" : [
Expand Down

0 comments on commit 67e81e1

Please sign in to comment.