Add logger & small refactor

master^2
Álex Hermida 2018-05-03 16:46:46 +02:00
parent 38b608ec5c
commit f9c3ebdcd0
4 changed files with 32 additions and 8 deletions

View File

@ -8,7 +8,6 @@ clients = {}
class Client class Client
constructor: (@ws) -> constructor: (@ws) ->
@.id = uuid.v4() @.id = uuid.v4()
@.handleEvents() @.handleEvents()
handleEvents: () -> handleEvents: () ->
@ -25,7 +24,7 @@ class Client
try try
msg = JSON.parse(message) msg = JSON.parse(message)
catch e catch e
return null console.error "Error: ", e
if msg.cmd == 'ping' if msg.cmd == 'ping'
@.sendPong() @.sendPong()
@ -58,7 +57,7 @@ class Client
try try
@ws.send(JSON.stringify({cmd: "pong"})) @ws.send(JSON.stringify({cmd: "pong"}))
catch e catch e
console.error("Error: ", e) console.error "Error: ", e
close: () -> close: () ->
if @.subscriptionManager if @.subscriptionManager

View File

@ -3,5 +3,11 @@
"secret": "mysecret", "secret": "mysecret",
"webSocketServer": { "webSocketServer": {
"port": 8888 "port": 8888
},
"loggerOptions": {
"level": "debug",
"handleExceptions": true,
"json": false,
"colorize": true
} }
} }

View File

@ -1,13 +1,31 @@
winston = require 'winston'
webSocket = require 'ws'
eventsConfig = require('./events-config') eventsConfig = require('./events-config')
argv = require('minimist')(process.argv.slice(2)); argv = require('minimist')(process.argv.slice(2))
eventsConfig.loadConfigFile(argv.config || './config') eventsConfig.loadConfigFile(argv.config || './config')
config = eventsConfig.config
config = eventsConfig.config; client = require './client'
client = require('./client') WebSocketServer = webSocket.Server
simplestFormat = winston.format.printf((info) =>
"#{info.timestamp} #{info.message}")
logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
simplestFormat
),
transports: [
new winston.transports.Console(config.loggerOptions)
],
exitOnError: false,
});
WebSocketServer = require('ws').Server
wss = new WebSocketServer(config.webSocketServer) wss = new WebSocketServer(config.webSocketServer)
wss.on 'connection', (ws) -> wss.on 'connection', (ws) ->

View File

@ -29,6 +29,7 @@
"bluebird": "^2.9.10", "bluebird": "^2.9.10",
"minimist": "^1.2.0", "minimist": "^1.2.0",
"node-uuid": "^1.4.2", "node-uuid": "^1.4.2",
"winston": "^3.0.0-rc5",
"ws": "^2.0.3" "ws": "^2.0.3"
} }
} }