Skip to content
This repository was archived by the owner on Jan 6, 2021. It is now read-only.

Commit 0976013

Browse files
committed
Logging works now, v 0.2, ready to use
1 parent c58ce46 commit 0976013

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#c9.io and my testing files
12
.c9revisions/
23
test/
3-
config/test.json
4+
config/*
45
response.txt
56

67
lib-cov

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ Configuration parameters:
3333

3434
TODO
3535
===
36+
* Variables in command definition
3637
* Commands string instade of array.
3738
* cfg.require parameter with applying the cfg.commands results

app.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ var processFile = function(fileName){
1212
var filePath = cfg_dir + fileName;
1313
var fileExt = path.extname(fileName);
1414
if(fileExt != '.json'){
15-
return log('Problem with file "' + filePath + '". There must be .json file extension.');
15+
return log('Problem with file "' + filePath + '". There must be .json file extension.', 'runtime');
1616
}
1717

1818
fs.readFile(filePath, 'utf-8', function(err, data) {
19+
var cfg = {};
1920
try{
2021
if(err) throw err;
2122

22-
var cfg = JSON.parse(data);
23+
cfg = JSON.parse(data);
2324
if([cfg.path, cfg.user, cfg.commands].indexOf(undefined) !== -1){
2425
throw new Error('Bad config file "' + filePath + '". It need to be json object with path, user and commands keys.');
2526
}
2627
} catch(e) {
27-
return log('Error while processing file "' + filePath + '": ' + e);
28+
return log('Error while processing file "' + filePath + '": ' + e, 'runtime');
2829
}
2930
//Populate good cfg object to objects map by filename without extension
3031
return cfg_map[path.basename(fileName, fileExt)] = cfg;
@@ -33,14 +34,16 @@ var processFile = function(fileName){
3334

3435
// Readfiles to object on server start
3536
fs.readdir(cfg_dir, function(wtf, files){
37+
var watchCallback = function(prev, next) {
38+
processFile(files[i]);
39+
};
40+
3641
for(var i in files){
3742
try{
3843
processFile(files[i]);
39-
fs.watchFile(cfg_dir + files[i], function(prev, next) {
40-
processFile(files[i]);
41-
});
44+
fs.watchFile(cfg_dir + files[i], watchCallback);
4245
} catch(e) {
43-
log(e);
46+
log(e, 'startup');
4447
}
4548
}
4649

@@ -89,7 +92,7 @@ http.createServer(function(request, response) {
8992
}
9093

9194
if(!fs.readdirSync(cfg.path)) {
92-
return log('Invalid path "' + cfg.path + '" in config "' + request.url + '"');
95+
return log('Invalid path "' + cfg.path + '" in config "' + request.url + '"', request.url + '.error');
9396
}
9497
spawn_options.cwd = cfg.path;
9598

@@ -103,15 +106,15 @@ http.createServer(function(request, response) {
103106
break;
104107
}
105108
}
106-
if(refNotMatch) return log('No refs match. Aborting.');
109+
if(refNotMatch) return log('No refs match. Aborting.', request.url + '.info');
107110
}
108111

109112
if(cfg.commands.length){
110113
var onData = function(data) {
111-
log('Command "' + commandString + '" with data: ' + data);
114+
log('Command "' + commandString + '" with data: ' + data, request.url + '.info');
112115
};
113116
var onError = function(data) {
114-
log('Error in command "' + commandString + '" with data: ' + data);
117+
log('Error in command "' + commandString + '" with data: ' + data, request.url + '.error');
115118
};
116119

117120
for(var i in cfg.commands){
@@ -125,7 +128,7 @@ http.createServer(function(request, response) {
125128
}
126129
});
127130

128-
response.end("Deploy in queue!");
131+
response.end("Process in queue!");
129132
} else {
130133
response.writeHead(404, 'There is nothing.');
131134
response.end("404;");

logging/lib/log.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1+
var fs = require("fs");
2+
var path = require("path");
3+
var util = require("util");
4+
15
var dbg = true;
2-
var dir = __dirname + '/../logs';
6+
var logs_dir = __dirname + '/../logs';
7+
8+
Date.prototype.toLocaleFormat = function(format) {
9+
var f = {y : this.getYear() + 1900,m : this.getMonth() + 1,d : this.getDate(),H : this.getHours(),M : this.getMinutes(),s : this.getSeconds()};
10+
for(var k in f){
11+
format = format.replace('%' + k, f[k] < 10 ? "0" + f[k] : f[k]);
12+
}
13+
return format;
14+
};
315

416
exports = module.exports = function(msg, file){
17+
console.log(msg);
18+
if(dbg && typeof msg == 'object'){
19+
console.dir(msg);
20+
}
521
if(file){
6-
//TODO: Write to file
7-
} else {
8-
console.log(msg);
9-
if(dbg && typeof msg == 'object'){
10-
console.dir(msg);
22+
var toWrite = '[' + new Date().toLocaleFormat('%d.%m.%y %H:%M:%s') + ']' + msg + '\n';
23+
if(msg.stack) {
24+
toWrite += '---Call stack:---\n' + msg.stack + '\n---End Call stack---\n';
1125
}
26+
27+
fs.appendFile(path.join(logs_dir, file + '.txt'), toWrite, function (err) {
28+
if(err) {
29+
arguments.callee(err);
30+
}
31+
});
1232
}
1333

1434
return false;

0 commit comments

Comments
 (0)