2012-07-04 06:44:50 +08:00
|
|
|
var log4js = require('../log4js'),
|
|
|
|
net = require('net'),
|
|
|
|
END_MSG = '__LOG4JS__';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a server, listening on config.loggerPort, config.loggerHost.
|
|
|
|
* Output goes to config.actualAppender (config.appender is used to
|
|
|
|
* set up that appender).
|
|
|
|
*/
|
|
|
|
function logServer(config) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a utf-8 string, returns an object with
|
|
|
|
* the correct log properties.
|
|
|
|
*/
|
|
|
|
function deserializeLoggingEvent(clientSocket, msg) {
|
|
|
|
var loggingEvent;
|
2011-11-02 12:49:46 +08:00
|
|
|
try {
|
2012-11-09 13:02:16 +08:00
|
|
|
loggingEvent = JSON.parse(msg);
|
|
|
|
loggingEvent.startTime = new Date(loggingEvent.startTime);
|
|
|
|
loggingEvent.level = log4js.levels.toLevel(loggingEvent.level.levelStr);
|
2011-11-02 12:49:46 +08:00
|
|
|
} catch (e) {
|
2012-07-04 06:44:50 +08:00
|
|
|
// JSON.parse failed, just log the contents probably a naughty.
|
|
|
|
loggingEvent = {
|
|
|
|
startTime: new Date(),
|
|
|
|
categoryName: 'log4js',
|
2012-11-09 13:02:16 +08:00
|
|
|
level: log4js.levels.ERROR,
|
2012-07-04 06:44:50 +08:00
|
|
|
data: [ 'Unable to parse log:', msg ]
|
|
|
|
};
|
2011-11-02 12:49:46 +08:00
|
|
|
}
|
|
|
|
|
2012-07-04 06:44:50 +08:00
|
|
|
loggingEvent.remoteAddress = clientSocket.remoteAddress;
|
|
|
|
loggingEvent.remotePort = clientSocket.remotePort;
|
|
|
|
|
|
|
|
return loggingEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
var actualAppender = config.actualAppender,
|
|
|
|
server = net.createServer(function serverCreated(clientSocket) {
|
|
|
|
clientSocket.setEncoding('utf8');
|
2013-03-30 18:23:58 +08:00
|
|
|
var logMessage = '';
|
2012-07-04 06:44:50 +08:00
|
|
|
|
2013-03-30 18:23:58 +08:00
|
|
|
function logTheMessage(msg) {
|
|
|
|
if (logMessage.length > 0) {
|
|
|
|
actualAppender(deserializeLoggingEvent(clientSocket, msg));
|
2012-07-04 06:44:50 +08:00
|
|
|
}
|
2013-03-30 18:23:58 +08:00
|
|
|
}
|
2012-07-04 06:44:50 +08:00
|
|
|
|
2013-03-30 18:23:58 +08:00
|
|
|
function chunkReceived(chunk) {
|
|
|
|
var event;
|
|
|
|
logMessage += chunk || '';
|
|
|
|
if (logMessage.indexOf(END_MSG) > -1) {
|
|
|
|
event = logMessage.substring(0, logMessage.indexOf(END_MSG));
|
|
|
|
logTheMessage(event);
|
|
|
|
logMessage = logMessage.substring(event.length + END_MSG.length) || '';
|
|
|
|
//check for more, maybe it was a big chunk
|
|
|
|
chunkReceived();
|
2012-07-04 06:44:50 +08:00
|
|
|
}
|
2013-03-30 18:23:58 +08:00
|
|
|
}
|
2011-11-02 12:49:46 +08:00
|
|
|
|
2013-03-30 18:23:58 +08:00
|
|
|
clientSocket.on('data', chunkReceived);
|
|
|
|
clientSocket.on('end', chunkReceived);
|
2012-07-04 06:44:50 +08:00
|
|
|
});
|
2013-03-30 18:23:58 +08:00
|
|
|
|
2012-07-04 06:44:50 +08:00
|
|
|
server.listen(config.loggerPort || 5000, config.loggerHost || 'localhost');
|
|
|
|
|
|
|
|
return actualAppender;
|
2011-11-02 12:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function workerAppender(config) {
|
2012-07-04 06:44:50 +08:00
|
|
|
var canWrite = false,
|
|
|
|
buffer = [],
|
|
|
|
socket;
|
|
|
|
|
|
|
|
createSocket();
|
|
|
|
|
|
|
|
function createSocket() {
|
|
|
|
socket = net.createConnection(config.loggerPort || 5000, config.loggerHost || 'localhost');
|
|
|
|
socket.on('connect', function() {
|
|
|
|
emptyBuffer();
|
|
|
|
canWrite = true;
|
|
|
|
});
|
|
|
|
socket.on('timeout', socket.end.bind(socket));
|
|
|
|
//don't bother listening for 'error', 'close' gets called after that anyway
|
|
|
|
socket.on('close', createSocket);
|
|
|
|
}
|
|
|
|
|
|
|
|
function emptyBuffer() {
|
|
|
|
var evt;
|
|
|
|
while ((evt = buffer.shift())) {
|
|
|
|
write(evt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function write(loggingEvent) {
|
|
|
|
socket.write(JSON.stringify(loggingEvent), 'utf8');
|
|
|
|
socket.write(END_MSG, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
return function log(loggingEvent) {
|
|
|
|
if (canWrite) {
|
|
|
|
write(loggingEvent);
|
|
|
|
} else {
|
|
|
|
buffer.push(loggingEvent);
|
|
|
|
}
|
|
|
|
};
|
2011-11-02 12:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function createAppender(config) {
|
2012-07-04 06:44:50 +08:00
|
|
|
if (config.mode === 'master') {
|
|
|
|
return logServer(config);
|
|
|
|
} else {
|
|
|
|
return workerAppender(config);
|
|
|
|
}
|
2011-11-02 12:49:46 +08:00
|
|
|
}
|
|
|
|
|
2013-02-20 23:06:59 +08:00
|
|
|
function configure(config, options) {
|
2012-07-04 06:44:50 +08:00
|
|
|
var actualAppender;
|
|
|
|
if (config.appender && config.mode === 'master') {
|
|
|
|
log4js.loadAppender(config.appender.type);
|
2013-02-20 23:06:59 +08:00
|
|
|
actualAppender = log4js.appenderMakers[config.appender.type](config.appender, options);
|
2012-07-04 06:44:50 +08:00
|
|
|
config.actualAppender = actualAppender;
|
|
|
|
}
|
|
|
|
return createAppender(config);
|
2011-11-02 12:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.appender = createAppender;
|
|
|
|
exports.configure = configure;
|