Commit 6e001363 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Added websocket logging

parent 829d8fec
"use strict";
let util = require('util');
let winston = require('winston');
var CallbackLogger = winston.transports.CallbackLogger = function (options) {
this.name = 'customLogger';
this.level = options.level || 'info';
this.callback = options.callback;
};
util.inherits(CallbackLogger, winston.Transport);
CallbackLogger.prototype.log = function (level, msg, meta, callback) {
this.callback(level, msg);
callback(null, true);
};
module.exports = CallbackLogger;
......@@ -292,6 +292,8 @@ module.exports = {
INVALIDATE_CORE_CACHE: true,
WITH_SIGNATURES_AND_POW: true,
WEBMIN_LOGS_CACHE: 2000,
MEMORY_CLEAN_INTERVAL: 60 * 60, // hourly
SAFE_FACTOR: 3,
BLOCKS_COLLECT_THRESHOLD: 30 // Blocks to collect from memory and persist
......
......@@ -2,7 +2,8 @@
var moment = require('moment');
var path = require('path');
var winston = require('winston');
var directory = require('../lib/directory');
var cbLogger = require('./callbackLogger');
var directory = require('./directory');
var customLevels = {
levels: {
......@@ -43,6 +44,24 @@ var logger = new (winston.Logger)({
]
});
// Singleton
let loggerAttached = false;
logger.addCallbackLogs = (callbackForLog) => {
if (!loggerAttached) {
loggerAttached = true;
logger.add(cbLogger, {
callback: callbackForLog,
level: 'trace',
levels: customLevels.levels,
handleExceptions: false,
colorize: true,
timestamp: function() {
return moment().format();
}
});
}
};
logger.addHomeLogs = (home) => {
logger.add(winston.transports.File, {
level: 'info',
......
......@@ -2,8 +2,10 @@
var path = require('path');
var es = require('event-stream');
var constants = require('../../lib/constants');
var network = require('../../lib/network');
var dtos = require('../../lib/streams/dtos');
var logger = require('../../lib/logger')('webmin');
let WebSocketServer = require('ws').Server;
......@@ -37,12 +39,37 @@ module.exports = function(dbConf, overConf, interfaces, httpLogs) {
path: '/webmin/ws'
});
//wssSync.on('connection', function connection() {
// wssSync.broadcast(JSON.stringify({
// type: 'download',
// value: 55
// }));
//});
let lastLogs = [];
wssEvents.on('connection', function connection(ws) {
ws.on('message', () => {
wssEvents.broadcast(JSON.stringify({
type: 'log',
value: lastLogs
}));
});
wssEvents.broadcast(JSON.stringify({
type: 'log',
value: lastLogs
}));
// The callback which write each new log message to websocket
logger.addCallbackLogs((level, msg) => {
lastLogs.splice(0, Math.max(0, lastLogs.length - constants.WEBMIN_LOGS_CACHE + 1));
lastLogs.push({
level: level,
msg: msg
});
wssEvents.broadcast(JSON.stringify({
type: 'log',
value: [{
level: level,
msg: msg
}]
}));
});
});
wssEvents.broadcast = (data) => wssEvents.clients.forEach((client) => client.send(data));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment