Skip to content

Commit c291731

Browse files
committed
Add some basic logging functionality for "service.js".
1 parent d4a680b commit c291731

File tree

6 files changed

+157
-11
lines changed

6 files changed

+157
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules/
33
*.tern-port
44
config/api_keys.json
55
config/databases.json
6+
config/log.json

config/init.d/mootools-website

+18-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ do_stop() {
7373
return "$RETVAL"
7474
}
7575

76+
#
77+
# Function that sends a SIGUSR2 to the daemon/service
78+
#
79+
do_reload_logs() {
80+
# Return
81+
# 0 if the signal was sent
82+
# 1 if the signal could not be sent
83+
start-stop-daemon --stop --signal USR2 --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null || return 1
84+
start-stop-daemon --stop --signal USR2 --quiet --pidfile $PIDFILE --exec $DAEMON
85+
return 0
86+
}
87+
7688
#
7789
# Function that sends a SIGHUP to the daemon/service
7890
#
@@ -106,6 +118,11 @@ case "$1" in
106118
status)
107119
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
108120
;;
121+
reload-logs)
122+
log_daemon_msg "Reloading logs of $DESC" "$NAME"
123+
do_reload_logs
124+
log_end_msg $?
125+
;;
109126
reload|force-reload)
110127
log_daemon_msg "Reloading $DESC" "$NAME"
111128
do_reload
@@ -130,7 +147,7 @@ case "$1" in
130147
esac
131148
;;
132149
*)
133-
echo "Usage: $SCRIPTNAME {start|stop|status|restart|reload|force-reload}" >&2
150+
echo "Usage: $SCRIPTNAME {start|stop|status|restart|reload|reload-logs|force-reload}" >&2
134151
exit 3
135152
;;
136153
esac

config/log.sample.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"outFile": "tests/out.log",
3+
"errFile": "tests/err.log"
4+
}

config/logrotate.d/mootools-website

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/var/opt/mootools/log/mootools-website.*.log {
2+
weekly
3+
missingok
4+
rotate 52
5+
compress
6+
delaycompress
7+
notifempty
8+
create 0640 www-data adm
9+
sharedscripts
10+
postrotate
11+
service mootools-website reload-logs
12+
endscript
13+
}

lib/log.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"use strict";
2+
3+
var fs = require('fs');
4+
5+
var silent = false,
6+
monitors = [],
7+
files = {},
8+
streams = {};
9+
10+
function close(type){
11+
if (streams[type]){
12+
streams[type].end();
13+
streams[type] = null;
14+
}
15+
}
16+
17+
function open(type, file){
18+
if (file){
19+
files[type] = file;
20+
} else {
21+
file = files[type];
22+
}
23+
24+
close(type);
25+
26+
if (file){
27+
var stream = fs.createWriteStream(file, {
28+
flags: 'a+',
29+
defaultEncoding: 'utf8',
30+
mode: parseInt('640', 8)
31+
});
32+
stream.addListener('error', function(error){
33+
stream.end();
34+
streams[type] = null;
35+
if (monitors.length){
36+
for (var i = 0, l = monitors.length; i < l; ++i){
37+
monitors[i].emit('error', error);
38+
}
39+
} else {
40+
throw error;
41+
}
42+
});
43+
streams[type] = stream;
44+
}
45+
}
46+
47+
function write(type, data){
48+
var stream = streams[type];
49+
if (stream){
50+
stream.write('[' + (new Date()).toISOString() + '] ' + data);
51+
}
52+
}
53+
54+
function configure(configuration){
55+
if (configuration.silent != null) silent = configuration.silent;
56+
if (configuration.outFile) open('out', configuration.outFile);
57+
if (configuration.errFile) open('err', configuration.errFile);
58+
}
59+
60+
function reload(){
61+
var types = Object.keys(files);
62+
for (var i = 0, l = types.length; i < l; ++i){
63+
open(types[i]);
64+
}
65+
}
66+
67+
function attach(monitor){
68+
if (monitors.indexOf(monitor) == -1) monitors.push(monitor);
69+
monitor.addListener('stdout', log);
70+
monitor.addListener('stderr', logError);
71+
}
72+
73+
function detach(monitor){
74+
var index = monitors.indexOf(monitor);
75+
if (index > -1) monitors.splice(index, 1);
76+
monitor.removeListener('stdout', log);
77+
monitor.removeListener('stderr', logError);
78+
}
79+
80+
function log(data){
81+
write('out', data);
82+
if (!silent) process.stdout.write(data);
83+
}
84+
85+
function logError(data){
86+
write('err', data);
87+
if (!silent) process.stderr.write(data);
88+
}
89+
90+
exports.configure = configure;
91+
exports.reload = reload;
92+
exports.attach = attach;
93+
exports.detach = detach;
94+
95+
exports.write = function(data){ return log(data + '\n'); };
96+
exports.error = function(data){ return logError(data + '\n'); };

service.js

+25-10
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,66 @@
33
var app = 'index',
44
name = 'mootools-website';
55

6-
var Monitor = require('forever-monitor').Monitor;
6+
var Monitor = require('forever-monitor').Monitor,
7+
log = require('./lib/log.js');
78

89
var monitor = new Monitor(__dirname + '/' + app + '.js', {
10+
silent: true,
911
minUptime: 2000,
1012
args: process.argv.splice(2)
1113
});
1214

15+
log.attach(monitor);
16+
try {
17+
log.configure(require('./config/log.json'));
18+
} catch (error){
19+
}
20+
1321
function stop(){
14-
console.error('** Requested stop of ' + name);
22+
log.error('** Requested stop of ' + name);
1523
monitor.stop();
1624
}
1725

1826
function reload(){
19-
console.error('** Requested reload of ' + name);
27+
log.error('** Requested reload of ' + name);
28+
log.reload();
2029
monitor.restart();
2130
}
2231

32+
function reloadLogs(){
33+
log.error('** Requested log reload of ' + name);
34+
log.reload();
35+
}
36+
2337
process.addListener('SIGINT', stop);
2438
process.addListener('SIGTERM', stop);
2539
process.addListener('SIGHUP', reload);
40+
process.addListener('SIGUSR2', reloadLogs);
2641

2742
monitor.addListener('start', function(){
28-
console.error('** Starting ' + name + ' monitor');
43+
log.error('** Starting ' + name + ' monitor');
2944
});
3045

3146
monitor.addListener('stop', function(){
32-
console.error('** Stopping ' + name + ' monitor');
47+
log.error('** Stopping ' + name + ' monitor');
3348
});
3449

3550
monitor.addListener('exit:code', function(code){
3651
if (code == null) code = 0;
37-
console.error('** Detected ' + name + ' process exited (exit code ' + code + ')');
52+
log.error('** Detected ' + name + ' process exited (exit code ' + code + ')');
3853
});
3954

4055
monitor.addListener('restart', function(){
41-
console.error('** Restarting ' + name + ' process (#' + monitor.times + ')');
56+
log.error('** Restarting ' + name + ' process (#' + monitor.times + ')');
4257
});
4358

4459
monitor.addListener('error', function(error){
4560
if (error.stack){
46-
console.error('** ' + error.stack);
61+
log.error('** ' + error.stack);
4762
} else if (error.message) {
48-
console.error('** Error: ' + error.message);
63+
log.error('** Error: ' + error.message);
4964
} else {
50-
console.error('** Error: ' + error);
65+
log.error('** Error: ' + error);
5166
}
5267
});
5368

0 commit comments

Comments
 (0)