From 1f533d25fbd3f38ec6bac3842c49991c9eb0770a Mon Sep 17 00:00:00 2001 From: Annika Backstrom Date: Sun, 9 Apr 2017 13:55:11 -0400 Subject: [PATCH] Use winston for logging --- jukebox/child-process.js | 11 +++++---- jukebox/media-player.js | 6 ++--- jukebox/tag-reader.js | 6 ++--- package.json | 1 + player.js | 50 +++++++++++++++++----------------------- 5 files changed, 35 insertions(+), 39 deletions(-) diff --git a/jukebox/child-process.js b/jukebox/child-process.js index 6582c01..b36854e 100644 --- a/jukebox/child-process.js +++ b/jukebox/child-process.js @@ -7,20 +7,22 @@ const spawn = require('child_process').spawn; const EventEmitter = require('events').EventEmitter; module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmitter { - constructor(command) { + constructor(command, logger) { super(); + this.logger = logger; + var cmd = command[0], args = command.slice(1); this.childProcess = spawn(cmd, args); 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.emit('close', code, signal); + logger.debug("process closed", { name: name, code: code, signal: signal }); }); this.childProcess.stdout.on('data', (data) => { @@ -31,11 +33,12 @@ module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmit }); this.childProcess.stderr.on('data', (data) => { - this.emit('error', data); + this.emit('message', lines[i]); }); } kill() { + this.logger.debug("killing child process"); this.childProcess.kill(); } diff --git a/jukebox/media-player.js b/jukebox/media-player.js index f0af95d..dba1277 100644 --- a/jukebox/media-player.js +++ b/jukebox/media-player.js @@ -3,13 +3,13 @@ const ChildProcessEmitter = require('./child-process').ChildProcessEmitter; class MediaPlayer extends ChildProcessEmitter { - constructor(config) { + constructor(config, logger) { super(config.mpg321); } } -module.exports = function(config) { - return new MediaPlayer(config); +module.exports = function(config, logger) { + return new MediaPlayer(config, logger); }; // vim:set ts=2 sw=2 et: diff --git a/jukebox/tag-reader.js b/jukebox/tag-reader.js index c44ed73..47e5d9e 100644 --- a/jukebox/tag-reader.js +++ b/jukebox/tag-reader.js @@ -3,13 +3,13 @@ const ChildProcessEmitter = require('./child-process').ChildProcessEmitter; class TagReader extends ChildProcessEmitter { - constructor(config) { + constructor(config, logger) { super(config.tag_reader); } } -module.exports = function(config) { - return new TagReader(config); +module.exports = function(config, logger) { + return new TagReader(config, logger); }; // vim:set ts=2 sw=2 et: diff --git a/package.json b/package.json index 425df91..cb56ef6 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "glob": "^7.1.1", "morgan": "^1.7.0", "mustache": "^2.3.0", + "winston": "^2.3.1", "ws": "^2.2.2" }, "devDependencies": { diff --git a/player.js b/player.js index 4214410..6ed45d8 100644 --- a/player.js +++ b/player.js @@ -7,10 +7,19 @@ const glob = require('glob'); const views = require('./jukebox/views')(__dirname + '/templates/'); const express = require('express'); -const logger = require('morgan') +const morgan = require('morgan') +const winston = require('winston'); -const MediaPlayer = require('./jukebox/media-player')(config); -const TagReader = require('./jukebox/tag-reader')(config); +var logger = new (winston.Logger)({ + 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 server = require('http').createServer(); @@ -22,8 +31,8 @@ var wss = new WebSocketServer({server: server}); var play_log = []; function exitHandler(options, err) { - console.log('[player] closing children'); - if (err) console.log(err.stack); + logger.debug('closing child tasks'); + if (err) logger.debug(err.stack); TagReader.kill(); MediaPlayer.kill(); @@ -36,30 +45,13 @@ function exitHandler(options, err) { process.on('exit', exitHandler.bind(null)); 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) => { processLine(data); }); if (config.debug) { 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(); - console.log("[tag] " + last_tag); + logger.debug("got tag from tag reader", { tag: last_tag }); // min 1 sec debounce debounce_until = Date.now() / 1000 + 1; @@ -113,13 +105,13 @@ function processLine(data) { wait_sec = wait_sec || 1; debounce_until = Date.now() / 1000 + wait_sec; 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"); } }); } -app.use(logger('dev')) +app.use(morgan('dev')) app.use(express.static(__dirname + '/static')) app.get('/', function (req, res, next) { @@ -150,16 +142,16 @@ wss.broadcast = function broadcast(data) { }; wss.on('connection', function (ws) { - console.log('[websocket] client connected'); + logger.info('websocket client connected'); ws.on('close', function () { - console.log('[websocket] client disconnected'); + logger.info('websocket client disconnected'); }); }); server.on('request', app); 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: