+ logging.js : adding winston logging

This commit is contained in:
nicobo 2021-06-09 23:24:38 +02:00
parent 5e22ae450b
commit 332e25eb13
No known key found for this signature in database
GPG key ID: 2581E71C5FA5285F
2 changed files with 31 additions and 18 deletions

View file

@ -1,6 +1,6 @@
const yargs = require('yargs'); const yargs = require('yargs');
const path = require('path'); const path = require('path');
const log = require('winston'); const log = require('./logging');
const udp = require('dgram'); const udp = require('dgram');
const server = udp.createSocket('udp4'); const server = udp.createSocket('udp4');
const http = require('http'); const http = require('http');
@ -31,16 +31,16 @@ const send = (host, path, headers) =>
}); });
resp.on('end', () => { resp.on('end', () => {
// console.log(data); // log.info(data);
}); });
} }
) )
.on('error', err => { .on('error', err => {
console.error('Error on send(',host,path,headers,') :', err.message); log.error('Error on send(',host,path,headers,') :', err.message);
}); });
const sendEventServerAddress = (hostname,port) => { const sendEventServerAddress = (hostname,port) => {
//console.debug("sendEventServerAddress",hostname,port); //log.debug("sendEventServerAddress %s %s",hostname,port);
send(hostname, send(hostname,
'/YamahaExtendedControl/v1', { '/YamahaExtendedControl/v1', {
'X-AppName': 'MusicCast/1', 'X-AppName': 'MusicCast/1',
@ -51,12 +51,12 @@ const sendEventServerAddress = (hostname,port) => {
server.on('close', () => { server.on('close', () => {
console.log('Server is closed!'); log.info('Server is closed!');
// TODO ? Notify the device not to send events anymore ? // TODO ? Notify the device not to send events anymore ?
}); });
server.on('error', error => { server.on('error', error => {
console.error('Socket error (',host,path,headers,') :', error); log.error('Socket error (',host,path,headers,') :', error);
server.close(); server.close();
}); });
@ -65,9 +65,9 @@ server.on('message', (msg, _info) => {
try { try {
body = JSON.parse(msg.toString('utf8')); body = JSON.parse(msg.toString('utf8'));
// console.log(body); // log.info(body);
} catch (err) { } catch (err) {
console.warn('Could not parse event', msg.toString()); log.warn('Could not parse event', msg.toString());
return; return;
} }
@ -82,10 +82,7 @@ server.on('listening', () => {
const port = address.port; const port = address.port;
const ipaddr = address.address; const ipaddr = address.address;
console.log( log.info( 'Incoming event server is listening at port %s:%s', ipaddr, port );
'Incoming event server is listening at port',
ipaddr + ':' + port
);
// Register at each configured 'source' // Register at each configured 'source'
var sourcesDict = {}; var sourcesDict = {};
@ -99,7 +96,7 @@ server.on('listening', () => {
var sourcesList = Object.keys(sourcesDict); var sourcesList = Object.keys(sourcesDict);
for ( var s=0 ; s<sourcesList.length ; s++ ) { for ( var s=0 ; s<sourcesList.length ; s++ ) {
var source = sourcesList[s]; var source = sourcesList[s];
console.log("Registering with port",port,"at ",source); log.info("Registering with port %s at %s",port,source);
sendEventServerAddress(source,port); sendEventServerAddress(source,port);
// After 10 minutes the receiver will drop this server to be notified unless we // After 10 minutes the receiver will drop this server to be notified unless we
@ -122,7 +119,7 @@ const argv = yargs
.help() .help()
.alias('help', 'h') .alias('help', 'h')
.argv; .argv;
log.debug("argv:", argv); log.debug("argv: %o", argv);
// Instanciates the handlers for each scenario // Instanciates the handlers for each scenario
var scenarii = []; var scenarii = [];
@ -130,17 +127,17 @@ const scripts = argv.scripts;
for ( var s=0 ; s<scripts.length ; s++ ) { for ( var s=0 ; s<scripts.length ; s++ ) {
var scenarioModule = scripts[s]; var scenarioModule = scripts[s];
console.log("Loading scenario :", scenarioModule); log.info("Loading scenario : %s", scenarioModule);
var scenarioClass = require(scenarioModule); var scenarioClass = require(scenarioModule);
var scenarioName = path.basename(scenarioModule, path.extname(scenarioModule)); var scenarioName = path.basename(scenarioModule, path.extname(scenarioModule));
console.log("Scenario name :", scenarioName); log.info("Scenario name : %s", scenarioName);
// Merges top options and scenario-specific ones (specific overrides top ones) // Merges top options and scenario-specific ones (specific overrides top ones)
var conf = Object.assign({}, argv); var conf = Object.assign({}, argv);
if ( argv.conf !== undefined && argv.conf[scenarioName] !== undefined ) { if ( argv.conf !== undefined && argv.conf[scenarioName] !== undefined ) {
conf = Object.assign(conf, argv.conf[scenarioName]); conf = Object.assign(conf, argv.conf[scenarioName]);
} }
console.log("Scenario conf. :", conf); log.info("Scenario conf. %o :", conf);
scenarii.push({ scenarii.push({
name: scenarioName, name: scenarioName,
@ -148,7 +145,7 @@ for ( var s=0 ; s<scripts.length ; s++ ) {
handler: new scenarioClass(conf) handler: new scenarioClass(conf)
}); });
} }
console.log("Scenarii :", scenarii); log.info("Scenarii : %o", scenarii);
server.bind(INCOMING_EVENT_SERVER_PORT, LOCAL_IP); server.bind(INCOMING_EVENT_SERVER_PORT, LOCAL_IP);

16
logging.js Normal file
View file

@ -0,0 +1,16 @@
const winston = require('winston');
module.exports = winston.createLogger({
level: 'debug',
transports: [
new winston.transports.Console()
],
format: winston.format.combine(
winston.format.timestamp(),
winston.format.splat(),
winston.format.cli(),
winston.format.printf((info) => {
return `${info.timestamp} ${info.level}\t${info.message}`;
}),
),
});