Change the logger to send at a specific interval and with a different message format

Log messages to PubNub are now an array instead of an object.
Each element of the array is an object with m (message), t (timestamp) and s (isSystem, optional) attributes.
Logs are sent at a specific interval (110ms, fit with some margin to PubNub's approximated 10 messages/s limit), and truncated to PubNub's 32KB limit.
This commit is contained in:
Pablo Carranza Velez 2016-07-13 11:55:50 -03:00
parent 6de51543a5
commit a8dd29cd82
3 changed files with 28 additions and 17 deletions

View File

@ -1,3 +1,5 @@
* **[Breaking]** Change the logger to send at a specific interval and with a different message format [Pablo]
# v1.14.0
* Allow using an HTTP header for auth [Pablo]

View File

@ -77,7 +77,7 @@ logTypes =
humanName: 'Restarting application'
logSystemMessage = (message, obj, eventName) ->
logger.log({ message, isSystem: true })
logger.log({ m: message, s: true })
utils.mixpanelTrack(eventName ? message, obj)
logSystemEvent = (logType, app, error) ->

View File

@ -5,6 +5,13 @@ Promise = require 'bluebird'
es = require 'event-stream'
Lock = require 'rwlock'
LOG_PUBLISH_INTERVAL = 110
# Pubnub's message size limit is 32KB (unclear on whether it's KB or actually KiB,
# but we'll be conservative). So we limit a log message to 2 bytes less to account
# for the [ and ] in the array.
MAX_LOG_BYTE_SIZE = 31998
disableLogs = false
initialised = new Promise (resolve) ->
@ -21,6 +28,7 @@ dockerPromise = initialised.then (config) ->
# Queue up any calls to publish logs whilst we wait to be initialised.
publish = do ->
publishQueue = []
publishQueueRemainingBytes = MAX_LOG_BYTE_SIZE
initialised.then (config) ->
if config.offlineMode
@ -29,25 +37,26 @@ publish = do ->
return
pubnub = PUBNUB.init(config.pubnub)
channel = config.channel
doPublish = ->
return if publishQueue.length is 0
pubnub.publish({ channel, message: publishQueue })
publishQueue = []
publishQueueRemainingBytes = MAX_LOG_BYTE_SIZE
setInterval(doPublish, LOG_PUBLISH_INTERVAL)
# Redefine original function
publish = (message) ->
return (message) ->
# Disable sending logs for bandwidth control
return if disableLogs
return if disableLogs or publishQueueRemainingBytes <= 0
if _.isString(message)
message = { message }
message = { m: message }
_.defaults message,
timestamp: Date.now()
# Stop pubnub logging loads of "Missing Message" errors, as they are quite distracting
message: ' '
t: Date.now()
m: ''
msgLength = Buffer.byteLength(JSON.stringify(message), 'utf8')
publishQueueRemainingBytes -= msgLength
publishQueue.push(message) if publishQueueRemainingBytes >= 0
pubnub.publish({ channel, message })
# Replay queue now that we have initialised the publish function
publish(args...) for args in publishQueue
return -> publishQueue.push(arguments)
# disable: A Boolean to pause the Log Publishing - Logs are lost when paused.
exports.disableLogPublishing = (disable) ->