diff --git a/README.md b/README.md index d150f762..2993c16b 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,27 @@ The promise returns an instance of Y. We denote it with a lower case `y`. * y.db.userId :: String * The used user id for this client. **Never overwrite this** +### Logging +Yjs uses [debug](https://github.com/visionmedia/debug) for logging. The flag +`y*` enables logging for all y-* components. You can selectively remove +components you are not interested in: E.g. The flag `y*,-y:connector-message` +will not log the long `y:connector-message` messages. + +##### Enable logging in Node.js +```sh +DEBUG=y* node app.js +``` + +Remove the colors in order to log to a file: +```sh +DEBUG_COLORS=0 DEBUG=y* node app.js > log +``` + +##### Enable logging in the browser +```js +localStorage.debug = 'y*' +``` + ## Changelog ### 12.0.0 diff --git a/src/Connector.js b/src/Connector.js index d1ea7eae..bc2fc722 100644 --- a/src/Connector.js +++ b/src/Connector.js @@ -43,6 +43,8 @@ module.exports = function (Y/* :any */) { } else { throw new Error("Role must be either 'master' or 'slave'!") } + this.log = Y.debug('y:connector') + this.logMessage = Y.debug('y:connector-message') this.y.db.forwardAppliedOperations = opts.forwardAppliedOperations || false this.role = opts.role this.connections = {} @@ -73,8 +75,10 @@ module.exports = function (Y/* :any */) { } } reconnect () { + this.log('reconnecting..') } disconnect () { + this.log('discronnecting..') this.connections = {} this.isSynced = false this.currentSyncTarget = null @@ -84,7 +88,7 @@ module.exports = function (Y/* :any */) { return this.y.db.stopGarbageCollector() } repair () { - console.info('Repairing the state of Yjs. This can happen if messages get lost, and Yjs detects that something is wrong. If this happens often, please report an issue here: https://github.com/y-js/yjs/issues') + this.log('Repairing the state of Yjs. This can happen if messages get lost, and Yjs detects that something is wrong. If this happens often, please report an issue here: https://github.com/y-js/yjs/issues') for (var name in this.connections) { this.connections[name].isSynced = false } @@ -95,6 +99,7 @@ module.exports = function (Y/* :any */) { } setUserId (userId) { if (this.userId == null) { + this.log('Set userId to "%s"', userId) this.userId = userId return this.y.db.setUserId(userId) } else { @@ -109,6 +114,7 @@ module.exports = function (Y/* :any */) { } userLeft (user) { if (this.connections[user] != null) { + this.log('User left: %s', user) delete this.connections[user] if (user === this.currentSyncTarget) { this.currentSyncTarget = null @@ -132,6 +138,7 @@ module.exports = function (Y/* :any */) { if (this.connections[user] != null) { throw new Error('This user already joined!') } + this.log('User joined: %s', user) this.connections[user] = { isSynced: false, role: role @@ -200,9 +207,12 @@ module.exports = function (Y/* :any */) { } } send (uid, message) { - if (this.debug) { - console.log(`send ${this.userId} -> ${uid}: ${message.type}`, message) // eslint-disable-line - } + this.log('Send \'%s\' to %s', message.type, uid) + this.logMessage('Message: %j', message) + } + broadcast (message) { + this.log('Broadcast \'%s\'', message.type) + this.logMessage('Message: %j', message) } /* Buffer operations, and broadcast them when ready. @@ -239,11 +249,10 @@ module.exports = function (Y/* :any */) { if (sender === this.userId) { return Promise.resolve() } - if (this.debug) { - console.log(`receive ${sender} -> ${this.userId}: ${message.type}`, JSON.parse(JSON.stringify(message))) // eslint-disable-line - } + this.log('Receive \'%s\' from %s', message.type, sender) + this.logMessage('Message: %j', message) if (message.protocolVersion != null && message.protocolVersion !== this.protocolVersion) { - console.error( + this.log( `You tried to sync with a yjs instance that has a different protocol version (You: ${this.protocolVersion}, Client: ${message.protocolVersion}). The sync was stopped. You need to upgrade your dependencies (especially Yjs & the Connector)! diff --git a/src/y.js b/src/y.js index fe5d77cf..ff7a9b98 100644 --- a/src/y.js +++ b/src/y.js @@ -8,6 +8,9 @@ require('./Struct.js')(Y) require('./Utils.js')(Y) require('./Connectors/Test.js')(Y) +Y.debug = require('debug') +// Y.debug.log = console.log.bind(console) + var requiringModules = {} module.exports = Y