Created by Christian Ranz
There is not just a single tool to use. We need to …
There should be no data loss after an uncontrolled shutdown.
When you decide about the way you are logging, think about, having a save way in cases like uncaught exceptions.
logger = module.exports;
logger.level = 'debug';
logger.levelMap = {'emerg': 0, 'error': 1, 'warning': 2,
'info': 3, 'debug': 4};
logger.log = function(level, message) {
if(logger.levelMap[level] > logger.levelMap[logger.level]) {
return;
}
if(level === 'emerg') {
process.stderr.write(level + ': ' + message + '\n');
process.exit(1);
} else {
process.stdout.write(level + ': ' + message + '\n');
}
};
Winston is a multi-transport, asynchronous logging library for node.js
var winston = require('winston');
require('winston-mongo').Mongo;
var logger = new (winston.Logger)({
transports: [
new winston.transports.Console()
new winston.transports.File({ filename: 'path/to/logs.log' })
new winston.transports.MongoDB({ db: 'logging', level: 'info'}) // should be capped
]
exceptionHandlers: [
new winston.transports.File({ filename: 'path/to/exceptions.log' })
]
});
#!upstart
description "node.js daemon"
author "chris"
start on startup
stop on shutdown
script
export HOME="/root"
echo $$ > /var/run/node-daemon.pid
exec sudo -u vagrant node /vagrant/node/qeuedaemon-monit.js >> /var/log/node-daemon.log 2>&1
end script
pre-start script
# Date format same as (new Date()).toISOString() for consistency
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/node-daemon.log
end script
pre-stop script
rm /var/run/node-daemon.pid
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/node-daemon.log
end script
check process node
with pidfile "/var/run/node-daemon.pid"
start program = "/sbin/start node-daemon"
stop program = "/sbin/stop node-daemon"
if changed pid
then restart
check process node
with pidfile "/var/run/node-daemon.pid"
start program = "/sbin/start node-daemon"
stop program = "/sbin/stop node-daemon"
if changed pid
then restart
if totalmemory > 2% for 5 cycles
then alert
if totalmemory > 10% for 5 cycles
then restart
check file node_forcekill path "/var/run/node-daemon.pid" every 10 cycles
start program = "/sbin/start node-daemon"
stop program = "/sbin/stop node-daemon"
if timestamp > 1 days
then restart
check file node_alive path "/var/log/node-daemon-alive.log"
start program = "/sbin/start node-daemon"
stop program = "/sbin/stop node-daemon"
if timestamp > 30 seconds
then exec "/bin/bash -c 'kill -s SIGKILL `cat /var/run/node-daemon.pid`'"
Initial state is the best state.
so let it die...