From f9c3ebdcd09008bf81e5ac453ae39f444320e35a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex=20Hermida?= Date: Thu, 3 May 2018 16:46:46 +0200 Subject: [PATCH 1/2] Add logger & small refactor --- client.coffee | 5 ++--- config.example.json | 6 ++++++ index.coffee | 28 +++++++++++++++++++++++----- package.json | 1 + 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/client.coffee b/client.coffee index 200cad9..1d774f3 100644 --- a/client.coffee +++ b/client.coffee @@ -8,7 +8,6 @@ clients = {} class Client constructor: (@ws) -> @.id = uuid.v4() - @.handleEvents() handleEvents: () -> @@ -25,7 +24,7 @@ class Client try msg = JSON.parse(message) catch e - return null + console.error "Error: ", e if msg.cmd == 'ping' @.sendPong() @@ -58,7 +57,7 @@ class Client try @ws.send(JSON.stringify({cmd: "pong"})) catch e - console.error("Error: ", e) + console.error "Error: ", e close: () -> if @.subscriptionManager diff --git a/config.example.json b/config.example.json index 3d2bd84..9905a06 100644 --- a/config.example.json +++ b/config.example.json @@ -3,5 +3,11 @@ "secret": "mysecret", "webSocketServer": { "port": 8888 + }, + "loggerOptions": { + "level": "debug", + "handleExceptions": true, + "json": false, + "colorize": true } } diff --git a/index.coffee b/index.coffee index bbe9842..4f6a3bb 100644 --- a/index.coffee +++ b/index.coffee @@ -1,13 +1,31 @@ +winston = require 'winston' +webSocket = require 'ws' + eventsConfig = require('./events-config') -argv = require('minimist')(process.argv.slice(2)); - +argv = require('minimist')(process.argv.slice(2)) 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.on 'connection', (ws) -> diff --git a/package.json b/package.json index a66a773..c367b01 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "bluebird": "^2.9.10", "minimist": "^1.2.0", "node-uuid": "^1.4.2", + "winston": "^3.0.0-rc5", "ws": "^2.0.3" } } From ef1300b6ecb1c5d6e84c11f9b3f9ce684f16b1df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex=20Hermida?= Date: Tue, 8 May 2018 16:47:29 +0200 Subject: [PATCH 2/2] Refactor logger --- client.coffee | 9 +++++---- config.example.json | 6 ------ index.coffee | 19 ++----------------- logger.coffee | 24 ++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 logger.coffee diff --git a/client.coffee b/client.coffee index 1d774f3..27d5c8f 100644 --- a/client.coffee +++ b/client.coffee @@ -1,6 +1,7 @@ uuid = require('node-uuid') signing = require('./signing') SubscriptionManager = require('./subscription').SubscriptionManager +logger = require('./logger').logger clients = {} @@ -17,14 +18,14 @@ class Client handleError: (error) -> req = @ws.upgradeReq headers = req.headers - console.log "evt=client_error", "x_forwarded_for=#{headers['x-forwarded-for']}" - console.log "Error: ", error + logger.error "evt=client_errorx_forwarded_for=%s", headers['x-forwarded-for'] + logger.error error handleMessage: (message) -> try msg = JSON.parse(message) catch e - console.error "Error: ", e + return null if msg.cmd == 'ping' @.sendPong() @@ -57,7 +58,7 @@ class Client try @ws.send(JSON.stringify({cmd: "pong"})) catch e - console.error "Error: ", e + logger.error e close: () -> if @.subscriptionManager diff --git a/config.example.json b/config.example.json index 9905a06..3d2bd84 100644 --- a/config.example.json +++ b/config.example.json @@ -3,11 +3,5 @@ "secret": "mysecret", "webSocketServer": { "port": 8888 - }, - "loggerOptions": { - "level": "debug", - "handleExceptions": true, - "json": false, - "colorize": true } } diff --git a/index.coffee b/index.coffee index 4f6a3bb..984f072 100644 --- a/index.coffee +++ b/index.coffee @@ -1,4 +1,5 @@ -winston = require 'winston' +logger = require('./logger').logger + webSocket = require 'ws' eventsConfig = require('./events-config') @@ -10,22 +11,6 @@ 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, -}); - wss = new WebSocketServer(config.webSocketServer) wss.on 'connection', (ws) -> diff --git a/logger.coffee b/logger.coffee new file mode 100644 index 0000000..077adf5 --- /dev/null +++ b/logger.coffee @@ -0,0 +1,24 @@ +winston = require 'winston' + +simplestFormat = winston.format.printf((info) => + "#{info.timestamp} #{info.level} #{info.message}") + +logger = winston.createLogger({ + format: winston.format.combine( + winston.format.splat(), + winston.format.timestamp({ + format: 'YYYY-MM-DD HH:mm:ss' + }), + simplestFormat + ), + transports: [ + new winston.transports.Console({ + level: "debug", + handleExceptions: true, + json: false, + colorize: true + }) + ], +}); + +exports.logger = logger