Use winston for logging

This commit is contained in:
Annika Backstrom 2017-04-09 13:55:11 -04:00
parent b0f0694832
commit 1f533d25fb
5 changed files with 35 additions and 39 deletions

View File

@ -7,20 +7,22 @@ const spawn = require('child_process').spawn;
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmitter { module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmitter {
constructor(command) { constructor(command, logger) {
super(); super();
this.logger = logger;
var cmd = command[0], var cmd = command[0],
args = command.slice(1); args = command.slice(1);
this.childProcess = spawn(cmd, args); this.childProcess = spawn(cmd, args);
this.childProcess.on('error', function(err) { this.childProcess.on('error', function(err) {
this.emit('process_error', err); logger.error("process error", { name: name, err: err });
}); });
this.childProcess.on('close', function(code, signal) { this.childProcess.on('close', function(code, signal) {
this.emit('close', code, signal); logger.debug("process closed", { name: name, code: code, signal: signal });
}); });
this.childProcess.stdout.on('data', (data) => { this.childProcess.stdout.on('data', (data) => {
@ -31,11 +33,12 @@ module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmit
}); });
this.childProcess.stderr.on('data', (data) => { this.childProcess.stderr.on('data', (data) => {
this.emit('error', data); this.emit('message', lines[i]);
}); });
} }
kill() { kill() {
this.logger.debug("killing child process");
this.childProcess.kill(); this.childProcess.kill();
} }

View File

@ -3,13 +3,13 @@
const ChildProcessEmitter = require('./child-process').ChildProcessEmitter; const ChildProcessEmitter = require('./child-process').ChildProcessEmitter;
class MediaPlayer extends ChildProcessEmitter { class MediaPlayer extends ChildProcessEmitter {
constructor(config) { constructor(config, logger) {
super(config.mpg321); super(config.mpg321);
} }
} }
module.exports = function(config) { module.exports = function(config, logger) {
return new MediaPlayer(config); return new MediaPlayer(config, logger);
}; };
// vim:set ts=2 sw=2 et: // vim:set ts=2 sw=2 et:

View File

@ -3,13 +3,13 @@
const ChildProcessEmitter = require('./child-process').ChildProcessEmitter; const ChildProcessEmitter = require('./child-process').ChildProcessEmitter;
class TagReader extends ChildProcessEmitter { class TagReader extends ChildProcessEmitter {
constructor(config) { constructor(config, logger) {
super(config.tag_reader); super(config.tag_reader);
} }
} }
module.exports = function(config) { module.exports = function(config, logger) {
return new TagReader(config); return new TagReader(config, logger);
}; };
// vim:set ts=2 sw=2 et: // vim:set ts=2 sw=2 et:

View File

@ -18,6 +18,7 @@
"glob": "^7.1.1", "glob": "^7.1.1",
"morgan": "^1.7.0", "morgan": "^1.7.0",
"mustache": "^2.3.0", "mustache": "^2.3.0",
"winston": "^2.3.1",
"ws": "^2.2.2" "ws": "^2.2.2"
}, },
"devDependencies": { "devDependencies": {

View File

@ -7,10 +7,19 @@ const glob = require('glob');
const views = require('./jukebox/views')(__dirname + '/templates/'); const views = require('./jukebox/views')(__dirname + '/templates/');
const express = require('express'); const express = require('express');
const logger = require('morgan') const morgan = require('morgan')
const winston = require('winston');
const MediaPlayer = require('./jukebox/media-player')(config); var logger = new (winston.Logger)({
const TagReader = require('./jukebox/tag-reader')(config); transports: [
new (winston.transports.Console)()
]
});
logger.level = config.debug ? 'debug' : 'info';
const MediaPlayer = require('./jukebox/media-player')(config, logger);
const TagReader = require('./jukebox/tag-reader')(config, logger);
var app = express(); var app = express();
var server = require('http').createServer(); var server = require('http').createServer();
@ -22,8 +31,8 @@ var wss = new WebSocketServer({server: server});
var play_log = []; var play_log = [];
function exitHandler(options, err) { function exitHandler(options, err) {
console.log('[player] closing children'); logger.debug('closing child tasks');
if (err) console.log(err.stack); if (err) logger.debug(err.stack);
TagReader.kill(); TagReader.kill();
MediaPlayer.kill(); MediaPlayer.kill();
@ -36,30 +45,13 @@ function exitHandler(options, err) {
process.on('exit', exitHandler.bind(null)); process.on('exit', exitHandler.bind(null));
process.on('SIGINT', exitHandler.bind(null, {exit:true})); process.on('SIGINT', exitHandler.bind(null, {exit:true}));
function setupChildLogging(name, child) {
child.on('process_error', (err) => {
console.log(`[${name}] error=${err}`);
});
child.on('close', (code, signal) => {
console.log(`[${name}] process closed:code=${code} signal=${signal}`);
});
child.on('error', (data) => {
console.log(`[${name}] stderr: ${data}`);
});
}
setupChildLogging('tagreader', TagReader);
setupChildLogging('mediaplayer', MediaPlayer);
TagReader.on('message', (data) => { TagReader.on('message', (data) => {
processLine(data); processLine(data);
}); });
if (config.debug) { if (config.debug) {
MediaPlayer.on('message', (data) => { MediaPlayer.on('message', (data) => {
console.log(`[mediaplayer] stdout: ${data}`); logger.debug("mediaplayer send message", { line: data });
}); });
} }
@ -104,7 +96,7 @@ function processLine(data) {
} }
last_tag = String(data).trim(); last_tag = String(data).trim();
console.log("[tag] " + last_tag); logger.debug("got tag from tag reader", { tag: last_tag });
// min 1 sec debounce // min 1 sec debounce
debounce_until = Date.now() / 1000 + 1; debounce_until = Date.now() / 1000 + 1;
@ -113,13 +105,13 @@ function processLine(data) {
wait_sec = wait_sec || 1; wait_sec = wait_sec || 1;
debounce_until = Date.now() / 1000 + wait_sec; debounce_until = Date.now() / 1000 + wait_sec;
if (action) { if (action) {
console.log(`[action] [${action}] for [${last_tag}]`); logger.info("sending action to media player", { action: action, tag: last_tag });
MediaPlayer.send(action + "\n"); MediaPlayer.send(action + "\n");
} }
}); });
} }
app.use(logger('dev')) app.use(morgan('dev'))
app.use(express.static(__dirname + '/static')) app.use(express.static(__dirname + '/static'))
app.get('/', function (req, res, next) { app.get('/', function (req, res, next) {
@ -150,16 +142,16 @@ wss.broadcast = function broadcast(data) {
}; };
wss.on('connection', function (ws) { wss.on('connection', function (ws) {
console.log('[websocket] client connected'); logger.info('websocket client connected');
ws.on('close', function () { ws.on('close', function () {
console.log('[websocket] client disconnected'); logger.info('websocket client disconnected');
}); });
}); });
server.on('request', app); server.on('request', app);
server.listen(config.port, function () { server.listen(config.port, function () {
console.log('[http] listening on http://localhost:' + config.port) logger.info('express listening on http://localhost:' + config.port)
}) })
// vim:ts=2 sw=2 et: // vim:ts=2 sw=2 et: